php 卷曲错误:操作超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21501159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:12:06  来源:igfitidea点击:

Curl error: Operation timed out

phpcurl

提问by alias51

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_Request2and Curlmodules. What's the best approach to try and resolve this?

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

回答by Sabuj Hassan

Your curl gets timed out. Probably the url you are trying that requires more that 30 seconds.

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

If you are running the script through browser, then set the set_time_limitto zero for infinite seconds.

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

set_time_limit(0);

Increase the curl's operation time limit using this option CURLOPT_TIMEOUT

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

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);

回答by user7224508

Some time this error in Joomla appear because some thing incorrect with SESSION or coockie. That may because incorrect HTTPd server setting or because some before CURL or Server http requests

有时在 Joomla 中出现此错误是因为 SESSION 或 coockie 的某些事情不正确。这可能是因为不正确的 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 .."

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

回答by Haris Khurshid

I got same problem lot of time. Check your request url, if you are requesting on local server like 127.1.1/apior 192.168...., try to change it, make sure you are hitting cloud.

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

回答by Ocean

$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\":\"[email protected]\",\"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”。

回答by Maulik Makwana

In curl request add time out 0 so its infinite time set like CURLOPT_TIMEOUT set 0

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