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
- DogYun 双十二 7 折 香港&日本&德国 VPS 按小时计费日本VPS[主机]
- spryservers→“Pi Day” $4 月 KVM 3G 内存 全球[VPS测评]
- 双十一 腾讯云新上秒杀活动→企业用户专享一折 每天 5 场秒杀活动 2G全球[VPS测评]
- DogYun 动态云全场 8 折 香港 CMI 机房 7 折 新开免设置香港VPS[主机]
- 疯狂猜成语 图猜成语一个人在唱歌一个人在哭泣是什么成语?全球[VPS测评]
- Pyclouds→深港 IPLC KVM VPS 40Mbps-200M全球[VPS测评]
- MySQL 数据库查询字符串长度大于 小于或等于指定值的记录全球[VPS测评]
- 腾讯云服务器采购季活动被忽略的亮点,1 核 1G 内存 3M 带宽香港云香港VPS[主机]
- #补货#Buyvm:AMD Ryzen平台 1核512M DDR4/10全球[VPS测评]
- 野草云香港CN2 VPS 云服务器年付三六折优惠,¥136.8起/年;独独立服务器[U]
- anyNode新年特惠 拉斯维加斯 VPS 2核/4G内存/60G硬盘 全球[VPS测评]
- 腾讯云新春采购节2019 - 每天五场2折秒杀/满减代金券/云产品三折全球[VPS测评]
- 盘点2021年国内外VPS云服务器可视化面板和一键安装脚本全球[VPS测评]
- 快云-台湾hinet nat vps 月付20元 2T流量@600Mbp全球[VPS测评]
- 无忧云怎么样?大连BGP德阳100G高防香港CN2VPS云服务器香港VPS[主机]
- 拼多多也要做跨境电商?出海之路能一帆风顺吗,了解一下全球[VPS测评]
- SugarHosts:虚拟主机三折起,VPS五折起,续费也可以享受优惠全球[VPS测评]
- 金斗云双12活动:全场8折起,美国圣何塞CN2 GIA高防VPS20元/美国VPS[主机]
- 香港vps免费:青云互联,1核1G/50G硬盘/500GB流量/2M带宽香港VPS[主机]
- 亚当云怎么样?香港安畅cn2vps带宽10M月付28元香港VPS[主机]
- 阿里云香港服务器价格优惠:老用户香港2核4G3M云服务器仅1181.5元香港VPS[主机]
- 6种样式社交论坛网站HTML5模板 - Social全球[VPS测评]
- 恒星云:香港沙田CN2VPS低至9.9元/起_国内BGP云服务器4核4G香港VPS[主机]
- 奇异互动:香港2核4G5M云服务器仅83元/月,700元/年;裸金属服务香港VPS[主机]
- 2022年最新的15个美容院抖音文案,美容院文案怎么写全球[VPS测评]
- 锤子云:香港CN2云服务器15元/月,三网CN2线路回程,建站vps专属香港VPS[主机]
- 孤狼云:2021春节优惠,香港安畅CN2、香港沙田CN2云服务器低至10香港VPS[主机]
- 者也vps年终特惠:全场云服务器7折终身优惠,赠1G内存,充1000送3全球[VPS测评]
- 云服务器和服务器各有什么优缺点?全球[VPS测评]
- tmhhost:香港安畅机房三网CN2 GIA VPS,月付45起;美国美国VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-211659.html











