PHP:fopen失败“;HTTP请求失败";,但响应头的状态代码为200

PHP:fopen失败“;HTTP请求失败";,但响应头的状态代码为200,php,http,proxy,fopen,Php,Http,Proxy,Fopen,我有一个PHP脚本,应该连接到一个代理,从代理列表中选择并下载一个文件。有些代理(在200-400个工作代理中)工作得很好,但其他代理则不行,我无法找到原因 下面是通过代理连接的代码: $proxy = determine_proxy ($proxyList); $proxyString = 'tcp://' . $proxy['ip'] . ':' . $proxy['port']; $userAgent = $userAgents [rand (0, $agentsCount

我有一个PHP脚本,应该连接到一个代理,从代理列表中选择并下载一个文件。有些代理(在200-400个工作代理中)工作得很好,但其他代理则不行,我无法找到原因

下面是通过代理连接的代码:

    $proxy = determine_proxy ($proxyList);
 $proxyString = 'tcp://' . $proxy['ip'] . ':' . $proxy['port'];

 $userAgent = $userAgents [rand (0, $agentsCount - 1)];

 // set up our headers
 $hdrs = array( 'http' => array(
     'method' => "GET",
     'header'=> "Host: www.example.net\r\n" .
      // "User-Agent: $userAgent\r\n" .
      "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" .
      "Accept-Language: en-us,en;q=0.5\r\n" .
      "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" . 
      "Keep-Alive: 115\r\n" .
      "Proxy-Connection: keep-alive\r\n" .
  "Referer: http://$url",     // Setting the http-referer
     'proxy' => "$proxyString",
     'request_fulluri' => true
     )
 );

 echo "Using proxy: "; print_r ($proxy); echo '<br>';

 $context = stream_context_create ($hdrs);  // set up the context

 $timeout = 3;
 $oldTimeout = ini_set('default_socket_timeout', $timeout);
 $oldAgent = ini_set ('user_agent', $userAgent);

 $fp = fopen ("http://www.example.net$file", 'r', false, $context); // open the file

 if (!$fp) {
  echo 'fopen failed! Skipping this proxy for now...<br>';
  print_r ($http_response_header); echo '<br />';
  unset ($http_response_header);
  flush(); @ob_flush();
  ini_set ('user_agent', $oldAgent);
  ini_set('default_socket_timeout', $oldTimeout);
  continue;
 }

 print_r ($http_response_header); echo '<br />';
 unset ($http_response_header);
有时是这样的:

   Array ( 
 [0] => HTTP/1.0 200 OK 
 [1] => Server: falcon 
 [2] => Date: Sun, 16 Jan 2011 14:06:47 GMT 
 [3] => Content-Type: application/x-bittorrent 
 [4] => Cache-Control: must-revalidate, post-check=0, pre-check=0 
 [5] => Content-Disposition: attachment; filename="example2.torrent" 
 [6] => Vary: Accept-Encoding,User-Agent 
 [7] => X-Cache: MISS from proxy 
 [8] => Proxy-Connection: close
)
这是成功尝试的响应标题:

HTTP/1.0 200 OK
Server: falcon
Date: Fri, 21 Jan 2011 18:53:00 GMT
Content-Type: application/x-bittorrent
Cache-Control: must-revalidate, post-check=0, pre-check=0
Content-Disposition: attachment; filename="example3.torrent"
Vary: Accept-Encoding,User-Agent
X-Cache: MISS from www.example.com
X-Cache-Lookup: MISS from www.example.com:3128
Via: 1.0 www.example.com (squid/3.0.STABLE23-BZR)
Proxy-Connection: close
我正在将用户代理设置为有效的用户代理字符串,我已选中allow_url_fopen,并将其设置为On

根据RFC-2616第10节:

200行

请求已成功。这个 随响应返回的信息 取决于中使用的方法 请求,例如:

获取对应于 请求的资源以 反应


服务器通过代理返回的状态为200,而fopen仍然失败,这怎么可能呢?有人知道这个问题以及如何解决吗?

服务器报告200 OK,但代理仍然不知道将此数据转发到何处,因此您收到了一个失败的请求


尝试使用VIA头文件

问题在于,我设置的套接字超时在某些情况下太低,fopen无法管理和下载所有数据。超时时间过后,fopen仍然没有下载数据,它返回FALSE并抛出“HTTP请求失败”错误。

但在下载成功的情况下,我也会得到完全相同的头(我问题中的第一个示例)。这怎么可能?你所说的“代理仍然不知道将数据转发到哪里”是什么意思?一些(旧)代理可能不会将数据发送回你,除非你告诉他们(在我不知道的某个标题中),一些(旧)代理不知道将数据发送到哪里,除非你告诉他们(通过某个标题)但这在您的情况下可能不正确,因为您已成功下载,请尝试“代理连接:关闭\r\n”。我尝试使用代理连接:close,结果是一样的。奇怪的是,在尝试失败后的响应标头中,有时甚至会有一个内容处置标头!我尝试过避免CURL,但我想我也必须尝试一下。我还查看了所有HTTP标头请求字段,但我找不到一个,可以提供服务您描述的目的(告诉代理将数据转发到哪里)。由于您已经回答了自己的问题,请将答案标记为“已接受”。这将阻止该问题出现在未回答的问题列表中,也将有助于将来找到此页面的任何人寻找类似的答案。谢谢。我习惯将其打开几天,以便其他人能够回答(可能以更好的方式)。
HTTP/1.0 200 OK
Server: falcon
Date: Fri, 21 Jan 2011 18:53:00 GMT
Content-Type: application/x-bittorrent
Cache-Control: must-revalidate, post-check=0, pre-check=0
Content-Disposition: attachment; filename="example3.torrent"
Vary: Accept-Encoding,User-Agent
X-Cache: MISS from www.example.com
X-Cache-Lookup: MISS from www.example.com:3128
Via: 1.0 www.example.com (squid/3.0.STABLE23-BZR)
Proxy-Connection: close