2012-11-16 30 views
1

在PHP中,我打開一個流,寫入它,然後從它讀取。我想設置流的讀取超時,但無論我設置了多少(0微秒,10微秒),元數據都不會顯示「timed_out」!stream_set_timeout永遠不會工作,無論多麼低的超時

相關代碼:

//open the socket 
if ($fp = fsockopen(gethostbyname(host), port, $errno, $errstr, $timeout)) { 

    //Send command to the host 
    if (fwrite($fp, $requestCommand)) { 
     //Set timeout and blocking 
     stream_set_blocking($fp, FALSE); 
     stream_set_timeout($fp, 0, 10); 

     //Check for timeout 
     $info = stream_get_meta_data($fp); 
     echo $info[ 'timed_out' ]; 

     //Read and check for timeout 
     while (!$info['timed_out'] && !feof($fp)) { 
      $response .= fread($fp, 4096); 

      //Get meta data (which has timeout info) 
      $info = stream_get_meta_data($fp); 
     } 
    } 
} 

我在做什麼錯?

+0

您正在CLI或瀏覽器中執行此操作嗎? – Pheonix

回答

1

我找到的關鍵是stream_set_blocking($fp, TRUE)

如果FALSE,那麼$status['timed_out']似乎沒有任何實際效果。 TRUE [PHP默認]有效。

+0

工作!謝謝!我在PHP網站上看到的例子已將它設置爲FALSE,由於某種原因,我從來沒有想過將它作爲阻止。你知道這是API「合同」應該工作的方式嗎? –

相關問題