2012-06-22 59 views
1

stream_get_contents似乎不能正確處理持久(KeepAlive)連接。它在返回之前等待連接超時。默認情況下,Apache 2.2具有5秒的KeepAliveTimeout。我能做些什麼嗎? (除了禁用服務器上的KeepAlive,或使用protocol_version 1.0)stream_get_contents卡住持久(KeepAlive)連接

$opts = array('http' => 
    array(
     'method' => 'GET', 
     'protocol_version' => 1.1, 
    ) 
); 
$context = stream_context_create($opts); 
$stream = fopen('http://google.com', 'r', false, $context); 
$metadata = stream_get_meta_data($stream); 
$data = stream_get_contents($stream); 
fclose($stream); 

謝謝。

+0

與循環+ feof()+ fread()相同的問題。 – goat

回答

2
$opts = array('http' => 
    array(
     'method' => 'GET', 
     'protocol_version' => 1.1, 
     'header' => 'Connection: close' 
    ) 
); 

Connection: close告訴服務器不使用持久連接和響應發送後丟棄的TCP連接。

這是part of the HTTP/1.1 standard,並作爲PHP manual說:

如果[PROTOCOL_VERSION]設定爲1.1這是你的責任是1.1兼容。