What is the best practice for extracting and storing data from external websites using PHP?
When extracting and storing data from external websites using PHP, it is best practice to use cURL library to make HTTP requests and retrieve the data. Once the data is fetched, it can be parsed and stored in a database or file for further processing.
<?php
// Initialize cURL session
$ch = curl_init();
// Set the URL to fetch data from
curl_setopt($ch, CURLOPT_URL, 'https://example.com/data');
// Set option to return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Execute the request
$data = curl_exec($ch);
// Close cURL session
curl_close($ch);
// Parse and store the data as needed
// For example, storing in a file
file_put_contents('data.txt', $data);
?>