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
- 免费 KT 旗下 VPS 品牌 iON 免费送 14 天新加坡 VPS 全球[VPS测评]
- wishosting 法国 OVH 高防大硬盘 VPS 闪购活动,300全球[VPS测评]
- ¥26.98 月 256M 内存 4G 硬盘 100G@3Mbps KV韩国VPS[主机]
- iWFHosting→KVM2G 每月 8.99 美元起 洛杉矶可选 1全球[VPS测评]
- 微信支付接入报错→签名错误全球[VPS测评]
- 夏季优惠 Sharktech 促销活动→E3-1270v2 16G 2T全球[VPS测评]
- raksmart→美国(西海岸)独立服务器 优化线路 低至$46 月 高独立服务器[U]
- LOCVPS→57.6 元 月-2GB 40GB 3M 香港大埔 CN2香港VPS[主机]
- raksmart 日本 cn2+软银服务器带宽速度及综合性能测评,rak日本VPS[主机]
- 双十一 RAKsmart→注册就有机会抽到一年免费服务器 100M 不限全球[VPS测评]
- Scarlet Cloud→134.4 元 半年 256MB 内存 5G虚拟空间(主机)
- 用XAMPP搭建本地PHP测试环境及安装WordPress全球[VPS测评]
- 如何在WordPress博客实现历史上的今天的小功能全球[VPS测评]
- begin主题添加《英雄榜》每评论一次你就会自动排第一代码全球[VPS测评]
- 阿里云轻量级服务器低至144年付全球[VPS测评]
- 购买香港服务器需要做哪些准备香港VPS[主机]
- 吸血鬼崛起服务器怎么快速搭建?全球[VPS测评]
- 兼容移动和PC的loading加载和toast消息插件全球[VPS测评]
- JGKVM活动钜惠:美国cn2,香港cmi,大带宽vps,三网直连,10美国VPS[主机]
- centos7如何查看firewall防火墙状态:开启/关闭/重启防火墙全球[VPS测评]
- 桔子数据:1核1GB香港CN2 GIA vps促销28元/月,最高30M香港VPS[主机]
- 初忆云:湖北电信云服务器1核2G仅6.8/月,81.6元/年;香港cn2香港VPS[主机]
- VPS成立BurjeelHoldings全球[VPS测评]
- 触摸云:香港BGP/香港CN2/美国GIA高防CN2 GIA云服务器2H美国VPS[主机]
- 香港云服务器的优势有哪些方面香港VPS[主机]
- 8K超高清记录中国空间站凌月:天和核心舱与问天实验舱组成“土”字结构虚拟空间(主机)
- centos7如何查看firewall防火墙状态:开启/关闭/重启防火墙全球[VPS测评]
- YYYHost年终钜惠来袭:香港建站云/美国Cera云月付8折,季付7折美国VPS[主机]
- 麻花云双12活动:香港CN2云主机首月9元;安徽移动vps月付29元香港VPS[主机]
- HostKVM香港云地VPS,2核4G内存折后$8.4/月,30M大带宽香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-208707.html











