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
- 百度云→CDN 活动 500GB 流量包 年付 55 元;1TB 流量包全球[VPS测评]
- JQuery 判断 input 文本框的值是否为空全球[VPS测评]
- 国外 VPS – 支付宝 微信付款全球[VPS测评]
- Bootstrap 清除浮动全球[VPS测评]
- EtherNetservers→$12 年 OpenVZ-1GB 30G全球[VPS测评]
- 标准互联→新上特惠型美国裸金属服务器 8 核 32G 32ip 1G 带美国VPS[主机]
- 限时抢购 $35 年 2 核 CPU 3G 内存 40G 硬盘 5T 流全球[VPS测评]
- 扫米工具→未注册域名批量查询全球[VPS测评]
- Chrome 浏览器开发者工具中引入网络 js 文件进行测试的方法全球[VPS测评]
- 便宜 $7.2 年 512M 内存 10G 硬盘 1T 流量 100Mb全球[VPS测评]
- ZetServers→€119 月 E3-1220v3 8GB 内存 2全球[VPS测评]
- CloudCone2021开年限时抢购 高IOPS/纯SSD硬盘/int全球[VPS测评]
- Pacificrack洛杉矶便宜VPS买两年送一年,叠加五折优惠码,三年全球[VPS测评]
- win10和office激活工具 HEU KMS Activator 集全球[VPS测评]
- Wikihost(idc.wiki) → 香港沙田Gen2 大宽带大流香港VPS[主机]
- LiteServers.nl 2022黑五 荷兰NVME Storage全球[VPS测评]
- 什么是Bootstrap?全球[VPS测评]
- 游戏服务器是什么意思?游戏服务器可以用云服务器吗?全球[VPS测评]
- 中概股扫描网传拼多多正筹备跨境电商平台首站登陆美国美国VPS[主机]
- 快速云:云计算分类和云计算的特点全球[VPS测评]
- 为什么大家都选择香港服务器建站香港VPS[主机]
- 什么是VPS?国内VPS有哪些?全球[VPS测评]
- 1分钟快速读懂云计算全球[VPS测评]
- 青果云怎么样?美国/香港/日本云主机 买多久送多久日本VPS[主机]
- 跨境电商周报丨拼多多布局跨境电商平台;Shopify将推出新平台支持网红全球[VPS测评]
- 很简单的图片不规则布局样式,纯css样式实现的图片瀑布流布局全球[VPS测评]
- 关键路径、资金情况、政策关键数据中心的可持续发展路径在哪里?全球[VPS测评]
- cloudpowerall:美国洛杉矶CN2 VPS,年付$9.99;香美国VPS[主机]
- 腾讯云美国硅谷服务器/香港/新加坡:1核1G(Linux)云服务器仅28美国VPS[主机]
- 游戏服务器是什么意思?游戏服务器可以用云服务器吗?全球[VPS测评]
转载请注明原文地址:http://140.238.13.167:12355/read-202865.html











