PHP - 如何检查您的网站是否已启动(最多15个站点)

时间:2015-03-07 18:18:03

标签: php web

我想创建一个脚本来检查我的所有网站是否已启动。 该代码适用于一个站点,但是当我尝试检查例如一次有10个站点,代码停止工作。

<?php
    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";
     $ch=curl_init();
     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_exec($ch);
     $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     curl_close($ch);
     if($httpcode>=200 && $httpcode<300)
         return true;
     else
        return false;
    }
    $do = array();
    $n = 0;

    $myfile = fopen("domens.txt", "r") or die("Unable to open file!");
        while(!feof($myfile)) {
            $do = fgets($myfile);
            $n = $n + 1;
        }
    fclose($myfile);
    echo '<br><br>';
    $trimmed = file('domens.txt', FILE_SKIP_EMPTY_LINES);
    for($x=0;$x<$n;$x++){
        if(checkStatus($trimmed[$x]))
            echo " <br>Website is up  " . $trimmed[$x];
        else
            echo "  <br> Website is down ". $trimmed[$x];
    }
?>

2 个答案:

答案 0 :(得分:0)

要检查您的网络服务器是否仍然正常工作,请尝试使用Nagios或Icinga等监控服务器。

答案 1 :(得分:0)

function checkOnline($domain) {
   $curlInit = curl_init($domain);
   curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10);
   curl_setopt($curlInit,CURLOPT_HEADER,true);
   curl_setopt($curlInit,CURLOPT_NOBODY,true);
   curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true);

   //get answer
   $response = curl_exec($curlInit);

   curl_close($curlInit);
   if ($response) return true;
   return false;
}

if(checkOnline('http://google.com')) { echo "google online\n"; }
if(checkOnline('http://facebook.com')) { echo "facebook online\n"; }
if(checkOnline('http://stackoverflow.com')) { echo "stackoverflow online\n"; }

来自Get the site status - up or down的代码