在PHP中设置Curl的超时。

时间:2022-10-25 21:45:27

I'm running a curl request on an eXist database through php. The dataset is very large, and as a result, the database consistently takes a long amount of time to return an XML response. To fix that, we set up a curl request, with what is supposed to be a long timeout.

我正在通过php在eXist数据库上运行curl请求。数据集非常大,因此,数据库始终需要大量的时间来返回XML响应。为了解决这个问题,我们设置了一个curl请求,它应该是一个长时间的超时。

$ch = curl_init();
$headers["Content-Length"] = strlen($postString);
$headers["User-Agent"] = "Curl/1.0";

curl_setopt($ch, CURLOPT_URL, $requestUrl);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, 'admin:');
curl_setopt($ch,CURLOPT_TIMEOUT,1000);
$response = curl_exec($ch);
curl_close($ch);

However, the curl request consistently ends before the request is completed (<1000 when requested via a browser). Does anyone know if this is the proper way to set timeouts in curl?

然而,curl请求在请求完成之前始终会结束(在浏览器请求的时候是<1000)。有谁知道这是在curl中设置超时的正确方法吗?

7 个解决方案

#1


262  

See documentation: http://www.php.net/manual/en/function.curl-setopt.php

看到文档:http://www.php.net/manual/en/function.curl-setopt.php

CURLOPT_CONNECTTIMEOUT - The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.
CURLOPT_TIMEOUT - The maximum number of seconds to allow cURL functions to execute.

CURLOPT_CONNECTTIMEOUT——在尝试连接时等待的秒数。使用0无限期地等待。CURLOPT_TIMEOUT——允许cURL函数执行的最大秒数。

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); 
curl_setopt($ch, CURLOPT_TIMEOUT, 400); //timeout in seconds

also don't forget to enlarge time execution of php script self:

也别忘了放大php脚本自我的时间执行:

set_time_limit(0);// to infinity for example

#2


52  

Hmm, it looks to me like CURLOPT_TIMEOUT defines the amount of time that any cURL function is allowed to take to execute. I think you should actually be looking at CURLOPT_CONNECTTIMEOUT instead, since that tells cURL the maximum amount of time to wait for the connection to complete.

嗯,在我看来,CURLOPT_TIMEOUT定义了任何cURL函数被允许执行的时间长度。我认为您实际上应该查看CURLOPT_CONNECTTIMEOUT,因为它告诉cURL要等待连接完成的最大时间。

#3


25  

Your code sets the timeout to 1000 seconds. For milliseconds, use CURLOPT_TIMEOUT_MS.

您的代码将超时设置为1000秒。CURLOPT_TIMEOUT_MS毫秒,使用。

#4


18  

There is a quirk with this that might be relevant for some people... From the PHP docs comments.

有一种怪癖可能与某些人有关……来自PHP文档注释。

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:

如果您想在不到一秒的时间内cURL超时,那么可以使用CURLOPT_TIMEOUT_MS,尽管在“类unix系统”上存在一个bug/“特性”,如果值小于1000 ms,则会导致libcurl立即超时,而错误“cURL error(28):超时”。对这种行为的解释是:

"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."

“如果libcurl是使用标准系统名称解析器构建的,那么传输的部分仍将使用完整的第二次解决方案,在一秒内允许最小超时。”

What this means to PHP developers is "You can't 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)"

对于PHP开发人员来说,这意味着“您不能在没有测试的情况下使用这个函数,因为您无法判断libcurl是否使用了标准系统名称解析器(但是您可以确定它是)”

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.

问题是,在(Li|U)nix上,当libcurl使用标准名称解析器时,在名称解析过程中会提高一个SIGALRM, libcurl认为这是超时警报。

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:

解决方案是使用CURLOPT_NOSIGNAL来禁用信号。下面是一个示例脚本,它请求自己造成10秒的延迟,以便您可以测试超时:

<?php
if (!isset($_GET['foo'])) {
        // Client
        $ch = curl_init('http://localhost/test/test_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_error\n";
        } else {
                echo "Data received: $data\n";
        }
} else {
        // Server
        sleep(10);
        echo "Done.";
}
?>

