gpt4 book ai didi

php - 在 PHP 中设置 Curl 的超时时间

转载 作者:IT老高 更新时间:2023-10-28 11:39:53 32 4
gpt4 key购买 nike

我正在通过 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 中设置超时的正确方法?

最佳答案

参见文档:http://www.php.net/manual/en/function.curl-setopt.php

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

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

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

set_time_limit(0);// to infinity for example

关于php - 在 PHP 中设置 Curl 的超时时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2582057/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com