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
- Mineserver:香港CMI大带宽云服务器38元/月起(300M不限 香港VPS[主机]
- 特网云:高防香港云主机/CN2/真实防御/30G高防御/金盾防护无视CC 香港VPS[主机]
- 恒创科技:海外全球大促,香港美国服务器,全场低至3折限量首购 美国VPS[主机]
- 欧路云:美国特价云服务器/50M带宽8元/月起;香港云服务器月付15元起 美国VPS[主机]
- 自适应bootstrap左侧导航可关闭 全球[VPS测评]
- 什么是IconFont ?iconfont 的使用和解析 全球[VPS测评]
- Linux系统下安装Java JDK 全球[VPS测评]
- 如何更改WHMCS默认后台路径让WHMCS系统更安全 全球[VPS测评]
- Linux chown -R 指令介绍与使用 全球[VPS测评]
- JGKVM促销,美国cn2vps服务器价格再创新低,平均6.5元/月起 美国VPS[主机]
- 印象云怎么样?85折香港安畅CN2vps带宽3M月付23元 香港VPS[主机]
- 印象云vps,香港安畅gia低至22元起,1核1G内存,小带宽无限流量, 香港VPS[主机]
- 手帕云,便宜小带宽香港CN2 VPS,集群10G防御,双向CN2直连,仅 香港VPS[主机]
- 恒创科技:国庆中秋双节同庆,香港1核2G云服务器,年付378元 香港VPS[主机]
- RAKsmart:1核1GB/40GB空间/不限流量/15Mbps/KV 虚拟空间(主机)
- 百家云服务器怎么样?免备案香港云服务器、即开即用 香港VPS[主机]
- 初忆云:香港美国cn2云服务器30元;香港cn2高防vps30元,美国1 美国VPS[主机]
- 汇速云:香港沙田CN2云主机,2核CPU带宽1M月付6元,2核2M带宽月 香港VPS[主机]
- 孤狼云:香港CN2/美国CN2 VPS首月特价¥6.8元;香港虚拟主机仅 美国VPS[主机]
- 硅云香港云服务器促销活动:CPU性能100%性能,无需备案 香港VPS[主机]
- 95IDC :香港vps优惠活动,1核2G月付折后25元;香港物理机低至 香港VPS[主机]
- 老六云:⾹港⼤宽带服务器、香港大带宽vps_新购⾸⽉85折优惠 香港VPS[主机]
- 百星数据:全场云服务器7折优惠,美国cn2/香港cn2/韩国cn2/日本 日本VPS[主机]
- 悠久云:香港云服务器低至9.9元/月;香港大带宽vps,4核4G30M仅 香港VPS[主机]
- 衡天云:香港/美国物理服务器499元/月_香港/美国云服务器_超低特价 美国VPS[主机]
- pigyun:美国VPSCUVIP-AS9929仅16元起,美国原生IP 韩国VPS[主机]
- 易梦云:香港/美国vps优惠活动,2核1G3M云主机仅16元/月 美国VPS[主机]
- Centos7的firewall 防火墙如何设置端口转发? 全球[VPS测评]
- Linux系统下安装Java JDK 全球[VPS测评]
- CubeCloud,香港cn2,美国cn2 gia线路vps主机,2核1 美国VPS[主机]
转载请注明原文地址: http://140.238.13.167:12355/read-220635.html











