卷曲超时小于1000ms总是失败?
此代码始终失败(即 $result
是布尔值 false
):
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$curl_version = curl_version();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 999);
$result = curl_exec($ch);
curl_close($ch);
此代码始终成功(即 $result
是包含标头的字符串):
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$curl_version = curl_version();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 1000);
$result = curl_exec($ch);
curl_close($ch);
唯一的区别是我将超时从 999 毫秒更改为 1000 毫秒。
这肯定是curl 中的一个错误,或者是我错过的连接超时文档中的某种最小值。是哪一个?我的钱花在后者上。
This code always fails (i.e., $result
is Boolean false
):
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$curl_version = curl_version();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 999);
$result = curl_exec($ch);
curl_close($ch);
This code always succeeds (i.e., $result
is a string containing the header):
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$curl_version = curl_version();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 1000);
$result = curl_exec($ch);
curl_close($ch);
The only difference is that I've changed the timeout from 999ms to 1000ms.
This must be either a bug in curl or some sort of minimum in the documentation for connection timeouts that I missed. Which is it? My money is on the latter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自: http://www.php.net/manual/en/function .curl-setopt.php
from: http://www.php.net/manual/en/function.curl-setopt.php
处理此问题的另一种方法是,除了
CURLOPT_CONNECTTIMEOUT_MS
之外,还将CURLOPT_NOSIGNAL
选项设置为 1。请参阅http://curl.haxx.se/libcurl/c/CURLOPT_NOSIGNAL.html 了解更多信息。Another way of dealing with this would be to set the
CURLOPT_NOSIGNAL
option to 1 in addition to theCURLOPT_CONNECTTIMEOUT_MS
. See http://curl.haxx.se/libcurl/c/CURLOPT_NOSIGNAL.html for more info.