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
- 极光KVM:4月G口新品发布美国大带宽vps;香港cn2+BGP直连云服 美国VPS[主机]
- 八成企业看好出海投资 防止“溺水”需重视这一点 全球[VPS测评]
- 40火伞高压电爬坡更让企业用电于民 全球[VPS测评]
- BBR对比BBR2BBRPLUSBBR2CAKE锐速哪个效果好 全球[VPS测评]
- 新华全媒+丨第六届全球跨境电商大会:跨境电商加快拓宽中国贸易新通道 全球[VPS测评]
- wikihost,微基主机服务,100M带宽香港tudcloud,5G防 香港VPS[主机]
- sugarhosts首推DECADE云服务器,香港云服务器,1核512M 香港VPS[主机]
- 桔子数据怎么样?便宜香港安畅CN2 VPS1核1G10M 28/月 香港VPS[主机]
- 印象云怎么样?85折香港安畅CN2vps带宽3M月付23元 香港VPS[主机]
- CMCUCT:2核2GB/20GB香港ntt线路vps 6TB流量/1G 香港VPS[主机]
- earidc怎么样?香港三网cn2vps带宽1M月付29元 香港VPS[主机]
- HostKVM香港云地VPS,2核4G内存折后$8.4/月,30M大带宽 香港VPS[主机]
- 众创逸云:1核2G云服务器,不限流量vps/5Mbps/KVM/香港CN 香港VPS[主机]
- locvps:香港cn2vps(葵湾机房),2核2G40g硬盘,8折优惠 WINDOWS
- 桔子数据:1核1GB香港CN2 GIA vps促销28元/月,最高30M 香港VPS[主机]
- 六六云,2核2G香港云服务器/1000GB流量/50Mbps端口/KVM 香港VPS[主机]
- tudcloud:香港vps/2核/2GB/20GB空间/不限流量/KV 虚拟空间(主机)
- 阳帝云:香港大宽带vps仅15元/月;美国vps200G防御仅28元/月 美国VPS[主机]
- 后浪云:优惠码”2021″,美国/香港2核4G2M云服务器仅32.5元/ 美国VPS[主机]
- 野草云:香港CN2VPS年付36折,137元/年;独立服务器350元月起 独立服务器[U]
- UCloud:香港云服务器,1核1G内存89元/年起(可选购3年/CN2 香港VPS[主机]
- ucloud香港服务器,香港云服务器vps主机优惠价格 香港VPS[主机]
- 易探云:独立IP香港云服务器租用,香港vps海外云主机BGP线路/CN2 独立服务器[U]
- 什么是分布式拒绝服务攻击,其原理及防范措施 全球[VPS测评]
- 极光KVM:美国联通CUVIP线路套餐立减50元,香港30M大带宽年付1 美国VPS[主机]
- wordpress 上传附件报错 “抱歉,出于安全的考虑,不支持此文件类 全球[VPS测评]
- 什么是VPS?国内VPS有哪些? 全球[VPS测评]
- wikihost,微基主机服务,100M带宽香港tudcloud,5G防 香港VPS[主机]
- 磐逸云怎么样?1核1G香港安畅CN2 VPS带宽5M年付128元 香港VPS[主机]
- 麻花云怎么样?安徽移动vps月付29元/香港vps月付21元 香港VPS[主机]
转载请注明原文地址: http://140.238.13.167:12355/read-202865.html











