Check if a remote page exists using PHP?

2019-01-12 08:16发布

问题:

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

回答1:

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



回答2:

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



回答3:

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;
}


回答4:

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";
    }


回答5:

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/



回答6:

Just a note that these solutions will not work on a site that does not give an appropriate response for a page not found. e.g I just had a problem with testing for a page on a site as it just loads a main site page when it gets a request it cannot handle. So the site will nearly always give a 200 response even for non-existent pages.

Some sites will give a custom error on a standard page and not still not give a 404 header.

Not much you can do in these situations unless you know the expected content of the page and start testing that the expected content exists or test for some expected error text within the page and that is all getting a bit messy...



标签: php http