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
- 线路渣-StockServers→$3 月 2 核 2G 1G 宽带 5美国VPS[主机]
- $3.5 月 1G 内存 50G 硬盘 1T 流量 1Gbps KVM 全球[VPS测评]
- 樊云→40 元 月 2GB 内存 30GB SSD 空间 不限流量 10虚拟空间(主机)
- HostXen→日本 2G 套餐月付 70 元 美国 2G 套餐月付 6日本VPS[主机]
- 黑色星期五 sharktech→10G 不限量国内直连服务器超低价全球[VPS测评]
- CN2 GIA ¥249 年 512M 内存 20G SSD 1Gbps全球[VPS测评]
- 阿里云 9 月优惠→云服务器 2 折起购 香港 1G 内存套餐两年付 5香港VPS[主机]
- php 获取当前时间戳全球[VPS测评]
- CloudCone→$17.5 年 KVM-1GB 20GB 1TB 洛全球[VPS测评]
- 腾讯云服务器采购季活动被忽略的亮点,1 核 1G 内存 3M 带宽香港云香港VPS[主机]
- 更新WordPress网站PHP7.2新版本的注意事项全球[VPS测评]
- WordPress后台reCaptcha验证登录保护插件:reCaptC全球[VPS测评]
- 比TinyPNG好用的Squoosh在线图片压缩全球[VPS测评]
- 搬瓦工双十一 全场11%优惠码 BWH20201111 循环折扣终身八九全球[VPS测评]
- CAD清理注册表/CAD重装失败怎么办?AUTO Uninstaller全球[VPS测评]
- 使用 HE Tunnel Broker 的 IPv6 隧道免费给 I全球[VPS测评]
- 百度智能云云服务器BCC测评硬盘IO、速度、延迟、丢包率和路由测试全球[VPS测评]
- HKServerSolution 七夕优惠汇总 香港 HKBN 8 折 香港VPS[主机]
- 众创逸云:国庆中秋节大促销优惠,不限流量香港CN2线路VPS 5.9元/香港VPS[主机]
- 景林网络:香港CN2vps,1核1G3M月付35元;美国高防vps,48美国VPS[主机]
- 跨境电商洋码头生死攸关:买手流失资金被冻结保全全球[VPS测评]
- 虾皮跨境电商怎么样?虾皮跨境电商靠谱吗?全球[VPS测评]
- 什么是云计算?全球[VPS测评]
- 快速云:科普云服务器服务器的概念和作用2022-08-2313:26来源全球[VPS测评]
- 硅云服务器拼团活动:香港云服务器1核1G,拼团价仅98元/年香港VPS[主机]
- TmhHost:美国洛杉矶CN2 GIA高防vps/日本/香港BGP等K日本VPS[主机]
- 私有云架设之优选拓普龙S465-24高密度存储服务器机箱评测解析全球[VPS测评]
- 六一云:香港CN2/洛杉矶高防85折优惠,CDN半年优惠,充值返利送水杯香港VPS[主机]
- RAKsmart:E3服务器秒杀$30/月起,韩国服务器,香港/日本/美站群服务器[IP]
- 56云服务器怎么样?56云服务器价格和优势是什么?全球[VPS测评]
转载请注明原文地址:http://140.238.13.167:12355/read-217623.html











