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
- 蓝米云→21 元 月 1GB 内存 40GB SSD 空间 500GB 虚拟空间(主机)
- Xhostserver→499 元 月 E3-1230v2 16GB 2全球[VPS测评]
- 补货 CloudCone→512M 内存套餐 20 美元每年的套餐又补货全球[VPS测评]
- LunaNode→加拿大 法国按小时计费 KVM 月费 3.5 美元起全球[VPS测评]
- Virtono - 春季特价促销 VPS 最低二八折年付€9.95全球[VPS测评]
- 补货 CloudCone→年付$15 1 核 512M 内存 10G 硬全球[VPS测评]
- hostsolutions→充值多送 50%额度 无视 DMCA 投诉的独立服务器[U]
- XEN 容易超售-HostXen 预热双十一→充 300 送 100 续全球[VPS测评]
- VPS 推荐 vultr 日本线路日本VPS[主机]
- ArvanCloud→€6.5 月 1GB 内存 25GB SSD 空间虚拟空间(主机)
- 锐速 BBR 魔改 BBR 效果对比 锐速和 BBR 选择哪个好全球[VPS测评]
- ginernet→€5.99 月 西班牙 10Gbps 带宽 VPS 无全球[VPS测评]
- 热网互联→美国 日本 香港 CN2 GIA 线路 注册领 40 元 香港日本VPS[主机]
- 大流量 €6.99 月 1G 内存 40G 硬盘 4T 流量 1IP 1全球[VPS测评]
- 初春优惠 糖果主机→香港云服务器¥27.75 月起 虚拟主机新购最低可享香港VPS[主机]
- 如何在百度搜索结果中显示你的WordPress站点Logo全球[VPS测评]
- 今日头条欲做号内搜索,可搜站外不少内容全球[VPS测评]
- Hostodo拉斯维加斯机房VPS促销送DirectAdmin授权,硬盘全球[VPS测评]
- 结合预算和需求来选择香港服务器和美国服务器美国VPS[主机]
- 上新优惠: UOVZ 莞港IPLC 1G 内存 20G SSD 100G全球[VPS测评]
- 数据中心助力光纤光缆行业发展全球[VPS测评]
- 数据中心助力光纤光缆行业发展全球[VPS测评]
- 桔子数据怎么样?美国、香港CN2云服务器2核2G10M带宽仅480元/年美国VPS[主机]
- 华为云香港企业级云服务器优惠:4核8G5M香港云服务器低至4764元/年香港VPS[主机]
- 云计算竞争日趋白热化云计算行业市场前景分析全球[VPS测评]
- UCloud优刻得云服务器建站教程:香港云服务器购买优惠方法香港VPS[主机]
- 千古互动:香港vps,1核/1G/20M,月付35元起;挂机宝5元/月起香港VPS[主机]
- RFCHost:1核1G洛杉矶vps/15GB空间/1TB流量/KVM/虚拟空间(主机)
- 曾经最大跨境电商平台团队收缩,只剩20人丨GoingGlobal周报全球[VPS测评]
- 阿里云香港服务器价格优惠:老用户香港2核4G3M云服务器仅1181.5元香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-217623.html











