2011-01-05 101 views
19

我想检查一个网站是在一个特定的实例使用PHP启动或关闭。我开始知道curl会获取文件的内容,但我不想阅读网站的内容。我只想检查网站的状态。有没有办法检查网站的状态?我们可以使用ping来检查状态吗?我只需从服务器获取状态信号(404,403等)就足够了。一小段代码可能会帮助我很多。curl和ping - 如何检查网站是否启动或关闭?

+2

你怎么定义'up'?返回HTTP'200'的空白页面已启动? – webbiedave 2011-01-05 18:28:28

+0

@Nate:感谢您的编辑! – brainless 2011-01-05 21:11:30

回答

42

这样的事情应该工作

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

我也更喜欢设置'CURLOPT_CONNECTTIMEOUT'和'CURLOPT_TIMEOUT'以降低数值。 – machineaddict 2013-10-18 10:15:38

+0

@machineaddict你的意思是输出“网站离线”,而不是等待连接?我没有看到一次检查2次和等待几次当前操作之间的区别。 – erm3nda 2015-03-13 07:49:55

+0

@machineaddict即使将CURLOPT_FOLLOWLOCATION设置为true,仍然会返回301返回码......这怎么可能? – 2015-06-18 23:01:55

5

你见过get_headers()函数吗? http://it.php.net/manual/en/function.get-headers.php。它似乎正是你所需要的。

如果使用-I标志直接使用curl,它将返回HTTP标头(404等)而不是页面HTML。在PHP中,相当于curl_setopt($ch, CURLOPT_NOBODY, 1);选项。

2

ping不会做你正在寻找的东西 - 它只会告诉你机器是否启动(并响应ping)。但这并不一定意味着网络服务器已经启动。

您可能想尝试使用http_head方法 - 它将检索Web服务器发回给您的标头。如果服务器正在发回标题,那么你知道它已经启动并正在运行。

+0

此嵌入式超链接http://ca.php.net/http_head重定向到http://ca.php.net/manual-lookup.php?pattern=http_head&lang=en&scope=404quickref其中包含以下消息:*** http_head * *不存在。最近的匹配项:*。为了包含正确的参考,需要编辑答案。 – 2018-03-09 17:16:43

2

您无法使用ping测试网络服务器,因为它的服务不同。服务器可能正在运行,但webserver-daemon可能会崩溃。所以卷曲是你的朋友。只是忽略内容。

9
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; 
6
curl -Is $url | grep HTTP | cut -d ' ' -f2 
+11

当提供解决问题的代码时,最好也是至少给出一个关于它是如何工作的简短解释,以便让人们阅读并不需要从头到尾仔细分析,以便理解差异。 – Fluffeh 2012-09-27 11:17:10

4

这里是我做到了。我设置了用户代理以最大限度地减少目标禁止我的机会,并且还禁用了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); 
}