What are some best practices for accessing data from another website using SQL queries in PHP?

When accessing data from another website using SQL queries in PHP, it is important to ensure that you have permission to access the data and that you are following best practices for security and data integrity. One common approach is to use APIs provided by the website to access the data in a secure and controlled manner.

<?php

// Set up cURL to make API request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.example.com/data');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

// Parse the JSON response
$data = json_decode($result, true);

// Connect to your database
$mysqli = new mysqli('localhost', 'username', 'password', 'database');

// Insert data into your database using prepared statements
$stmt = $mysqli->prepare("INSERT INTO table_name (column1, column2) VALUES (?, ?)");
$stmt->bind_param('ss', $data['value1'], $data['value2']);
$stmt->execute();

// Close the database connection
$mysqli->close();

?>