gpt4 book ai didi

PHP:fopen 失败 "HTTP Request Failed",但响应 header 的状态代码为 200

转载 作者:可可西里 更新时间:2023-10-31 23:34:42 24 4
gpt4 key购买 nike

我有一个 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:37 GMT
[3] => Content-Type: application/x-bittorrent
[4] => Cache-Control: must-revalidate, post-check=0, pre-check=0
[5] => Content-Disposition: attachment; filename="example.torrent"
[6] => Vary: Accept-Encoding,User-Agent
[7] => Connection: close
)

有时,是这样的:

   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
)

这是来自成功尝试的响应 header :

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 并将其设置为开。

来自 RFC-2616,第 10 节:

200 OK

The request has succeeded. The information returned with the response is dependent on the method used in the request, for example:

GET an entity corresponding to the requested resource is sent in the response;

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

最佳答案

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

关于PHP:fopen 失败 "HTTP Request Failed",但响应 header 的状态代码为 200,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4705902/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com