卷曲和ping - 如何检查网站是上升还是下降?

bra*_*ess 23 php webserver curl ping http-status-codes

我想使用PHP检查网站在特定实例中是启动还是关闭.我开始知道curl会获取文件的内容,但我不想阅读网站的内容.我只想查看网站的状态.有没有办法检查网站的状态?我们可以使用ping来检查状态吗?我可以从服务器获取状态信号(404,403等).一小段代码可能对我有很大帮助.

Jef*_*sby 44

这样的事情应该有效

    $url = 'yoururl';
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_exec($ch);
    $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if (200==$retcode) {
        // All's well
    } else {
        // not so much
    }
Run Code Online (Sandbox Code Playgroud)

  • 我也更喜欢将`CURLOPT_CONNECTTIMEOUT`和`CURLOPT_TIMEOUT`设置为较低的值。 (2认同)
  • 成功的 HTTP 响应代码比 200 多得多。 https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#2xx_Success (2认同)

Din*_*ara 9

function checkStatus($url) {
    $agent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; pt-pt) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27";

    // initializes curl session
    $ch = curl_init();

    // sets the URL to fetch
    curl_setopt($ch, CURLOPT_URL, $url);

    // sets the content of the User-Agent header
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);

    // make sure you only check the header - taken from the answer above
    curl_setopt($ch, CURLOPT_NOBODY, true);

    // follow "Location: " redirects
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    // return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // disable output verbose information
    curl_setopt($ch, CURLOPT_VERBOSE, false);

    // max number of seconds to allow cURL function to execute
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);

    // execute
    curl_exec($ch);

    // get HTTP response code
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);

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

// how to use
//===================
if ($this->checkStatus("http://www.dineshrabara.in"))
    echo "Website is up";
else
    echo "Website is down";
exit;
Run Code Online (Sandbox Code Playgroud)


小智 7

curl -Is $url | grep HTTP | cut -d ' ' -f2
Run Code Online (Sandbox Code Playgroud)

  • 当提供解决问题的代码时,最好还至少给出一个简短的解释,告诉他们如何工作,这样人们就不必逐行解析它来理解差异. (18认同)

mik*_*kel 5

你见过get_headers()函数吗?http://it.php.net/manual/en/function.get-headers.php.它似乎完全符合你的需要.

如果直接使用curl -I标志,它将返回HTTP标头(404等)而不是页面HTML.在PHP中,等价物是curl_setopt($ch, CURLOPT_NOBODY, 1);选项.


gir*_*uid 5

ping不会做你正在寻找的事情 - 它只会告诉你机器是否已启动(并响应ping)。但这并不一定意味着网络服务器已经启动。

您可能想尝试使用http_head方法 - 它会检索网络服务器发送回给您的标头。如果服务器发回标头,那么您就知道它已启动并正在运行。


Dan*_*lay 5

这是我如何做到的。我设置了用户代理以最大程度地减少目标禁止我的机会并禁用 SSL 验证,因为我知道目标:

private static function checkSite( $url ) {
    $useragent = $_SERVER['HTTP_USER_AGENT'];

    $options = array(
            CURLOPT_RETURNTRANSFER => true,      // return web page
            CURLOPT_HEADER         => false,     // do not return headers
            CURLOPT_FOLLOWLOCATION => true,      // follow redirects
            CURLOPT_USERAGENT      => $useragent, // who am i
            CURLOPT_AUTOREFERER    => true,       // set referer on redirect
            CURLOPT_CONNECTTIMEOUT => 2,          // timeout on connect (in seconds)
            CURLOPT_TIMEOUT        => 2,          // timeout on response (in seconds)
            CURLOPT_MAXREDIRS      => 10,         // stop after 10 redirects
            CURLOPT_SSL_VERIFYPEER => false,     // SSL verification not required
            CURLOPT_SSL_VERIFYHOST => false,     // SSL verification not required
    );
    $ch = curl_init( $url );
    curl_setopt_array( $ch, $options );
    curl_exec( $ch );

    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return ($httpcode == 200);
}
Run Code Online (Sandbox Code Playgroud)