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
- 疯狂猜成语 图猜成语一个人在唱歌一个人在哭泣是什么成语?全球[VPS测评]
- 速度抢购 搬瓦工 37.79 美元套餐限量补货 双向 CN2 GIA 线全球[VPS测评]
- 圣诞闪购 RackNerd→3 核 2G 内存 30G SSD 5T 流全球[VPS测评]
- 八月优惠 LOCVPS→香港大埔 cn2 VPS 补货 VPS 全场 8香港VPS[主机]
- 免费域名申请及使用 后缀 tk ml cf ga gq全球[VPS测评]
- 跑 BT 不错-Ikoula→€9.99 月 Celeron®G3900全球[VPS测评]
- php 使用 md5 函数生成 16 位 md5 值返回乱码的原因以及解全球[VPS测评]
- 补货通知 OLVPS→俄罗斯伯力机房 VPS 补货 终身 35 折优惠全球[VPS测评]
- Hosteons→KVM 八折 OpenVZ 年付五折 不限流量 免费升全球[VPS测评]
- DediPath→$175 月 E3-1270v2 16GB 内存 2T全球[VPS测评]
- Dedicated→$35 月 E3-1270 8GB 内存 1TB 硬全球[VPS测评]
- windows vpsWINDOWS
- pacificrack→洛杉矶 KVM 系列 VPS 低至$6.52 年全球[VPS测评]
- 搬瓦工VPS主机IP被封原因及应对方法全球[VPS测评]
- RackNerd圣诞节1.5g内存20g硬盘2.5T大流量年付仅16.8全球[VPS测评]
- YYYHost洛杉矶高防CN2GIA1G内存8m带宽外加100g防御 月全球[VPS测评]
- 购买香港服务器需要做哪些准备香港VPS[主机]
- 八成企业看好出海投资 防止“溺水”需重视这一点全球[VPS测评]
- 云计算的三种服务模式,SaaS和PaaS的区别在哪里?全球[VPS测评]
- 海量科技:香港2核2G云服务器,5M独享带宽,低至168元/月香港VPS[主机]
- 跨境贸易和跨境电商的三大区别简单分析全球[VPS测评]
- 云计算竞争日趋白热化云计算行业市场前景分析全球[VPS测评]
- 辰迅云怎么样?国内香港云服务器全场5折,物理服务器限时特价香港VPS[主机]
- 原创解析:云计算含义及定义全球[VPS测评]
- 什么是IconFont ?iconfont 的使用和解析全球[VPS测评]
- 个人如何做跨境电商?全球[VPS测评]
- tudcloud:香港不限流量VPS,2核2G仅218元/年,支持WinWINDOWS
- 云米科技:美国CERA三网回程GIA 10Mbps大带宽,年付最低400美国VPS[主机]
- 阿里云和腾讯云免备案香港云服务器哪个更优惠?香港VPS[主机]
- 舍利云:香港美国vps云服务器/BGP线路元旦全场9折,低至36元/月美国VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-220635.html











