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
- hostworld→£1.99 月 10GB SSD 空间 不限流量 D虚拟空间(主机)
- thinkphp5 中文乱码解决方案全球[VPS测评]
- hostkvm 香港湾仔机房补货 2G 内存 30G 硬盘每月 68 元香港VPS[主机]
- 数脉科技→香港独立服务器低至 360 元 月 E3-1230v2 16g独立服务器[U]
- Sharktech→$99 月 E3-1270v2 16GB 内存 2T全球[VPS测评]
- Windows 系统 远程桌面 漏洞公告 及如何安装安全补丁WINDOWS
- 疯狂猜成语 图猜成语屋子里有一只手在勾引狼是什么成语?全球[VPS测评]
- Dmit – 2 核 1G 内存 20G SSD 1200G 流量 20香港VPS[主机]
- racknerd→母亲节 VPS 优惠活动 $20.88 年 KVM 1全球[VPS测评]
- 青云互联 香港安畅 CN2GIA VPS八折优惠 1G内存/单核/5Mb香港VPS[主机]
- ZJI上线香港华为云 E3 CPU 便宜服务器,下单立减300元,优惠后香港VPS[主机]
- Alpha bd → 18$ 月 孟加拉VPS Linux 2核2G30全球[VPS测评]
- DMIT.io → 2G 内存 20G SSD 无限流量 200M带宽 全球[VPS测评]
- 什么是Bootstrap?全球[VPS测评]
- 2022年黄河流域跨境电商博览会将于8月26日至28日在青岛西海岸新区举全球[VPS测评]
- 极客主机,美国高防/日本软银/新加坡双程CN2/香港VPS 折后39元/日本VPS[主机]
- MoonVM,香港HKT家宽VPS服务器,动态IP一键切换,1G带宽任意香港VPS[主机]
- 2.45亿、成都公安(大数据基座-服务器)采购:神码、虹信、海康、超聚变全球[VPS测评]
- 六一云:香港CN2/洛杉矶高防85折优惠,CDN半年优惠,充值返利送水杯香港VPS[主机]
- 咖啡主机:元旦活动全场7、8折,洛杉矶vps仅需16元/月,香港将军澳C香港VPS[主机]
- HKserver:香港GIA VPS,1核/512MB/10GB空间/1虚拟空间(主机)
- RAKsmart:1核1GB/40GB空间/不限流量/15Mbps/KV虚拟空间(主机)
- CombCloud,香港沙田CN2VPS测评,2核1G52元/月,安畅C香港VPS[主机]
- 硅云:香港云服务器2核2G5M促销1999元/3年起,香港虚拟主机1G空虚拟空间(主机)
- 茶猫云:香港云服务器,支持windows,2M带宽,34元/月WINDOWS
- 快速云:云计算分类和云计算的特点全球[VPS测评]
- LOCVPS:香港vps八折优惠中,双十一预热,充值充600送150,六香港VPS[主机]
- 青云互联:香港弹性云CN2直连线路,全场8折,免费windows系统,月WINDOWS
- 六一云:香港CN2/洛杉矶高防85折优惠,CDN半年优惠,充值返利送水杯香港VPS[主机]
- 雪花云怎么样?雪花云香港云服务器1核1G2M仅19元/月香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-217623.html











