tags:

views:

316

answers:

3

I need to create a function that returns if a URL is reachable or valid.

I am currently using something like the following to determine a valid url:

static public function urlExists($url)
{
    $fp = @fopen($url, 'r');

    if($fp)
    {
        return true;
    }
    return false;
}

It seems like there would be something faster, maybe something that just fetched the page header or something.

+2  A: 

You'll likely be limited to sending some kind of HTTP request. Then you can check HTTP status codes.

Be sure to send only a "HEAD" request, which doesn't pull back all the content. That ought to be sufficient and lightweight enough.

Omega
+6  A: 

You can use curl as follows:

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true); // set to HEAD request
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // don't output the response
curl_exec($ch);
$valid = curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200;
curl_close($ch);
rojoca
Also check for 2xx and 3xx because it could be 301 Moved Permanently or another http code that means the url works.
Alex L
+1  A: 

You could check http status code.

Here is a code you could use to check that an url returns 2xx or 3xx http code to ensure the url works.

<?php

$url = "http://stackoverflow.com/questions/1122845";

function urlOK($url)
{

    $url_data = parse_url ($url);
    if (!$url_data) return FALSE;

   $errno="";
   $errstr="";
   $fp=0;

   $fp=fsockopen($url_data['host'],80,$errno,$errstr,30);

   if($fp===0) return FALSE;
   $path ='';
   if  (isset( $url_data['path'])) $path .=  $url_data['path'];
   if  (isset( $url_data['query'])) $path .=  '?' .$url_data['query'];

   $out="GET /$path HTTP/1.1\r\n";
   $out.="Host: {$url_data['host']}\r\n";
   $out.="Connection: Close\r\n\r\n";

   fwrite($fp,$out);
   $content=fgets($fp);
   $code=trim(substr($content,9,4)); //get http code
   fclose($fp);
   // if http code is 2xx or 3xx url should work
   return  ($code[0] == 2 || $code[0] == 3) ? TRUE : FALSE;
}

echo $url;
if (urlOK($url)) echo " is a working URL";
    else echo " is a bad URL";
?>

Hope this helps!

Alex L