From http://www.php.net/manual/en/function.curl-setopt.php#104597

从http://www.php.net/manual/en/function.curl-setopt.php # 104597

#5


8  

You can't run the request from a browser, it will timeout waiting for the server running the CURL request to respond. The browser is probably timing out in 1-2 minutes, the default network timeout.

您无法从浏览器运行请求,它将超时等待运行CURL请求的服务器响应。浏览器可能在1-2分钟内超时,默认的网络超时。

You need to run it from the command line/terminal.

您需要从命令行/终端运行它。

#6


6  

You will need to make sure about timeouts between you and the file. In this case PHP and Curl.

您需要确保您和文件之间的超时。这里是PHP和Curl。

To tell Curl to never timeout when a transfer is still active, you need to set CURLOPT_TIMEOUT to 0, instead of 1000.

要告诉Curl在仍然处于活动状态时不要超时,您需要将CURLOPT_TIMEOUT设置为0,而不是1000。

curl_setopt($ch, CURLOPT_TIMEOUT, 0);

In PHP, again, you must remove time limits or PHP it self (after 30 seconds by default) will kill the script along Curl's request. This alone should fix your issue.
In addition, if you require data integrity, you could add a layer of security by using ignore_user_abort:

在PHP中,您必须再次删除时间限制或PHP(在默认情况下30秒之后),将根据Curl的请求终止脚本。仅此一点就可以解决你的问题。此外,如果您需要数据完整性,您可以使用ignore_user_abort添加一层安全性:

# The maximum execution time, in seconds. If set to zero, no time limit is imposed.
set_time_limit(0);

# Make sure to keep alive the script when a client disconnect.
ignore_user_abort(true);

A client disconnection will interrupt the execution of the script and possibly damaging data,
eg. non-transitional database query, building a config file, ecc., while in your case it would download a partial file... and you might, or not, care about this.

客户端断开将中断脚本的执行,并可能破坏数据(如)。非过渡数据库查询,构建一个配置文件,ecc。,在你的情况下,它会下载一个部分文件…你可能会关心这个。

Answering this old question because this thread is at the top on engine searches for CURL_TIMEOUT.

回答这个老问题,因为这个线程在引擎搜索的顶部是CURL_TIMEOUT。

#7


3  

If you are using PHP as a fastCGI application then make sure you check the fastCGI timeout settings. See: PHP curl put 500 error

如果您正在使用PHP作为fastCGI应用程序,那么请确保检查fastCGI超时设置。查看:PHP curl设置了500个错误。

#1


262  

See documentation: http://www.php.net/manual/en/function.curl-setopt.php

看到文档:http://www.php.net/manual/en/function.curl-setopt.php

CURLOPT_CONNECTTIMEOUT - The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.
CURLOPT_TIMEOUT - The maximum number of seconds to allow cURL functions to execute.

CURLOPT_CONNECTTIMEOUT——在尝试连接时等待的秒数。使用0无限期地等待。CURLOPT_TIMEOUT——允许cURL函数执行的最大秒数。

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); 
curl_setopt($ch, CURLOPT_TIMEOUT, 400); //timeout in seconds

also don't forget to enlarge time execution of php script self:

也别忘了放大php脚本自我的时间执行:

set_time_limit(0);// to infinity for example

#2


52  

Hmm, it looks to me like CURLOPT_TIMEOUT defines the amount of time that any cURL function is allowed to take to execute. I think you should actually be looking at CURLOPT_CONNECTTIMEOUT instead, since that tells cURL the maximum amount of time to wait for the connection to complete.

嗯,在我看来,CURLOPT_TIMEOUT定义了任何cURL函数被允许执行的时间长度。我认为您实际上应该查看CURLOPT_CONNECTTIMEOUT,因为它告诉cURL要等待连接完成的最大时间。

#3


25  

Your code sets the timeout to 1000 seconds. For milliseconds, use CURLOPT_TIMEOUT_MS.

