2011-01-05 97 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); 
}