简体   繁体   English

卷曲错误:操作超时

[英]Curl error: Operation timed out

I have the following fatal error when trying to use Curl:尝试使用 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

However, I can't see how to best debug it.但是,我看不出如何最好地调试它。 There's no reference to any line of code I've written, only the HTTP_Request2 and Curl modules.没有参考我编写的任何代码行,只有HTTP_Request2Curl模块。 What's the best approach to try and resolve this?尝试解决此问题的最佳方法是什么?

Your curl gets timed out.你的 curl 超时了。 Probably the url you are trying that requires more that 30 seconds.可能您正在尝试的网址需要超过 30 秒。

If you are running the script through browser, then set the set_time_limit to zero for infinite seconds.如果您通过浏览器运行脚本,则将set_time_limit设置为零无限秒。

set_time_limit(0);

Increase the curl's operation time limit using this option CURLOPT_TIMEOUT使用此选项CURLOPT_TIMEOUT增加 curl 的操作时间限制

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

It can also happen for infinite redirection from the server.来自服务器的无限重定向也可能发生这种情况。 To halt this try to run the script with follow location disabled.要停止此操作,请尝试在禁用跟随位置的情况下运行脚本。

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);

Some time this error in Joomla appear because some thing incorrect with SESSION or coockie.有时在 Joomla 中出现此错误是因为 SESSION 或 coockie 出现了某些错误。 That may because incorrect HTTPd server setting or because some before CURL or Server http requests这可能是因为不正确的 HTTPd 服务器设置或因为某些在 CURL 或服务器 http 请求之前

so PHP code like:所以 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);

will need replace to PHP code将需要替换为 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);

May be some body reply how this options connected with "Curl error: Operation timed out after .."可能是一些正文回复此选项如何与“卷曲错误:操作超时后..”相关联

I got same problem lot of time.我有很多时间遇到同样的问题。 Check your request url, if you are requesting on local server like 127.1.1/api or 192.168.... , try to change it, make sure you are hitting cloud.检查您的请求 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;
}

initialize array index "CURLOPT_TIMEOUT" with a value in seconds according to time required for response .根据响应所需的时间,使用以秒为单位的值初始化数组索引“CURLOPT_TIMEOUT”。

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

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

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