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
- servarica →$12 年 加拿大 VPS 原生 IP 1G 内存全球[VPS测评]
- 80VPS 韩国 LG 机房 KVM 上线 5 折优惠韩国VPS[主机]
- TheStack→$42.5 月-Dual L5630 24GB 2TB全球[VPS测评]
- mysql 替换字段指定内容的方法全球[VPS测评]
- 五月便宜 CloudCone→年付$12.95 1 核 256M 内存 全球[VPS测评]
- 优惠促销 T667→洛杉矶 伯力机房补货 提供各种机房优惠码 OLVPS全球[VPS测评]
- 疯狂猜成语 图猜成语李字箭头指着一个戴着写着张的帽子的人是什么成语?全球[VPS测评]
- vServer.site→€12 年 1GB 内存 5GB SSD 空间虚拟空间(主机)
- 恒创科技2020 跨年盛典!香港服务器全场低至 5 折 续费即赠额外时长香港VPS[主机]
- dedecms 判断当前页是否为首页全球[VPS测评]
- JQuery.validate 通过 class 属性批量设置 inpu全球[VPS测评]
- 傲游主机开学季全场 75 折 香港&韩国&美国 VPS 月付 52 元起日本VPS[主机]
- Centos7 yum 安装 Python3.6 环境全球[VPS测评]
- 促销 n3servers→E5-2620v2 服务器仅售$19.5 月 独立服务器[U]
- gcorelabs 伯力和海参崴机房 488m内存/8G硬盘/288G流全球[VPS测评]
- Hosting Ground → 10$ 月 4C1G50G硬盘 美国西美国VPS[主机]
- LetBox → 512M内存 11G NVMe储存+256G块储存 1全球[VPS测评]
- WHMCS 8.1如何简化注册页面全球[VPS测评]
- 空空云怎么样?香港CN2-GIA VPS,带宽2M,月付15元香港VPS[主机]
- 30个美容院抖音唯美文案,看到就直接拿去发吧全球[VPS测评]
- 人力资源管理系统前端Web模板 - SmartHR全球[VPS测评]
- CMCUCT:2核2GB/20GB香港ntt线路vps 6TB流量/1G香港VPS[主机]
- 青叶云怎么样?青叶云国内/国外弹性云服务器价格,海外vps低至25.6元全球[VPS测评]
- 金斗云怎么样?香港CN2月付23元,圣何塞CN2月付22元香港VPS[主机]
- 跨境电商平台首选香港服务器香港VPS[主机]
- SugarHosts糖果主机:虚拟主机5折(有独立IP);香港/美国cn独立服务器[U]
- 快云科技:香港CN2 GIA云服务器,全场7折终身优惠,带宽20M,20香港VPS[主机]
- 30个美容院抖音唯美文案,看到就直接拿去发吧全球[VPS测评]
- 青云互联:香港vps全场终身8折优惠,CN2线路,提供大带宽vps,月付香港VPS[主机]
- 六一云:香港CN2/洛杉矶高防/CDN,“返利+折扣”双优惠,折上折送实香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-214631.html











