Is it advisable to use CURL or fsockopen for checking the status of external image URLs in PHP, and why?
When checking the status of external image URLs in PHP, it is advisable to use CURL over fsockopen. CURL is a more robust and feature-rich library for making HTTP requests, including handling redirects and various protocols, making it more suitable for checking the status of external URLs.
// Using CURL to check the status of an external image URL
$url = 'https://example.com/image.jpg';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status == 200) {
echo "Image URL is valid";
} else {
echo "Image URL is invalid";
}