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
- [黑五]EtherNetservers 常规套餐 75 折 特价套餐年付全球[VPS测评]
- 价格一般-魔方云→62 元 月 1GB 内存 15GB SSD 空间 4虚拟空间(主机)
- 疯狂猜成语 图猜成语1+1=王是什么成语?全球[VPS测评]
- Vultr→新用户最高送 103 美元 全球 16 大机房 按小时计费 日本VPS[主机]
- Eazy.gr→可注册.gr .com.gr .edu.gr 等域名 年全球[VPS测评]
- 桔子数据→28 元 月 1GB 内存 40GB SSD 空间 500GB虚拟空间(主机)
- Domeny.pl→.pl .com.pl .edu.pl .site 全球[VPS测评]
- 疯狂猜成语 图猜成语五个人头上都有圆圈站在一起 旁边还有一个人头上也有圆全球[VPS测评]
- 真不咋地-CrownCloud→$7 月 4 核 4GB 内存 60GB虚拟空间(主机)
- 便宜 ¥119.8 年 1 核 512M 内存 10G SSD 1000全球[VPS测评]
- spinservers→$99 月 2*e5-2630L v2 64g 美国VPS[主机]
- 如何通过正确整理旧文章来增加网站SEO优化全球[VPS测评]
- SoftShellWeb 黑五促销 中国台湾大陆优化线路 1g内存 带5全球[VPS测评]
- WordPress搜索插件:Ivory Search全球[VPS测评]
- 轻云互联,香港VPS月付22元 美国VPS月付19元美国VPS[主机]
- 实例 GIA0910629396 重启失败,原因:sudo: unabl全球[VPS测评]
- Linux系统下安装Java JDK全球[VPS测评]
- 2022年有哪些顶级托管服务提供商全球[VPS测评]
- 七安云怎么样?香港CN2+BGP线路VPS 1核1G10M 16/月香港VPS[主机]
- kvmla,新加坡VPS、香港VPS、日本VPS,2核2G云主机仅425日本VPS[主机]
- 一篇漫画,看懂云计算全球[VPS测评]
- 亚当云怎么样?香港安畅cn2vps带宽10M月付28元香港VPS[主机]
- 什么是云计算?全球[VPS测评]
- 傲游主机:香港cn2高防云主机,159元/月/2核/2GB/40GB/2香港VPS[主机]
- 文物、沉船、村落!干旱导致大量遗迹重见天日 2022年干旱还要持续多久?全球[VPS测评]
- anyhk香港HKT商宽NAT VPS,1Gbps无限流量,终身8折¥3香港VPS[主机]
- 亿恩科技香港云服务器 2核2G服务器特价700元/年香港VPS[主机]
- UCloud快杰型海外云主机:可选香港/上海/北京/日本/新加坡/韩国/日本VPS[主机]
- 云计算的大门依旧没向雷军敞开全球[VPS测评]
- kvmcloud怎么样?香港BGP云服务器 月付12.9元香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-202865.html











