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."; }
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《curl CURLOPT_TIMEOUT_MS 小于1秒无效的解决方案》
文章链接:https://www.liuzhanwu.com/2107.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。
文章名称:《curl CURLOPT_TIMEOUT_MS 小于1秒无效的解决方案》
文章链接:https://www.liuzhanwu.com/2107.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。
相关推荐
456CDN:为网络安全与加速保驾护航,CDN测评推荐
456CDN:高防CDN,200GB DDoS防护,无视CC,1TB流量@50Mbps带宽,500元/月,安全加速,智能守护,稳定安全
#618大促#最后一天|凌霞软件旗下新一代服务器面板 1Panel:永久授权 539元起,10台4900元,即买即赠SSL证书
#618大促#最后一天|凌霞软件旗下强大易用的建站工具 Halo:永久授权 660元起,10台6000元,即买即赠SSL证书
#6.18促销#Themebetter:WordPress主题特惠7折,DUX主题折后559元,不限制域名授权数目
耗子Linux 面板:又一个轻量 Linux 服务器运维管理面板
#11.11#Themebetter:年度大放价,全场6折特惠,DUX主题折后480元,不限制域名授权数目
紧急提醒:DediPath官宣跑路