2011-08-26 187 views
4

我有一個問題,我的應用程序試圖訪問超時的URL。我試圖抓住這個超時和我使用此代碼來解決這個問題:fread超時'mod_fcgid:讀取超時管道'

$timeout = 120; 

    if(false == $handle = @fsockopen($host, $port, $errno, $errstr, $timeout)) 
    { 
     throw new Exception("Could not connect to url: ".$errstr); 
    } 

    $getRequest = "GET {$url} HTTP/1.0\r\n"; 
    $getRequest .= "Host: {$urlParts['host']}\r\n"; 
    $getRequest .= "Connection: close\r\n\r\n"; 

    fwrite($handle, $getRequest); 

    $maxExecutionTime = ini_get('max_execution_time'); 
    set_time_limit($timeout+10); 
    stream_set_timeout($handle, $timeout); 

    $head = fread($handle, 1024); // Read the first 1024 bytes 

    if($maxExecutionTime == 0) { 
     $maxExecutionTime = 30; 
    } 
    set_time_limit($maxExecutionTime); 

    $stream_metadata = stream_get_meta_data($handle); 

    if($stream_metadata['timed_out'] === true) { 
     throw new Exception("Connection timed out"); 
    } 

我使用的超時我的URL是在防火牆後面,所以我不能共享,但它的設計睡覺()5分鐘。當我嘗試運行這段代碼時,執行停在$ head = fread($ handle,1024);並在90秒後,我從服務器「腳本的提前結束」得到500錯誤。當我在調試級別的Apache日誌調查我看到:

[Fri Aug 26 11:10:45 2011] [warn] [client 192.168.10.202] mod_fcgid: read timeout from pipe 
[Fri Aug 26 11:10:45 2011] [error] [client 192.168.10.202] Premature end of script headers: validateUrl.php 

與「validateUrl.php」是URL我通過訪問這個腳本。我不確定將FcgidIOTimeout增加到更高的值是安全的,因爲它適用於我的頁面的全部。任何想法/意見?

系統細節: PHP 5.2.13版本在Windows NT上EPG-WEB 5.2構建3790 Apache上運行:阿帕奇/ 2.2.19(Win32的)mod_fcgid/2.3.6

+0

嘗試使用LIB捲曲,但這HTTP和超時爲你辦理 – Dani

+0

我需要在來回流動到服務器的數據非常嚴格的控制,所以我喜歡如果可以的話,堅持使用套接字。 – Drew

回答

12

嘗試一些這些指令在你的httpd.conf中:

<IfModule fcgid_module> 

    FcgidIOTimeout 1200 
    FcgidConnectTimeout 1200 
    FcgidBusyScanInterval 1200 
    FcgidBusyTimeout 1200 
    FcgidErrorScanInterval 1200 
    FcgidIdleScanInterval 1200 
    FcgidIdleTimeout 1200 

    FcgidProcessLifeTime 3600 
    FcgidZombieScanInterval 1200 

</IfModule> 

來源:http://httpd.apache.org/mod_fcgid/mod/mod_fcgid.html

+2

設置FcgidIOTimeout和FcgidBusyTimeout以超過PHP中的max_execution_time很方便。但是,您可能希望將* Interval時間保留爲默認值,或作爲相應值的倍數,或者相應值的超時時間可能幾乎是您設置的兩倍(例如,實際的忙時間可能是BusyTimeout + BusyScanInterval)。 – Reuben