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
- MySQL 修改字段类型或长度全球[VPS测评]
- 疯狂猜成语 图猜成语一个人拿着棍子后面有一群人是什么成语?全球[VPS测评]
- Servarica→$7 月 XEN-1G 内存 1.5T 硬盘 100全球[VPS测评]
- 域名价格监控及注册查询网站汇总全球[VPS测评]
- 新加坡绕-WINNERvps→$3.99 月 1GB 内存 25GB S虚拟空间(主机)
- 傲游主机→64 元 月 KVM-2GB 40GB 400GB 香港(CN香港VPS[主机]
- TeraDelta→美东费城虚拟主机 5GB SSD 空间 不限流量 年虚拟空间(主机)
- 看奈飞 99 元 月 1G 内存 10G 硬盘 1T 流量 300Mbp全球[VPS测评]
- ThinkPHP5 输出最后执行的 sql 语句全球[VPS测评]
- Sharktech→$29.6 年 1GB 内存 20GB SSD 空间虚拟空间(主机)
- Uovz→2000 元 月 2*E5-2450L 16GB 内存 1TB日本VPS[主机]
- 四折优惠 $0.8 月 256M 内存 15G 硬盘 256G 流量 1全球[VPS测评]
- 便宜 ¥119.8 年 1 核 512M 内存 10G SSD 1000全球[VPS测评]
- 促销 极光 KVM 月中优惠→东京 CN2 线路 1 核 1G 套餐月付全球[VPS测评]
- 如何阻止Windows Update捆绑更新设备驱动WINDOWS
- Nossting → 3.4$ 月 土耳其 2C3G60G硬盘 1Gbp全球[VPS测评]
- 2019黑五: ZeptoVM 欧洲CN2 → 1C 512M RAM 全球[VPS测评]
- 吸血鬼崛起服务器怎么快速搭建?全球[VPS测评]
- 无忧云,香港VPS全线5折促销,四川高防服务器/大连BGP/德阳高防/深香港VPS[主机]
- kvmla,新加坡VPS、香港VPS、日本VPS,2核2G云主机仅425日本VPS[主机]
- 咖啡主机:美国高防vps、香港vps 可享受6-7折促销 每月仅需14元美国VPS[主机]
- 麻花云双12年终盛典:香港云主机9元起,老客户续费6折;安徽BGP线路v香港VPS[主机]
- 常见的CN2,GIA,CIA 各个网络线路解释全球[VPS测评]
- 厘米云,江苏移动100G高防云服务器,4核4G内存20M带宽79元/月全球[VPS测评]
- 六六云:美国cera机房CN2 GIA高防/香港CN2 GIA建站/CM美国VPS[主机]
- 老六云怎么样?香港cn2vps月付23元香港NTT月付9.9元香港VPS[主机]
- TmhHost:1核/1G/40G SSD/3Mbps/香港三网CN2 香港VPS[主机]
- 六六云:美国cera机房CN2 GIA高防/香港CN2 GIA建站/CM美国VPS[主机]
- BBR对比BBR2BBRPLUSBBR2CAKE锐速哪个效果好全球[VPS测评]
- 牛信云入选艾瑞《2022年中国跨境电商服务行业趋势报告》典型厂商全球[VPS测评]
转载请注明原文地址:http://140.238.13.167:12355/read-220635.html











