Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php cURL操作在120308毫秒后超时,收到1个字节中的X个字节_Php_Curl_Screen Scraping - Fatal编程技术网

php cURL操作在120308毫秒后超时,收到1个字节中的X个字节

php cURL操作在120308毫秒后超时,收到1个字节中的X个字节,php,curl,screen-scraping,Php,Curl,Screen Scraping,在我的抓取脚本中,我偶尔会遇到这个错误(请参阅标题) X是大于0的整数字节数,是Web服务器响应时发送的实际字节数。我用Charles proxy调试了这个问题,下面是我看到的 正如您所看到的,响应中没有Content-Length:header,代理仍在等待数据(因此cURL等待了2分钟并放弃了) cURL错误代码是28 下面是该请求的var_export'ed curl_getinfo()的详细curl输出中的一些调试信息: * About to connect() to proxy 12

在我的抓取脚本中,我偶尔会遇到这个错误(请参阅标题)

X是大于0的整数字节数,是Web服务器响应时发送的实际字节数。我用Charles proxy调试了这个问题,下面是我看到的

正如您所看到的,响应中没有Content-Length:header,代理仍在等待数据(因此cURL等待了2分钟并放弃了)

cURL错误代码是28

下面是该请求的var_export'ed curl_getinfo()的详细curl输出中的一些调试信息:

