[不花钱站长]:从免费域名开始[0元] Oracle永久免费VPS[0元] VPS环境搭建免费脚本[0元] 秒变大盘挂载免费网盘[0元] 小白免费采集器1天500W[0元] CF免费DNS+CDN服务[0元]
[零基础网赚]:撸本站免费源码[0元] 网站强引免费蜘蛛法[0元] 图片视频文件免费存储[0元] 外链轻松发布免费友链[0元] 入坑免费教程学习源代码[0元] 网上百种+赚钱联盟推荐[?元]

↑↑VPS推荐网 WWW.VPS.COM.IN 稳定 便宜 免备案↑↑


您现在的位置是:首页 > 全球[VPS测评] >  永久免费VPS 香港VPS,国外永久免费VPS

curl CURLOPT_TIMEOUT_MS 小于 1 秒无效的解决方案

全球[VPS测评]来源:VPS推荐网点击:1532
【性价之王】【线路之王】【价格之王】【配置之王】
【免费之王】【香港首推】【梯子之王】【独服之王】
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


转载请注明原文地址:http://140.238.13.167:12355/read-211659.html

下一篇       上一篇