检查网站是否宕机PHP代码

<?php
function Networkcheck($url){
   $agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0";
   //curl_init-初始化一个curl会话
   $ch=curl_init();
   //curl_setopt — 为一个curl设置会话参数
   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, 5);
   curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
   curl_setopt($ch,CURLOPT_SSLVERSION,3);
   curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE);
   //curl_exec —执行一个curl会话
   $page=curl_exec($ch);
   //curl_getinfo — 获取一个curl连接资源句柄的信息
   $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
   //curl_close()函数的作用是关闭一个curl会话,唯一的参数是curl_init()函数返回的句柄。
   curl_close($ch);
   if($httpcode>=200 && $httpcode<300) 
       return true;
   else 
       return false;
}
//函数参数为要检查的网站的网址路径
if(Networkcheck("http://www.baidu.com"))
    echo "Website OK";
else
    echo "Website DOWN";
?>

你可能感兴趣的:(检查网站是否宕机PHP代码)