php curl超时采集,PHP curl超时问题

博客探讨了在使用PHP的curl库时遇到的超时问题,特别是如何区分并设置CURLOPT_CONNECTTIMEOUT和CURLOPT_TIMEOUT。文章指出,CURLOPT_CONNECTTIMEOUT用于限制建立连接的时间,而CURLOPT_TIMEOUT则限制接收完整响应的时间。通过设置这两个参数,可以避免因服务器响应慢或文件下载大导致的长时间等待。解决方案中提到了将超时时间设置为毫秒级别以更精确控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天调试一个非常老的代码时  发现nginx服务器超时  改了下nginx配置

发现是后台脚本一直等待  排查到最后发现是curl 超时引起的等待

具体解决方案:

curl_setopt( $this->ch, CURLOPT_URL, $url );

curl_setopt( $this->ch, CURLOPT_RETURNTRANSFER, 1 );

curl_setopt( $this->ch, CURLOPT_TIMEOUT_MS,3000);      //  3秒超时

curl_setopt( $this->ch, CURLOPT_HEADER, 0 );

curl_setopt( $this->ch, CURLOPT_SSL_VERIFYPEER, false );

curl_setopt( $this->ch, CURLOPT_SSL_VERIFYHOST, false );

PHP curl的超时主要有4个参数 原文如下;

There‘s a very distinctive difference between these two configurations within cURL. I‘ll try to define them for you, and then provide you a very common example which I share to people who I teach about cURL.

CURLOPT_CONNECTTIMEOUT is designed to tell the script how long to wait to make a successful connection to the server before starting to buffer the output. A destination‘s server which may be overloaded, offline or crashed would probably make this setting become useful.

CURLOPT_TIMEOUT is designed to tell the script how long to wait to receive a completely buffered output from the server. A destination‘s huge file, slow connection speeds or slow rendering would probably make this setting become useful.

A good example of where these will both apply to, is when you‘re telling cURL to download a MP3 file. CURLOPT_CONNECTTIMEOUT would be set at about 10 seconds which would mean that if no response is provided within 10 seconds then the script will abort, and CURLOPT_TIMEOUT would be set at about 100 seconds which would mean if the MP3 has not downloaded within 100 seconds then abort the script. It‘s the best way of explaining it to developers.

具体是:

CURLOPT_CONNECTTIMEOUT

建立连接时候的超时设置

CURLOPT_TIMEOUT

接收信息时的超时设置

CURLOPT_CONNECTTIMEOUT_MS,  CURLOPT_TIMEOUT_MS 意思相同 只是超时单位为毫秒了。。。

原文:http://www.cnblogs.com/glory-jzx/p/4581413.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值