如何设置PHP5的curl调用超时?已发布的 CURL 选项似乎不起作用

发布于 2024-08-02 11:40:38 字数 856 浏览 7 评论 0 原文

我们编写了一个从外部服务器提取数据的脚本。如果服务器出现故障,我们不希望服务器等待数据,因为我们处理大量数据,并且不希望服务器陷入困境。为了解决这个问题,如果卷曲调用花费的时间超过几百毫秒,我们会尝试使它们超时。

我发现一些文档说 CURLOPT_TIMEOUT_MS 和 CURLOPT_CONNECTTIMEOUT_MS 在我的 php 和 libcurl 版本中应该可用,但即使我将超时设置为 1ms,它似乎也没有超时。

$url = "http://www.cnn.com;

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER,0); //Change this to a 1 to return headers
            curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 1);

            $data = curl_exec($ch);

            curl_close($ch);

有谁知道我们做错了什么或有其他方法可以做到这一点?

We've written a script that pulls data from an external server. If the server goes down we don't want our server waiting for the data since we process a lot of data and we don't want it bogged down. To address this, we're trying to timeout our curl calls if they take more than a couple hundred milliseconds.

I found some documentation saying that CURLOPT_TIMEOUT_MS and CURLOPT_CONNECTTIMEOUT_MS should be available in my version of php and libcurl, but it does not seem to be timing out, even if I set the timeout to 1ms.

$url = "http://www.cnn.com;

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER,0); //Change this to a 1 to return headers
            curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 1);

            $data = curl_exec($ch);

            curl_close($ch);

Does anyone know what we're doing wrong or another way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神仙妹妹 2024-08-09 11:40:38

无响应的dns服务器和curl多重超时不起作用中看到了这个:

“...我们曾经有过这样的情况:
我们拉信息的网站有
DNS 服务器变得无响应。什么时候
这发生在curl中设置的超时
(php 绑定)不起作用
预期的。 1分14秒后超时
秒“无法解析主机:
www.yahoo.com(未找到域名)”
为了在测试环境中实现这一点,我们
修改 /etc/resolv.conf 以获得
名称服务器不存在
(名称服务器 1.1.1.1)。无论如何
它们被设置为

<前><代码>(CURLOPT_CONNECTTIMEOUT,CURLOPT_CONNECTTIMEOUT_MS
、CURLOPT_TIMEOUT、CURLOPT_TIMEOUT_MS)

当我们无法获取时,它们不会超时
到 DNS 服务器。我使用curl_multi
因为我们有多个来源
我们同时从中获取信息
时间。下面的例子使一个
例如简单性。并且作为一个
旁注:curl_errno 不返回
即使有错误代码
错误。不知道为什么......”

saw this in unresponsive dns server and curl multi timeouts not working:

"...We have had some times where a
site that we pull information has had
dns server become unresponsive. When
this happens the timeouts set in curl
(php bindings) do not work as
expected. It times out after 1min 14
sec with "Could not resolve host:
www.yahoo.com (Domain name not found)"
To make this happen in test env we
modify /etc/resolv.conf to have a
nameserver that does not exist
(nameserver 1.1.1.1). No mater what
they are set at

(CURLOPT_CONNECTTIMEOUT, CURLOPT_CONNECTTIMEOUT_MS 
, CURLOPT_TIMEOUT, CURLOPT_TIMEOUT_MS) 

they don't timeout when we cant get
to the DNS server. I use curl_multi
because i we have multiple sources
that we pull info from at the same
time. The example below makes one
call for example simplicity. And as a
side note curl_errno does not return
an error code even though there was an
error. Not sure why..."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文