您的代码将超时设置为1000秒。CURLOPT_TIMEOUT_MS毫秒,使用。

#4


18  

There is a quirk with this that might be relevant for some people... From the PHP docs comments.

有一种怪癖可能与某些人有关……来自PHP文档注释。

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:

如果您想在不到一秒的时间内cURL超时,那么可以使用CURLOPT_TIMEOUT_MS,尽管在“类unix系统”上存在一个bug/“特性”,如果值小于1000 ms,则会导致libcurl立即超时,而错误“cURL error(28):超时”。对这种行为的解释是:

"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."

“如果libcurl是使用标准系统名称解析器构建的,那么传输的部分仍将使用完整的第二次解决方案,在一秒内允许最小超时。”

What this means to PHP developers is "You can't 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)"

对于PHP开发人员来说,这意味着“您不能在没有测试的情况下使用这个函数,因为您无法判断libcurl是否使用了标准系统名称解析器(但是您可以确定它是)”

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.

问题是,在(Li|U)nix上,当libcurl使用标准名称解析器时,在名称解析过程中会提高一个SIGALRM, libcurl认为这是超时警报。

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:

解决方案是使用CURLOPT_NOSIGNAL来禁用信号。下面是一个示例脚本,它请求自己造成10秒的延迟,以便您可以测试超时:

<?php
if (!isset($_GET['foo'])) {
        // Client
        $ch = curl_init('http://localhost/test/test_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_error\n";
        } else {
                echo "Data received: $data\n";
        }
} else {
        // Server
        sleep(10);
        echo "Done.";
}
?>

From http://www.php.net/manual/en/function.curl-setopt.php#104597

从http://www.php.net/manual/en/function.curl-setopt.php # 104597

#5


8  

You can't run the request from a browser, it will timeout waiting for the server running the CURL request to respond. The browser is probably timing out in 1-2 minutes, the default network timeout.

您无法从浏览器运行请求,它将超时等待运行CURL请求的服务器响应。浏览器可能在1-2分钟内超时,默认的网络超时。

You need to run it from the command line/terminal.

您需要从命令行/终端运行它。

#6


6  

You will need to make sure about timeouts between you and the file. In this case PHP and Curl.

您需要确保您和文件之间的超时。这里是PHP和Curl。

To tell Curl to never timeout when a transfer is still active, you need to set CURLOPT_TIMEOUT to 0, instead of 1000.

要告诉Curl在仍然处于活动状态时不要超时,您需要将CURLOPT_TIMEOUT设置为0,而不是1000。

curl_setopt($ch, CURLOPT_TIMEOUT, 0);

In PHP, again, you must remove time limits or PHP it self (after 30 seconds by default) will kill the script along Curl's request. This alone should fix your issue.
In addition, if you require data integrity, you could add a layer of security by using ignore_user_abort:

在PHP中,您必须再次删除时间限制或PHP(在默认情况下30秒之后),将根据Curl的请求终止脚本。仅此一点就可以解决你的问题。此外,如果您需要数据完整性,您可以使用ignore_user_abort添加一层安全性:

# The maximum execution time, in seconds. If set to zero, no time limit is imposed.
set_time_limit(0);

# Make sure to keep alive the script when a client disconnect.
ignore_user_abort(true);

A client disconnection will interrupt the execution of the script and possibly damaging data,
eg. non-transitional database query, building a config file, ecc., while in your case it would download a partial file... and you might, or not, care about this.

客户端断开将中断脚本的执行,并可能破坏数据(如)。非过渡数据库查询,构建一个配置文件,ecc。,在你的情况下,它会下载一个部分文件…你可能会关心这个。

Answering this old question because this thread is at the top on engine searches for CURL_TIMEOUT.

回答这个老问题,因为这个线程在引擎搜索的顶部是CURL_TIMEOUT。

#7


3  

If you are using PHP as a fastCGI application then make sure you check the fastCGI timeout settings. See: PHP curl put 500 error

如果您正在使用PHP作为fastCGI应用程序,那么请确保检查fastCGI超时设置。查看:PHP curl设置了500个错误。