2012-05-21 18 views
-1

我有一個網站可以剔除它的姊妹網站,但由於報告原因,我希望能夠計算出需要運行的任務需要多長時間。我如何用PHP來解決這個問題,甚至有可能嗎?刮刮時的加工時機

在一個理想的世界中,如果任務無法連接到5秒後實際運行,我想從運行中終止該功能並報告失敗。

謝謝大家!

回答

2

如果您使用捲曲刮,你可以使用這樣

// create a new cURL resource 
$ch = curl_init(); 

// set URL and other appropriate options including timeout 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // capture the result in a string 
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // The number of seconds to wait while trying to connect. 
// grab the info 
if (!$result = curl_exec($ch)) 
{ 
    trigger_error(curl_error($ch)); 
} 

// close cURL resource, and free up system resources 
curl_close($ch); 

// process the $result 
+0

我能直接在PHP文件運行此超時功能? – David

+0

當然,你只需要啓用php_curl擴展,並設置$ url =「http://site_i_want_to_grab.com/some_page/some_id」 –

+0

我該如何去添加一個條件這個?例如在我的情況下,我想: if(you-can-find-the-url){do this} else {做到這一點並記錄錯誤} – David