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
- SoYouStart - Summer Deals 独立服务器夏季促销独立服务器[U]
- 优惠 桔子 VPS→1 核 1GB 内存 50M 带宽 五折优惠 月付¥全球[VPS测评]
- Virmach 黑五闪购套餐更新,超优惠 请不停刷新本页面全球[VPS测评]
- MySQL 获取当前时间及时间戳的方法全球[VPS测评]
- 大盘机 $6.95 月 1G 内存 1T 硬盘 5T 流量 1 个 IP全球[VPS测评]
- raksmart→“开年大促” 超大带宽 不限流量“独立服务器” 美国 独立服务器[U]
- Sharktech→10G 无限流量服务器 55 折 三机房可选 支持支全球[VPS测评]
- 四个倒着的脚字是什么成语?全球[VPS测评]
- 企鹅小屋→180 元 年 512MB 内存 400GB 空间 1TB 流虚拟空间(主机)
- 新加坡 VPS $3 月付 1G 内存 10G SSD 100M 不限流全球[VPS测评]
- RangCloud 推出山东BGP NAT VPS全球[VPS测评]
- WebHorizon新加坡NAT VPS基于ipv6搭建节点反代解锁奈飞全球[VPS测评]
- DediPath | Independence Day Sale 五折优独立服务器[U]
- SEO和网页加载速度有关系吗?如何提升?全球[VPS测评]
- 7月份,经济复苏边际趋缓,央行降息幅度先稳后升全球[VPS测评]
- 性价比最高的VPS网络评测全球[VPS测评]
- HostHatch:1核/2GB/20GB空间/KVM/香港/洛杉矶/纽虚拟空间(主机)
- 跨境电商有哪些平台?十大跨境电商平台!全球[VPS测评]
- 跨境电商拔节生长关键期,宁波政协委员月谈会精准助力!全球[VPS测评]
- 腾讯云香港服务器优惠价格:4核8G5M新用户1508元,老用户2639元香港VPS[主机]
- sugarhosts首推DECADE云服务器,香港云服务器,1核512M香港VPS[主机]
- 搬瓦工:全场VPS 9折优惠,10Gbps(美国cn2 gia+日本软银日本VPS[主机]
- 初忆云: 滁州电信、湖北电信云服务器价格低至10元起,续费同价全球[VPS测评]
- 易科云:香港cn2 vps,洛杉矶 cn2云主机;全场月付9折/季付8折香港VPS[主机]
- 中久数据:2021新年特惠便宜挂机宝5元/月,香港1G仅20元/月,美国美国VPS[主机]
- 六六云:美国cera机房CN2 GIA高防/香港CN2 GIA建站/CM美国VPS[主机]
- 雪花云怎么样?雪花云香港云服务器1核1G2M仅19元/月香港VPS[主机]
- 潮科技|阿里云发布第七代高主频云服务器ECS和含光800云服务器全球[VPS测评]
- 欧亚云:新春优惠,洛杉矶CERA云主机仅25元/月起,香港CN2仅28元香港VPS[主机]
- 美国信贷市场的痛苦将不亚于上世纪70年代的通胀时期美国VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-208707.html











