curl 和 ping - 如何检查网站是启动还是关闭?

问题描述:

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

I want to check whether a website is up or down at a particular instance using PHP. I came to know that curl will fetch the contents of the file but I don't want to read the content of the website. I just want to check the status of the website. Is there any way to check the status of the site? Can we use ping to check the status? It is sufficient for me to get the status signals like (404, 403, etc) from the server. A small snippet of code might help me a lot.

这样的事情应该可行

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