php get_headers一个很好的方法来判断一个网站是否已启动?

时间:2012-01-12 22:15:08

标签: php get-headers

我还是相当熟悉php,你能不能评论下面的代码是否有利于判断某个网站是关闭还是关闭以及它是否适合原因和更好的替代方案?

提前致谢。

$siteHeader = @get_headers($url , 1);
if ($siteHeader > 1) {
    $siteUp = true;
} else {
    $siteUp = false;
}

3 个答案:

答案 0 :(得分:2)

我使用curl,但那就是我:

function check($url, $ignore = '')
{
    $agent = "Mozilla/4.0 (B*U*S)";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);

    curl_setopt($ch, CURLOPT_VERBOSE, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 45);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 45);

    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, 5); //follow up to 10 redirections - avoids loops


    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //fix for certificate issue
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //fix for certificate issue


    $page = curl_exec($ch);
    $err = curl_error($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    $codes = array(
        0 => 'Domain Not Found',
        100 => 'Continue',
        101 => 'Switching Protocols',
        200 => 'OK',
        201 => 'Created',
        202 => 'Accepted',
        203 => 'Non-Authoritative Information',
        204 => 'No Content',
        205 => 'Reset Content',
        206 => 'Partial Content',
        300 => 'Multiple Choices',
        301 => 'Moved Permanently',
        302 => 'Found',
        303 => 'See Other',
        304 => 'Not Modified',
        305 => 'Use Proxy',
        307 => 'Temporary Redirect',
        400 => 'Bad Request',
        401 => 'Unauthorized',
        402 => 'Payment Required',
        403 => 'Forbidden',
        404 => 'Not Found',
        405 => 'Method Not Allowed',
        406 => 'Not Acceptable',
        407 => 'Proxy Authentication Required',
        408 => 'Request Timeout',
        409 => 'Conflict',
        410 => 'Gone',
        411 => 'Length Required',
        412 => 'Precondition Failed',
        413 => 'Request Entity Too Large',
        414 => 'Request-URI Too Long',
        415 => 'Unsupported Media Type',
        416 => 'Requested Range Not Satisfiable',
        417 => 'Expectation Failed',
        500 => 'Internal Server Error',
        501 => 'Not Implemented',
        502 => 'Bad Gateway',
        503 => 'Service Unavailable',
        504 => 'Gateway Timeout',
        505 => 'HTTP Version Not Supported'
    );

    $httpcode_out = 'http: ' . $httpcode . ' (' . $codes[$httpcode] . ')';
    $err = 'curl error: ' . $err;

    $out = array(
        $url,
        $httpcode_out,
        $err
    );

    if ($httpcode >= 200 && $httpcode < 307)
    {//good
        return array(
            'Work',
            $out
        );
    }
    else
    {//BAD
        return array(
            'Fail',
            $out
        );
    }
}

答案 1 :(得分:1)

更新:我想的越多,看起来就越不精致。我已经扩展了我最初的答案,回想起来是天真的。

基本用法很好,但您可能也想查看HTTP响应代码,而不仅仅是检查是否有响应。代码现在的方式,它只是告诉你有人在另一边听,这与大多数人会认为“网站已经启动”相差很远。

以下是如何轻松隔离HTTP响应代码(如果请求失败,则获取false):

$headers = get_headers('http://www.google.com');
$code = $headers ? intval(end(explode(' ', $headers[0], 2))) : false;

除此之外,还有重定向的问题:如果你看到重定向,你会怎么做?您查询的服务器可能没问题,但您重定向到的服务器可能已关闭。如果有人在浏览器中输入了URL,他们将被重定向并最终超时,而一步测试会说一切都没问题。如果有重定向循环怎么办?浏览器会检测到这种情况并最终超时,但是你需要编写相当多的代码才能做到这一点。

所以最后cURL看起来确实是唯一可靠的解决方案,因为它可以透明地完成所有这些。

答案 2 :(得分:1)

根据您的情况而定,特别是如果您的网址是由用户提交的,我会选择类似的内容。

//returns true, if domain is availible, false if not
function isDomainAvailible($domain)
{
   //check if URL is valid
   if(!filter_var($domain, FILTER_VALIDATE_URL)){
           return false;
   }

   $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, 10);
   curl_setopt ($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
   curl_setopt ($ch,CURLOPT_SSLVERSION, 3);
   curl_setopt ($ch,CURLOPT_SSL_VERIFYHOST, FALSE);
   $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;
}

主要是因为如果您不发送使用者,某些服务器将不会响应。