php curl 和 ping - 如何检查网站是启动还是关闭?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4607684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
curl and ping - how to check whether a website is either up or down?
提问by brainless
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.
我想使用 PHP 检查特定实例中的网站是启动还是关闭。我开始知道 curl 会获取文件的内容,但我不想阅读网站的内容。我只想检查网站的状态。有什么办法可以检查网站的状态吗?我们可以使用ping来检查状态吗?我从服务器获取状态信号(404、403 等)就足够了。一小段代码可能对我有很大帮助。
回答by Jeff Busby
something like this should work
这样的事情应该工作
$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
}
回答by Dinesh Rabara
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;
回答by ratz
curl -Is $url | grep HTTP | cut -d ' ' -f2
回答by Daniel Szalay
Here is how I did it. I set the user agent to minimize the chance of the target banning me and also disabled SSL verification since I know the target:
这是我如何做到的。我设置了用户代理以最大程度地减少目标禁止我的机会并禁用 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);
}
回答by mikel
Have you seen the get_headers() function ? http://it.php.net/manual/en/function.get-headers.php. It seems to do exactly what you need.
你见过 get_headers() 函数吗?http://it.php.net/manual/en/function.get-headers.php。它似乎完全符合您的需求。
If you use curl directly with the -I flag, it will return the HTTP headers (404 etc) instead of the page HTML. In PHP, the equivalent is the curl_setopt($ch, CURLOPT_NOBODY, 1);
option.
如果您直接使用带有 -I 标志的 curl,它将返回 HTTP 标头(404 等)而不是页面 HTML。在 PHP 中,等效的是curl_setopt($ch, CURLOPT_NOBODY, 1);
选项。
回答by girasquid
ping
won't do what you're looking for - it will only tell you if the machine is up (and responding to ping
). That doesn't necessarily mean that the webserver is up, though.
ping
不会做您正在寻找的事情 - 它只会告诉您机器是否已启动(并响应ping
)。但这并不一定意味着网络服务器已启动。
You might want to try using the http_headmethod - it'll retrieve the headers that the webserver sends back to you. If the server is sending back headers, then you know it's up and running.
您可能想尝试使用http_head方法 - 它会检索网络服务器发回给您的标头。如果服务器正在发回标头,那么您就知道它已启动并正在运行。
回答by KingCrunch
You can not test a webserver with ping
, because its a different service. The server may running, but the webserver-daemon may be crashed anyway. So curl is your friend. Just ignore the content.
您不能使用 来测试网络服务器ping
,因为它是一项不同的服务。服务器可能正在运行,但无论如何 webserver-daemon 可能会崩溃。所以 curl 是你的朋友。直接无视内容。