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.
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);
                        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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With