What are potential issues with using file_get_contents to retrieve data from multiple websites in PHP?

One potential issue with using file_get_contents to retrieve data from multiple websites in PHP is that it may not handle errors or timeouts gracefully, leading to script failures or long wait times. To solve this, you can use the cURL extension in PHP, which provides more control and flexibility when making HTTP requests.

// Initialize cURL session
$ch = curl_init();

// Set the URL to retrieve data from
curl_setopt($ch, CURLOPT_URL, 'https://www.example.com');

// Set options for handling timeouts and errors
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

// Execute the request and store the response
$response = curl_exec($ch);

// Close cURL session
curl_close($ch);

// Check for errors or handle response data
if($response === false){
    echo 'Error: ' . curl_error($ch);
} else {
    // Process the response data
    echo $response;
}