Today I was working on a site, where I had to set a no-image placeholder on the places where images are not present, that means I need to check images are present or not, its a very simple job, but what I found was image url’s are coming from database and they all are broken links.
So now my job is to write a code which will check the images links are broken or not if broken then place a no-image place holder else image url. Here is the example on how I checked broken image url’s:
function check_url($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
$headers = curl_getinfo($ch);
curl_close($ch);
return $headers['http_code'];
}
$myurl = "http://www.flickr.com/photos/thecancerus/2869110105/";
$satus = check_url($myurl);
if($satus == '200')
echo "Its works";
else
echo "broken url";
Hope it helps!!!


Pingback: Tweets that mention How to check for broken url in PHP | Code is poetry .... -- Topsy.com
Pingback: abcphp.com
Good. In fact you also can set CURLOPT_RETURNTRANSFER to false. You don’t need the return. You only need the status 200 OK. With CURLOPT_RETURNTRANSFER=0 you will save a little of unnecesary network trafic
yeah, thatnks