2012-05-05 128 views
1

我試圖做腳本,檢查站點是否啓動或關閉。一切工作正常,但當網站不是一個網站,我的意思是該網站不存在。然後php執行加載時間增加。我想以某種方式處理這個問題。如何檢查網站是否存在並打破捲曲會話?這裏是我的腳本:PHP:如何處理捲曲響應

<?php 
$ch = curl_init('http://86.100.69.228/'); // e.g. for this will increase execution time because the site doesn't exists 
curl_setopt($ch, CURLOPT_NOBODY, true); 
curl_exec($ch); 
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
echo $status_code; 
curl_close($ch); 
?> 

回答

1

您可以通過在curl_setopt設置捲曲轉移CURLOPT_CONNECTTIMEOUT和CURLOPT_TIMEOUT選項定義超時。如果網站停工,cURL將停止工作,您會收到它!

1

如果你想檢查一個網站是否關閉CURL可能不是最好的選擇,因爲你沒有檢查完整的URL,如http://eample.com/abx.phpx ......當你使用的服務,如「安全的DNS」,如Comodo http://www.comodo.com/secure-dns/所有網站將返回200因爲請求自動轉發驗證,所以你總是會裝載場與IP地址92.242.144.10Search 92.242.144.10

我的建議是隻是簡單的ping服務器

function ping($host) 
{ 
    exec(sprintf('ping -n 2 %s', escapeshellarg($host)), $res, $rval); 
    return $rval === 0; 
} 

$result = ping("86.100.69.228"); 

出檢查socke牛逼直接

function pingSocket($host, $port = 80, $timeout = 3) { 
    $fsock = fsockopen ($host, $port, $errno, $errstr, $timeout); 
    if (! $fsock) { 
     return FALSE; 
    } else { 
     return TRUE; 
    } 
} 

$result = pingSocket ("86.100.69.228"); 

這更好的工作大部分時間