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
- 死海网络→CN2 GIA 线路高速 VPS 美国原生 IP 可看奈飞 提美国VPS[主机]
- hkserversolution→51 优惠 美国大带宽 cn2 gia美国VPS[主机]
- Thinkphp5 屏蔽 php 中的 warning 错误提示全球[VPS测评]
- 大盘机 $6.95 月 1G 内存 1T 硬盘 5T 流量 1 个 IP全球[VPS测评]
- KVMLA 新加坡 CN2 线路服务器上线 75 折 VPS 八折 充 全球[VPS测评]
- NecoVM→199 元 年 128MB 内存 4GB 空间 500GB虚拟空间(主机)
- 便宜 ¥15 月 1 核 256M 内存 11G 硬盘 150G 流量 香港VPS[主机]
- VirMach→洛杉矶 西雅图 芝加哥 纽约等 KVM VPS 256M全球[VPS测评]
- 便宜 VPS $1.49 月 2CPU 512M 内存 10G SSD 全球[VPS测评]
- 便宜 Dogyun→香港机房 CN2 线路 2G 内存 20G SSD 香港VPS[主机]
- 老鹰主机再见夏季促销→虚拟主机低到 3 折 云服务器年付 7 折优惠全球[VPS测评]
- 便宜的免备案虚拟主机-Hostens 虚拟主机月付低至 0.99 美元全球[VPS测评]
- centOS7系统下如何修改服务器SSH端口默认的22。全球[VPS测评]
- WordPress站内链接修改插件:Velvet Blues Updat全球[VPS测评]
- 24fire → 2€ 月 1C1G10G硬盘 1G端口不限流量 德国 全球[VPS测评]
- 磐逸云:全场8折优惠,1核1G香港套餐月付24元,日本NNT线路套餐,月日本VPS[主机]
- HTML5绿色有机食品电商网站模板 - Organtio全球[VPS测评]
- 云计算拯救互联网巨头:谁抢跑,谁追赶?全球[VPS测评]
- 源云主机,香港沙田VPS CN2 1核1G 24/月 NTT 50M 9香港VPS[主机]
- 者也IO,香港日本美国云服务器促销,2核2G1000元/2年,7折+内存日本VPS[主机]
- 麻花云怎么样?安徽移动vps月付29元/香港vps月付21元香港VPS[主机]
- cmivps:香港vps终身7折,香港服务器终身9折,移动访问尤其优秀香港VPS[主机]
- 游戏服务器是什么意思?游戏服务器可以用云服务器吗?全球[VPS测评]
- 冀青云怎么样?香港CN2-GIA vps月28元/香港NTT 月9元香港VPS[主机]
- 野草云怎么样?香港、美国圣何塞高防VPS 1核1G3M 月付19元美国VPS[主机]
- BBR对比BBR2BBRPLUSBBR2CAKE锐速哪个效果好全球[VPS测评]
- 触摸云:香港GT ④区适合建站vps,折后最低套餐29.75/月香港VPS[主机]
- 情画数据:香港沙田CN2云服务器,2核/2G/5M带宽,年付200元香港VPS[主机]
- 跨境电商洋码头生死攸关:买手流失资金被冻结保全全球[VPS测评]
- 易探云:香港云服务器2核2G5M首月39元,带1G防御,年付6.8折优惠香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-205775.html











