繁体   English   中英

卷曲错误:操作超时

[英]Curl error: Operation timed out

尝试使用 Curl 时出现以下致命错误:

PHP Fatal error:  Uncaught HTTP_Request2_MessageException: 
Curl error: Operation timed out after 30000 milliseconds with 0 bytes received in      
/usr/share/php/HTTP/Request2/Adapter/Curl.php on line 200
Exception trace    Function       Location
0                  HTTP_Request2_Adapter_Curl::wrapCurlError('Resource id #12') 
                   /usr/share/php/HTTP/Request2/Adapter/Curl.php:200
1                  HTTP_Request2_Adapter_Curl->sendRequest(Object(HTTP_Request2))
/usr/share/php/HTTP/Request2.php:959< in /usr/share/php/HTTP/Request2/Adapter/Curl.php on line 172

但是,我看不出如何最好地调试它。 没有参考我编写的任何代码行,只有HTTP_Request2Curl模块。 尝试解决此问题的最佳方法是什么?

你的 curl 超时了。 可能您正在尝试的网址需要超过 30 秒。

如果您通过浏览器运行脚本,则将set_time_limit设置为零无限秒。

set_time_limit(0);

使用此选项CURLOPT_TIMEOUT增加 curl 的操作时间限制

curl_setopt($ch, CURLOPT_TIMEOUT,500); // 500 seconds

来自服务器的无限重定向也可能发生这种情况。 要停止此操作,请尝试在禁用跟随位置的情况下运行脚本。

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);

有时在 Joomla 中出现此错误是因为 SESSION 或 coockie 出现了某些错误。 这可能是因为不正确的 HTTPd 服务器设置或因为某些在 CURL 或服务器 http 请求之前

所以 PHP 代码如下:

  curl_setopt($ch, CURLOPT_URL, $url_page);
  curl_setopt($ch, CURLOPT_HEADER, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch, CURLOPT_TIMEOUT, 30); 
  curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
  curl_setopt($ch, CURLOPT_REFERER, $url_page);
  curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . "./cookie.txt");
  curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . "./cookie.txt");
  curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());

  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  if( $sc != "" ) curl_setopt($ch, CURLOPT_COOKIE, $sc);

将需要替换为 PHP 代码

  curl_setopt($ch, CURLOPT_URL, $url_page);
  curl_setopt($ch, CURLOPT_HEADER, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch, CURLOPT_TIMEOUT, 30); 
//curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
  curl_setopt($ch, CURLOPT_REFERER, $url_page);
  curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
//curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . "./cookie.txt");
//curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . "./cookie.txt");
//curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); // !!!!!!!!!!!!!
  //if( $sc != "" ) curl_setopt($ch, CURLOPT_COOKIE, $sc);

可能是一些正文回复此选项如何与“卷曲错误:操作超时后..”相关联

我有很多时间遇到同样的问题。 检查您的请求 url,如果您在本地服务器上请求127.1.1/api192.168.... ,请尝试更改它,确保您正在访问云。

$curl = curl_init();

  curl_setopt_array($curl, array(
  CURLOPT_URL => "", // Server Path
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 3000, // increase this
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\"email\":\"DarylBrowng@gmail.coh\",\"password\":\"markus William\",\"username\":\"Daryl Brown\",\"mobile\":\"013132131112\","msg":"No more SSRIs." }",
  CURLOPT_HTTPHEADER => array(
    "Content-Type: application/json",
    "Postman-Token: 4867c7a3-2b3d-4e9a-9791-ed6dedb046b1",
    "cache-control: no-cache"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

根据响应所需的时间,使用以秒为单位的值初始化数组索引“CURLOPT_TIMEOUT”。

在 curl 请求中添加超时 0,因此其无限时间设置为 CURLOPT_TIMEOUT 设置 0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM