Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Curl error: Operation timed out

Tags:

php

curl

I have the following fatal error when trying to use 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. What's the best approach to try and resolve this?

like image 682
alias51 Avatar asked Feb 01 '14 17:02

alias51


People also ask

What causes a cURL timeout?

There are many reasons for curl not working. Some of them can be, 1) Response time is slow. 2) Few site has check on few header parameters to respond to request. These parameters include User-Agent, Referer, etc to make sure it is coming from valid source and not through bots.

What does cURL error 28 mean?

If you see this error message displayed in your WordPress admin, it means something is preventing your emails to be sent. You may notice the sending is paused or the automated newsletters are not being sent.

Why does it say operation timed out?

This error occurs when Screens attempts to establish a connection to the remote computer, but it doesn't receive a response from that computer.


3 Answers

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

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

set_time_limit(0);

Increase the curl's operation time limit using this option 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);
like image 124
Sabuj Hassan Avatar answered Sep 22 '22 18:09

Sabuj Hassan


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.

like image 38
Haris Khurshid Avatar answered Sep 22 '22 18:09

Haris Khurshid


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

so PHP code like:

  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

  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 .."

like image 40
user7224508 Avatar answered Sep 21 '22 18:09

user7224508