How to check for broken url in PHP

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!!!

4 responses to “How to check for broken url in PHP

  1. Pingback: Tweets that mention How to check for broken url in PHP | Code is poetry .... -- Topsy.com

  2. Pingback: abcphp.com

  3. 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

  4. savitas

    yeah, thatnks

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Links

phpcamp pune 2011: A gathering of php enthusiast

Twitter

  • We are looking to hire few #WordPress developers with 1+ years of experience for our team apply now
    http://t.co/aFOdK1QY
    2012/02/20 13:05

  • http://t.co/rHQoh2GT
    2012/02/18 01:26
  • Can We Have Indian Language Website or Blog in WordPress?
    http://t.co/sNF4Fmwt
    2012/02/14 11:39
  • How to Create Your Own Super Simple #WordPress Plugins
    http://t.co/uUVEA62u
    2012/02/09 20:44
  • going to read "Internet Marketing with WordPress" over weekend, will write a review as well
    http://t.co/E0wXmXlp
    2012/02/08 16:32

Pages