* About to connect() to proxy 127.0.0.1 port 8888 (#584)
*   Trying 127.0.0.1...
* Adding handle: conn: 0x2f14d58
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 584 (0x2f14d58) send_pipe: 1, recv_pipe: 0
* Connected to 127.0.0.1 (127.0.0.1) port 8888 (#584)
> GET http://bakersfield.craigslist.org/sof/3834062623.html HTTP/1.0
User-Agent: Firefox (WindowsXP) Ц Mozilla/5.1 (Windows; U; Windows NT 5.1; en-GB
; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6
Host: bakersfield.craigslist.org
Accept: */*
Referer: http://bakersfield.craigslist.org/sof/3834062623.html
Proxy-Connection: Keep-Alive

< HTTP/1.1 200 OK
< Cache-Control: max-age=300, public
< Last-Modified: Thu, 11 Jul 2013 21:50:17 GMT
< Date: Thu, 11 Jul 2013 21:50:17 GMT
< Vary: Accept-Encoding
< Content-Type: text/html; charset=iso-8859-1
< X-MCP-Cache-Control: max-age=2592000, public
< X-Frame-Options: SAMEORIGIN
* Server Apache is not blacklisted
< Server: Apache
< Expires: Thu, 11 Jul 2013 21:55:17 GMT
* HTTP/1.1 proxy connection set close!
< Proxy-Connection: Close
<
* Operation timed out after 120308 milliseconds with 4636 out of -1 bytes receiv
ed
* Closing connection 584
Curl error: 28 Operation timed out after 120308 milliseconds with 4636 out of -1
 bytes received http://bakersfield.craigslist.org/sof/3834062623.htmlarray (
  'url' => 'http://bakersfield.craigslist.org/sof/3834062623.html',
  'content_type' => 'text/html; charset=iso-8859-1',
  'http_code' => 200,
  'header_size' => 362,
  'request_size' => 337,
  'filetime' => -1,
  'ssl_verify_result' => 0,
  'redirect_count' => 0,
  'total_time' => 120.308,
  'namelookup_time' => 0,
  'connect_time' => 0,
  'pretransfer_time' => 0,
  'size_upload' => 0,
  'size_download' => 4636,
  'speed_download' => 38,
  'speed_upload' => 0,
  'download_content_length' => -1,
  'upload_content_length' => 0,
  'starttransfer_time' => 2.293,
  'redirect_time' => 0,
  'certinfo' =>
  array (
  ),
  'primary_ip' => '127.0.0.1',
  'primary_port' => 8888,
  'local_ip' => '127.0.0.1',
  'local_port' => 63024,
  'redirect_url' => '',
)
*即将()连接到代理127.0.0.1端口8888(#584)
*正在尝试127.0.0.1。。。
*正在添加句柄:conn:0x2f14d58
*正在添加句柄:发送:0
*正在添加句柄:recv:0
*卷曲度加把手尺寸线:长度:1
*-Conn 584(0x2f14d58)发送管道:1,接收管道:0
*连接到127.0.0.1(127.0.0.1)端口8888(#584)
>得到http://bakersfield.craigslist.org/sof/3834062623.html HTTP/1.0
用户代理:Firefox(WindowsXP)和Mozilla/5.1(Windows;U;Windows NT 5.1;en GB
;rv:1.8.1.6)Gecko/20070725 Firefox/2.0.0.6
主持人:bakersfield.craigslist.org
接受:*/*
推荐人:http://bakersfield.craigslist.org/sof/3834062623.html
代理连接:保持活动状态
'http://bakersfield.craigslist.org/sof/3834062623.html',
'内容类型'=>'文本/html;字符集=iso-8859-1',
“http_代码”=>200,
“标题大小”=>362,
“请求大小”=>337,
“文件时间”=>-1,
“ssl验证结果”=>0,
“重定向\u计数”=>0,
“总时间”=>120.308,
“名称查找时间”=>0,
“连接时间”=>0,
“预传输时间”=>0,
“大小上传”=>0,
“下载大小”=>4636,
“速度下载”=>38,
“速度上传”=>0,
“下载内容长度”=>-1,
“上传内容长度”=>0,
“开始传输时间”=>2.293,
“重定向时间”=>0,
“certinfo”=>
排列(
),
'主ip'=>'127.0.0.1',
“主端口”=>8888,
'本地ip'=>'127.0.0.1',
“本地_端口”=>63024,
'重定向url'=>'',
)

我可以做一些事情,比如添加一个curl选项来避免这些超时。这不是连接超时,也不是数据等待超时-这两种设置都不起作用,因为curl实际上成功连接并接收了一些数据,因此错误超时总是~=120000毫秒。

我注意到您正在尝试解析Craigslist;这可能是他们的防洪保护措施吗? 如果您尝试解析其他网站,问题是否仍然存在?我曾经在尝试递归映射FTP时遇到过同样的问题

关于超时,如果您确定这既不是连接超时,也不是数据等待超时(CURLOPT_CONNECTTIMEOUT/CURLOPT_timeout),我会尝试增加PHP脚本本身的限制:

set_time_limit(0);

尝试在PHP配置文件
PHP.ini
中增加
default\u socket\u timeout
(例如
~/PHP.ini
),例如

或者在PHP代码中将其设置为:

curl_setopt($res, CURLOPT_TIMEOUT, 300);

其中,
$res
是您现有的资源变量。

您好,谢谢您的回复,但是设置了时间限制,CURLOPT_TIMEOUT和CURLOPT_CONNECTTIMEOUT选项由传递给curl包装类实例的选项数组更改,CURLOPT_TIMEOUT选项在我运行之前的测试时存在,我刚刚将CURLOPT_CONNECTTIMEOUT选项添加到该数组中,验证它已设置为curl句柄并重新运行,但再次,在10-20个请求中,同样的问题也存在。我仍然认为这是他们的某种抗洪机制。尝试每秒发出1个或更少的请求,看看是否仍然发生。是否尝试使用其他方式(如telnet)进行连接?也许你会有更多的信息。顺便说一下,Craigslist使用条款明确禁止爬虫。谢谢Hal,这不是我的项目/想法。我要把这个奖给你,因为这是唯一的答案。将尝试添加睡眠呼叫,但现在它也或多或少地与此故障一起工作,因此这些故障只需2分钟,然后继续正常运行,如果24/7运行,则不会有太多故障
curl_setopt($res, CURLOPT_TIMEOUT, 300);