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
- 活动 RAKsmart→机房升级 300G 防御 悬赏 100G 以上攻独立服务器[U]
- 企鹅小屋→350 元 月 E3-12XX 16GB 内存 1TB 硬盘 香港VPS[主机]
- 便宜 ¥119.8 年 1 核 512M 内存 10G SSD 1000全球[VPS测评]
- Could not reliably determine the ser全球[VPS测评]
- Moack.co.kr→29 美元韩国独立服务器每日限量抢独立服务器[U]
- 极客主机→VPS 全场 8 折 充 300 送 50 日本&新加坡&美国日本VPS[主机]
- 按时计费 $2.49 月 768M 内存 10G 硬盘 1T 流量 1G全球[VPS测评]
- 299 元 年 1G 内存 15G SSD 2Mbps 不限量 XEN 香港VPS[主机]
- TMThosting→西雅图 KVM 七五折起 月付 2.96 美元起 全球[VPS测评]
- LOCVPS→38.5 元 月 XEN-2GB 40GB 3M 香港机房香港VPS[主机]
- Vultr 新用户首次充多少送多少(最高送 100 美元)活动下架 当有全球[VPS测评]
- 樊云 香港CN2九折优惠 1g内存20G硬盘5m带宽不限流 月付仅22.香港VPS[主机]
- 忘记WordPress后台密码怎么办?全球[VPS测评]
- 哪些国外VPS商家可以支付宝付款?全球[VPS测评]
- WordPress文章发布时间怎么精确到秒?全球[VPS测评]
- 搬瓦工全场VPS12.22%折扣的新年优惠码还有效,洛杉矶、香港和日本C日本VPS[主机]
- Linux安装rinetd实现TCP UDP端口转发(rinetd使用教全球[VPS测评]
- Jetorbit-印尼雅加达VPS 10$ 月 1C1G25G硬盘 百兆全球[VPS测评]
- 很简单的图片不规则布局样式,纯css样式实现的图片瀑布流布局全球[VPS测评]
- 老六云怎么样?香港cn2vps月付23元香港NTT月付9.9元香港VPS[主机]
- 漂亮的Css3响应侧边栏菜单,动画风格侧边栏导航菜单样式全球[VPS测评]
- CMCUCT:2核2GB/20GB香港ntt线路vps 6TB流量/1G香港VPS[主机]
- 快速云:怎么连接vps?VPS的用途有哪些?全球[VPS测评]
- 阿里云服务器一年价格多少钱?阿里云服务器报价全球[VPS测评]
- 云计算拯救互联网巨头?全球[VPS测评]
- 春天的风最容易温暖心灵,适合朋友圈和抖音的12条文案全球[VPS测评]
- 青云互联:年终特惠香港弹性云服务器15元/月起,可自定义配置,可选winWINDOWS
- UCloud香港云服务器:免备案,CN2,全球31个机房,香港2核4G2香港VPS[主机]
- 优网主机怎么样?香港云主机/美国云主机价格低至19元/月美国VPS[主机]
- 阳帝云:3月优惠活动,香港GIA/香港TG/洛杉矶200G硬防vps,首香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-208707.html











