在 PHP 中设置 Curl 的超时
我正在通过 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);
然而,curl 请求始终在请求完成之前结束(通过浏览器请求时<1000)。有谁知道这是否是在curl中设置超时的正确方法?
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.
$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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
请参阅文档: http://www.php.net/manual/en/ function.curl-setopt.php
CURLOPT_CONNECTTIMEOUT
- 尝试连接时等待的秒数。使用 0 无限期等待。CURLOPT_TIMEOUT
- 允许 cURL 函数执行的最大秒数。也不要忘记增加 php 脚本自身的执行时间:
See documentation: 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.also don't forget to enlarge time execution of php script self:
嗯,在我看来,
CURLOPT_TIMEOUT
定义了任何 cURL 函数允许执行的时间量。我认为您实际上应该查看CURLOPT_CONNECTTIMEOUT
,因为它告诉 cURL 等待连接完成的最长时间。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 atCURLOPT_CONNECTTIMEOUT
instead, since that tells cURL the maximum amount of time to wait for the connection to complete.这有一个怪癖,可能与某些人相关......来自 PHP 文档注释。
来自 http://www.php.net/manual/en/function.curl-setopt.php#104597
There is a quirk with this that might be relevant for some people... From the PHP docs comments.
From http://www.php.net/manual/en/function.curl-setopt.php#104597
您的代码将超时设置为 1000秒。对于毫秒,请使用
CURLOPT_TIMEOUT_MS
。Your code sets the timeout to 1000 seconds. For milliseconds, use
CURLOPT_TIMEOUT_MS
.您需要确保您和文件之间存在超时。
在本例中是 PHP 和 Curl。
要告诉 Curl 在传输仍处于活动状态时永不超时,您需要将
CURLOPT_TIMEOUT
设置为0
,而不是1000
。在 PHP 中,您必须再次删除时间限制,否则 PHP 本身(默认情况下 30 秒后)将根据 Curl 的请求终止脚本。 仅此一项就可以解决您的问题。
此外,如果您需要数据完整性,则可以使用
ignore_user_abort
添加一层安全性:客户端断开连接将中断脚本的执行并可能损坏数据,
例如。非过渡性数据库查询,构建配置文件,等等,而在您的情况下,它会下载部分文件......您可能会或不会关心这一点。
回答这个老问题是因为该线程位于
CURL_TIMEOUT
引擎搜索的顶部。You will need to make sure about timeouts between you and the file.
In this case PHP and Curl.
To tell Curl to never timeout when a transfer is still active, you need to set
CURLOPT_TIMEOUT
to0
, instead of1000
.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
: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.
Answering this old question because this thread is at the top on engine searches for
CURL_TIMEOUT
.您无法从浏览器运行该请求,它将超时等待运行 CURL 请求的服务器响应。浏览器可能会在 1-2 分钟内超时,这是默认的网络超时。
您需要从命令行/终端运行它。
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.
You need to run it from the command line/terminal.
如果您使用 PHP 作为 fastCGI 应用程序,请确保检查 fastCGI 超时设置。
请参阅:PHP curl put 500 错误
If you are using PHP as a fastCGI application then make sure you check the fastCGI timeout settings.
See: PHP curl put 500 error
不相关,但请注意
CURLOPT_HTTPHEADER
需要设置一个 HTTP 标头字段数组,格式为array('Content-type: text/plain', 'Content-length: 100')
;-)Not related, but mind that
CURLOPT_HTTPHEADER
expects an array of HTTP header fields to set, in the formatarray('Content-type: text/plain', 'Content-length: 100')
;-)