2012-06-22 50 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兼容。