What potential issues can arise when using file_get_contents in PHP to retrieve data from APIs?

One potential issue that can arise when using file_get_contents in PHP to retrieve data from APIs is that it may not handle errors or HTTP status codes properly. To solve this, you can use the PHP cURL extension, which provides more control and flexibility when making HTTP requests and handling responses.

// Using cURL to retrieve data from an API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.example.com/data');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

if($response === false){
    echo 'Error: ' . curl_error($ch);
} else {
    // Process the API response
    echo $response;
}

curl_close($ch);