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
- XKCDN→亚洲 美国等节点 不限流量 月付 0.99 元;有国内节点美国VPS[主机]
- DedeCMS 后台模块管理空白不显示列表的解决办法全球[VPS测评]
- HTML 中正确设置表格 table 边框 border 的三种办法全球[VPS测评]
- Ramnode 充值送 25% KVM 月费 3 美元起 支持小时计费 全球[VPS测评]
- 存储型 VPS $2 月 512M 内存 11G NVMe+256G 空虚拟空间(主机)
- 网速一般-ShockHosting→$3.7 月 200G 硬盘 美国洛美国VPS[主机]
- $15 年 512M 内存 10G 硬盘 1T 流量 1Gbps KVM全球[VPS测评]
- JQuery 中 serialize 方法不提交隐藏元素全球[VPS测评]
- Raksmart 美国 4C 站群服务器买一送一,258 个 IP,圣何站群服务器[IP]
- CN2 GIA ¥249 年 512M 内存 20G SSD 1Gbps全球[VPS测评]
- 疯狂猜成语 图猜成语一只鹅和一只鸡在说话是什么成语?全球[VPS测评]
- 劳动节 edgeNAT→8 核 8G 40G SSD 4Mbps 不限流韩国VPS[主机]
- WordPress插件:Add-Document-Pages为你的网站增全球[VPS测评]
- 用Google Adsense自动广告来赚更多的钱全球[VPS测评]
- WordPress博客的百度快照怎么快速更新?全球[VPS测评]
- 大前端 DUX/D8/XIU WordPress 主题双11全场6折 最全球[VPS测评]
- Virmach → $1.5 月付 512M 内存 10G SSD硬盘 全球[VPS测评]
- 微基主机520常规优惠 虚拟主机2G三年付200元全球[VPS测评]
- 常见的CN2,GIA,CIA 各个网络线路解释全球[VPS测评]
- 极客主机,优惠8折充值返25%,香港CN2VPS,日本软银VPS,新加坡日本VPS[主机]
- 乐趣云怎么样?香港美国云服务器首月8.8元,新增IP5元/个美国VPS[主机]
- 邢志宏:加快推动高质量建设跨境电商综试区起势成势全球[VPS测评]
- 云计算拯救互联网巨头?全球[VPS测评]
- 原创解析:云计算含义及定义全球[VPS测评]
- PQhosting:香港HE/乌克兰/俄罗斯/荷兰/摩尔多瓦vps主机/香港VPS[主机]
- 咖啡主机:享受7、8折促销 美国vps仅需15.4元/月 香港vps仅需美国VPS[主机]
- VPSPlayer,超便宜香港cn2线路小带宽VPS,2核4G内存仅30香港VPS[主机]
- MoonVM,香港HKT家宽VPS服务器,动态IP一键切换,1G带宽任意香港VPS[主机]
- Linux系统下安装Java JDK全球[VPS测评]
- wikihost,微基主机服务,100M带宽香港CMIVPS,5G防御,香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-214631.html











