curl CURLOPT_TIMEOUT_MS 小于 1 秒无效的解决方案
|
![]() |
![]() |
![]() |
| 【性价之王】 | 【线路之王】 | 【价格之王】 | 【配置之王】 |
| 【免费之王】 | 【香港首推】 | 【梯子之王】 | 【独服之王】 |
CURLOPT_TIMEOUT 设置 cURL 允许执行的最长秒数
CURLOPT_TIMEOUT_MS 设置 cURL 允许执行的最长毫秒数
CURLOPT_CONNECTTIMEOUT 在发起连接前等待的时间,如果设置为 0,则无限等待
CURLOPT_CONNECTTIMEOUT_MS 尝试连接等待的时间,以毫秒为单位。如果设置为 0,则无限等待
CURLOPT_TIMEOUT_MS 在 cURL 7.16.2 中被加入。从 PHP 5.2.3 起可使用。
所以使用的时候请先查看 libcurl 版本 curl --version。
但是这个函数有个 bug,如果时间小于 1000 毫秒也就是 1 秒的话,会立马报错,查看下面说明
If you want cURL to timeout in less than one second, you can use CURLOPT_TIMEOUT_MS, although there is a bug/"feature" on "Unix-like systems" that causes libcurl to timeout immediately if the value is < 1000 ms with the error "cURL Error (28): Timeout was reached". The explanation for this behavior is: "If libcurl is built to use the standard system name resolver, that portion of the transfer will still use full-second resolution for timeouts with a minimum timeout allowed of one second." What this means to PHP developers is "You can use this function without testing it first, because you can't tell if libcurl is using the standard system name resolver (but you can be pretty sure it is)" The problem is that on (Li|U)nix, when libcurl uses the standard name resolver, a SIGALRM is raised during name resolution which libcurl thinks is the timeout alarm. The solution is to disable signals using CURLOPT_NOSIGNAL. Here's an example script that requests itself causing a 10-second delay so you can test timeouts: 增加 curl_setopt($ch, CURLOPT_NOSIGNAL, 1) 可以解决此问题: if (!isset($_GET['foo'])) { // Client $ch = curl_init('http://localhost/timeout.php?foo=bar'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_NOSIGNAL, 1); curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200); $data = curl_exec($ch); $curl_errno = curl_errno($ch); $curl_error = curl_error($ch); curl_close($ch); if ($curl_errno > 0) { echo "cURL Error ($curl_errno): $curl_errorn"; } else { echo "Data received: $datan"; } } else { // Server sleep(10); echo "Done."; } 猜你可能想看的VPS
- 长三角地区的算力需求规模到底有多大? 全球[VPS测评]
- 香港云服务器的优势有哪些方面 香港VPS[主机]
- 创意加载进度HTML动画特效,SVG+CSS3实现的动画死神来了load 全球[VPS测评]
- 香港的云服务器值得租用吗?有什么好处? 香港VPS[主机]
- CentOS 7.x 系统安装和配置redis-7.0.4集群 全球[VPS测评]
- 40火伞高压电爬坡更让企业用电于民 全球[VPS测评]
- 曾经最大跨境电商平台团队收缩,只剩20人丨GoingGlobal周报 全球[VPS测评]
- 狗云DogYun,香港cn2云服务器/优惠码/流量包,按小时计费云服务器 香港VPS[主机]
- 轻云互联,香港VPS月付22元 美国VPS月付19元 美国VPS[主机]
- 极客主机,优惠8折充值返25%,香港CN2VPS,日本软银VPS,新加坡 日本VPS[主机]
- 企鹅小屋:香港ntt vps,大带宽直连,1核/1G/2T流量/半年15 香港VPS[主机]
- tudcloud:香港vps终身7折,香港服务器终身9折,移动访问尤其优 香港VPS[主机]
- edgenat,低价年付促销活动,香港vps 4核8G仅300元/年,美 美国VPS[主机]
- 云群网络怎么样?香港云服务器高端弹性云2核2G2M带宽,仅588元/年 香港VPS[主机]
- 香港cn2线路服务器哪里好?香港cn2服务器推荐哪家好? 香港VPS[主机]
- 恒星云:喜迎新春香港CN2云服务器,首月9.9元;深圳2核4G云服务器仅 香港VPS[主机]
- 中久数据:2021新年特惠便宜挂机宝5元/月,香港1G仅20元/月,美国 美国VPS[主机]
- 魔方云:2021春节促销_香港CN2/美国CN2全年最高折扣75折/香港 美国VPS[主机]
- UCloud快杰型海外云主机:可选香港/上海/北京/日本/新加坡/韩国/ 日本VPS[主机]
- 易梦云:香港/美国vps优惠活动,2核1G3M云主机仅16元/月 美国VPS[主机]
- 易探云:香港bgp服务器1核1G5M带宽/10G数据盘,首月19元 香港VPS[主机]
- V5.NET:香港/美国云服务器7折月付42港元起;香港物理服务器月付3 美国VPS[主机]
- 文物、沉船、村落!干旱导致大量遗迹重见天日 2022年干旱还要持续多久? 全球[VPS测评]
- 中行升级服务力促跨境电商发展 全球[VPS测评]
- 播放量破千万,点赞量破百万,你绝对想不到这三款多功能产品居然在TK上卖爆 全球[VPS测评]
- 中概股扫描网传拼多多正筹备跨境电商平台首站登陆美国 美国VPS[主机]
- tudcloud,香港沙田大带宽vps特惠7折,CN2+BGP直连,10 香港VPS[主机]
- 奇维云优惠活动:香港CN21核1G1M带宽云服务器免费领取 香港VPS[主机]
- 野草云服务器怎么样?香港CN2+BGP带宽30M月付19元 香港VPS[主机]
- 搬瓦工:美国香港cn2 gia线路VPS,10Gbps带宽,移动+联通企 美国VPS[主机]
转载请注明原文地址: http://140.238.13.167:12355/read-214631.html











