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
- 易加速 CDN→洛杉矶 GIA 香港 CN2 节点 免费 DDOS CC香港VPS[主机]
- CN2 150 元 年 160M 内存 3G 硬盘 400G 流量@10全球[VPS测评]
- BlueVPS→$6.39 月 256MB 内存 10GB 空间 不限流虚拟空间(主机)
- 活动 腾讯云秒杀→1 核 2G 国内套餐年付 99 元 2 核 4G3M全球[VPS测评]
- iWFHosting→$57 月-E3 1230v2 16GB 2TB 全球[VPS测评]
- 疯狂猜成语 图猜成语一座房子前面是海(河)另一座房子前面是田地是什么成语全球[VPS测评]
- Windows and Office ISO Downloader 从微WINDOWS
- WordPress插件:WP China Yes解决主题插件无法更新的问全球[VPS测评]
- WordPress插件:Add-Document-Pages为你的网站增全球[VPS测评]
- WordPress插件:WP China Yes解决主题插件无法更新的问全球[VPS测评]
- Evoxt 上新 马来西亚VPS 测试(2.99$)全球[VPS测评]
- Virmach → $7年付 256M $13年付512M KVM机型 全球[VPS测评]
- 持续高温!长沙所有初中新生停止军训!全球[VPS测评]
- 如何根据业务需求选择合适的云服务器配置?全球[VPS测评]
- UCloud香港云主机怎么样?UCloud香港云主机1核1G1M评测香港VPS[主机]
- 6种样式社交论坛网站HTML5模板 - Social全球[VPS测评]
- 啤啤云怎么样?香港CN2线路vps,有香港安畅机房,全部9折,38.7元香港VPS[主机]
- 啤啤云怎么样?香港CN2线路vps,有香港安畅机房,全部9折,38.7元香港VPS[主机]
- BBR对比BBR2BBRPLUSBBR2CAKE锐速哪个效果好全球[VPS测评]
- ExCloud:1核256M存/8GB SSD空间/100GB流量/OV虚拟空间(主机)
- JGKVM活动钜惠:美国cn2,香港cmi,大带宽vps,三网直连,10美国VPS[主机]
- 乐趣云怎么样?香港美国云服务器首月8.8元,新增IP5元/个美国VPS[主机]
- 腾讯会议故障了 提示:会议发生异常请重新加入会议全球[VPS测评]
- Linux chown -R 指令介绍与使用全球[VPS测评]
- 六六云:1核1GB/15GB空间/1T流量/100Mbps/KVM/香港虚拟空间(主机)
- 福州职业技术学院获得世界职业院校技能大赛云计算赛项银牌全球[VPS测评]
- 轻云互联,香港VPS月付22元 美国VPS月付19元美国VPS[主机]
- 兼容移动和PC的loading加载和toast消息插件全球[VPS测评]
- 景林网络:香港CN2vps,1核1G3M月付35元;美国高防vps,48美国VPS[主机]
- 阿里云服务器一年价格多少钱?阿里云服务器报价全球[VPS测评]
转载请注明原文地址:http://140.238.13.167:12355/read-211659.html











