tags:

views:

557

answers:

5

In PHP, how can I determine if any remote file (accessed via HTTP) exists?

A: 

Use Curl, and check if the request went through successfully. http://w-shadow.com/blog/2007/08/02/how-to-check-if-page-exists-with-curl/

CodeJoust
Curl is a good suggestion. I'm not sure what the link you included has to do with anything.
Jason Leveille
Thanks, I can blame a faulty chome on linux clipboard for that.
CodeJoust
+5  A: 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); //follow up to 10 redirections - avoids loops
$data = curl_exec($ch);
curl_close($ch);
if (!$data) {
  echo "Domain could not be found";
}
else {
  preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/",$data,$matches);
  $code = end($matches[1]);
  if ($code == 200) {
    echo "Page Found";
  }
  elseif ($code == 404) {
    echo "Page Not Found";
  }
}

Modified version of code from here.

Dominic Rodger
+1  A: 

I recently was looking for the same info. Found some really nice code here: http://php.assistprogramming.com/check-website-status-using-php-and-curl-library.html

    function Visit($url){

    $agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL,$url );
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch,CURLOPT_VERBOSE,false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    $page=curl_exec($ch);
    //echo curl_error($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);




    if($httpcode >= 200 && $httpcode < 300){ 
     return true;
    }
    else {
     return false;
    }

}

    if(Visit("http://www.site.com")){
        echo "Website OK";
    }
    else{
        echo "Website DOWN";
    }
ign
what about `304 Not Modified`, `307 Temporary Redirect`, etc?
nickf
@nickf - Is `304 Not Modified` relevant in the context of a curl fetch? I don't know what headers curl sends along - presuming it doesn't send anything other than what you specify, how would the server know whether or not the page has modified since you last asked for it?
Dominic Rodger
+1  A: 

I like curl or fsockopen to solve this problem. Either one can provide header data regarding the status of the file requested. Specifically, you would be looking for a 404 (File Not Found) response. Here is an example I've used with fsockopen:

http://www.php.net/manual/en/function.fsockopen.php#39948

Jason Leveille
Of course that should be 404 "Not Found" and not 404 "File Not Found"
Jason Leveille
+2  A: 

This function will return the response code (the last one in case of redirection), or false in case of a dns or other error. If one argument (the url) is supplied a HEAD request is made. If a second argument is given, a full request is made and the content, if any, of the response is stored by reference in the variable passed as the second argument.

function url_response_code($url, & $contents = null)
{
    $context = null;
    if (func_num_args() == 1) {
        $context = stream_context_create(array('http' => array('method' => 'HEAD')));
    }
    $contents = @file_get_contents($url, null, $context);
    $code = false;
    if (isset($http_response_header)) {
        foreach ($http_response_header as $header) {
            if (strpos($header, 'HTTP/') === 0) {
                list(, $code) = explode(' ', $header);
            }
        }
    }
    return $code;
}
GZipp