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
- RAKsmart 五月促销,美国服务器$46 月起,大陆优化 CN2 线站群服务器[IP]
- 标准互联→襄阳电信裸金属服务器 8 核 16G 内存 290GB SSD全球[VPS测评]
- W3Space→$15 年 1GB 内存 25GB SSD 空间 1.5虚拟空间(主机)
- LOCVPS→日本大阪 65 折 XEN2G 套餐月付 44.2 元 充日本VPS[主机]
- 便宜 $2 月 512M 内存 20G SSD 1Gbps 不限流量 K全球[VPS测评]
- 疯狂猜成语 图猜成语一个人左边是一个天使右边是一个恶魔是什么成语?全球[VPS测评]
- 二三互联香港新世界 vps 促销,5-8 折优惠,CN2 小带宽,不限流香港VPS[主机]
- WPS 表格提取身份证号中的出生年月日并且自动计算年龄的方法全球[VPS测评]
- CloudCone→$15 年 KVM-512MB 10GB 1TB 洛全球[VPS测评]
- 解决Debian系统apt-get更新官方源失败 替换163源的方法全球[VPS测评]
- V5.NET 香港阿里云CN2专线 香港日本独服 4g内存240gssd日本VPS[主机]
- WebHorizon 波兰机房便宜vps,基于kvm,0.5G内存,£1全球[VPS测评]
- HostNamaste印度商家:美国/法国便宜KVM VPS 1核1G/美国VPS[主机]
- Ishosting inferno 香港 无限流量VPS测试(M24香港VPS[主机]
- ZeptoVM 512M内存 10G SSD 320G流量 1G带宽 伯全球[VPS测评]
- Wikihost(idc.wiki) → 日本CN2 去程163回程C日本VPS[主机]
- 数据中心助力光纤光缆行业发展全球[VPS测评]
- 6种样式社交论坛网站HTML5模板 - Social全球[VPS测评]
- 常见的CN2,GIA,CIA 各个网络线路解释全球[VPS测评]
- 瓜云互联怎么样?香港/美国洛杉矶CN2高防vps 50G月付34元美国VPS[主机]
- 亚当云怎么样?香港安畅cn2vps带宽10M月付28元香港VPS[主机]
- 傲闪云vps,1核1G低至10元起,国内高防/香港CN2/美国CN2/美美国VPS[主机]
- 云服务器都是有哪些特点?全球[VPS测评]
- MoonVM,香港HKT家宽VPS服务器,动态IP一键切换,1G带宽任意香港VPS[主机]
- LOCVPS:全球云美国洛杉矶CN2 VPS,香港云地Xen VPS七折美国VPS[主机]
- 品光网络:香港美国免备案云服务器超低价,2核2G2M年付仅750元/年美国VPS[主机]
- 一个纯CSS ToolTip提示工具组件,鼠标悬停的时候会出现气泡文字提全球[VPS测评]
- 亚当云:香港CN2云服务器26元/起_全场低至85折起/续费同价香港VPS[主机]
- ucloud云主机怎么样?ucloud云服务器1核2G仅52元/年起;香香港VPS[主机]
- 天上云:香港CN2弹性云服务器;1核/1G/20G/;促销月付24元起(香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-205775.html











