如何检测外部网站是否正常工作?

时间:2015-07-24 15:23:17

标签: php http

如何检测外部网站是否正常工作?我曾考虑过HTTP ERROR MESSAGE。一般来说:

if ( <<something(url)>> != 200 ) {
  // website defined in url working (up)
} else {
  // website defined in url not working (down)
}

200是定义成功查询网址的代码。只是这样理解在这里阅读:http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

1 个答案:

答案 0 :(得分:1)

尝试使用CURL。该示例改编自cur_getinfo()

// Create a curl handle
$ch = curl_init('http://stackoverflow.com/');
//Return only headers
curl_setopt($ch, CURLOPT_NOBODY, true);
// Execute
curl_exec($ch);
// Check if any error occurred
if(!curl_errno($ch))
   $info = curl_getinfo($ch);
// Close handle
curl_close($ch);

if ( isset($info) && $info['http_code'] == 200 )
  echo "Website is up!";
else 
  echo "Website is down.";