Curl php timeout exception

PHP: Setting cURL timeout options.

This is a short guide on how to use the cURL timeout options in PHP. In certain cases, you may want to specify a timeout in order to prevent your HTTP request from taking too long.

There are two cURL timeout options that you need to know about. These options are slightly different from one another, so I will explain them now:

  • CURLOPT_CONNECTTIMEOUT: The maximum amount of seconds that cURL should spend attempting to connect to a given URL.
  • CURLOPT_TIMEOUT: The maximum amount of seconds it should take for all cURL operations to be carried out. i.e. The maximum amount of time that the request should take.

Take a look at the following piece of code.

In the code above, I set CURLOPT_CONNECTTIMEOUT to 10 seconds and I set CURLOPT_TIMEOUT to 30 seconds. This means:

  • cURL should only spend 10 seconds attempting to connect to the given URL. If it can’t connect after 10 seconds, a timeout should occur.
  • If it does connect successfully, it should only spend a maximum of 30 seconds executing the request. In other words, if we connect, but the server takes too long to respond in full, then cURL should call it a day and stop waiting.
Читайте также:  Html редактор на маке

Testing the cURL timeout.

To test this, I added some basic cURL error handling to the end of my script so that any cURL errors would result in an exception being thrown. The local URL that I am sending a HTTP request to uses PHP’s sleep function to create a 40 second delay (which is higher than the 30 seconds limit that we set with CURLOPT_TIMEOUT). As a result, the following exception was thrown:

Uncaught exception ‘Exception’ with message ‘Operation timed out after 30015 milliseconds with 0 bytes received’

As you can see, the cURL timeout after 30,015 milliseconds, which is 30 seconds.

Hopefully this tutorial cleared a few things up for you!

Источник

PHP curl Connection timed out error

I am calling an API using curl in PHP, Sometimes it works fine and sometimes I get Failed to connect to api-domain.com port 80: Connection timed out It’s a little strange that sometimes it’s working and sometimes it’s not. To troubleshoot the issue I have printed the curl_getinfo() when it is not working, please check it below. It’s showing connect time = 0 and total time = 130 sec, I am not really sure what it means. If any one has good understanding about it please review the below log and help me understand what the exact issue is.

[url] => http://api-domain.com/?act=get_story_banners [content_type] => text/html; charset=UTF-8 [http_code] => 200 [header_size] => 630 [req uest_size] => 283 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 130.335916 [namelookup_time] => 0.000016 [connect_time] => 0 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 744 [speed_download] => 13814 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => -1 [starttransfer_time] => 0 [redirect_time] => 0 [redirect_url] => [primary_ip] => 34.231.133.7 [certinfo] => Array() [primary_port] => 80 [local_ip] => xxx.xxx.xxx.xxx [local_port] => 48080 

Edit

Sometimes curl request comes to the REST API Server and sometimes it doesn’t. It is discarded at connection level itself, does not reach to the REST API server. I am little confused as to why sometimes it connects and sometimes it doesn’t.

Источник

Curl error: Operation timed out

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?

have you tried debugging before and after the statements in your code that actually make the curl commands? try and find the offending request that way? from there you can try it command line, with the -v switch, and try and get more info that way. possibly the URL has now 404’d.

5 Answers 5

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.

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

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.

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

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

Источник

http — Setting Curl’s Timeout in PHP

I’m running a curl request on an eXist database through php. The dataset is very large, and as a result, the database consistently takes a long amount of time to return an XML response. To fix that, we set up a curl request, with what is supposed to be a long timeout.

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

Answer

Solution:

CURLOPT_CONNECTTIMEOUT — The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.
CURLOPT_TIMEOUT — The maximum number of seconds to allow cURL functions to execute.

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

also don’t forget to enlarge time execution of php script self:

set_time_limit(0);// to infinity for example 

Answer

Solution:

Hmm, it looks to me like CURLOPT_TIMEOUT defines the amount of time that any cURL function is allowed to take to execute. I think you should actually be looking at CURLOPT_CONNECTTIMEOUT instead, since that tells cURL the maximum amount of time to wait for the connection to complete.

Answer

Solution:

There is a quirk with this that might be relevant for some people. From the PHP docs comments.

If you want cURL to timeout in less than one second, you can use CURLOPT_TIMEOUT_MS , although there is a bug/»feature» on «Unix-like systems» that causes libcurl to timeout immediately if the value is < 1000 ms with the error "cURL Error (28): Timeout was reached". The explanation for this behavior is:

«If libcurl is built to use the standard system name resolver, that portion of the transfer will still use full-second resolution for timeouts with a minimum timeout allowed of one second.»

What this means to PHP developers is «You can’t use this function without testing it first, because you can’t tell if libcurl is using the standard system name resolver (but you can be pretty sure it is)»

The problem is that on (Li|U)nix, when libcurl uses the standard name resolver, a SIGALRM is raised during name resolution which libcurl thinks is the timeout alarm.

The solution is to disable signals using CURLOPT_NOSIGNAL. Here’s an example script that requests itself causing a 10-second delay so you can test timeouts:

if (!isset($_GET['foo'])) < // Client $ch = curl_init('http://localhost/test/test_timeout.php?foo=bar'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_NOSIGNAL, 1); curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200); $data = curl_exec($ch); $curl_errno = curl_errno($ch); $curl_error = curl_error($ch); curl_close($ch); if ($curl_errno >0) < echo "cURL Error ($curl_errno): $curl_error\n"; >else < echo "Data received: $data\n"; >> else < // Server sleep(10); echo "Done."; >

Answer

Solution:

Your code sets the timeout to 1000 seconds. For milliseconds, use CURLOPT_TIMEOUT_MS .

Answer

Solution:

You will need to make sure about timeouts between you and the file. In this case PHP and Curl.

To tell Curl to never timeout when a transfer is still active, you need to set CURLOPT_TIMEOUT to 0 , instead of 1000 .

curl_setopt($ch, CURLOPT_TIMEOUT, 0); 

In PHP, again, you must remove time limits or PHP it self (after 30 seconds by default) will kill the script along Curl’s request. This alone should fix your issue.
In addition, if you require data integrity, you could add a layer of security by using ignore_user_abort :

# The maximum execution time, in seconds. If set to zero, no time limit is imposed. set_time_limit(0); # Make sure to keep alive the script when a client disconnect. ignore_user_abort(true); 

A client disconnection will interrupt the execution of the script and possibly damaging data,
eg. non-transitional database query, building a config file, ecc., while in your case it would download a partial file. and you might, or not, care about this.

Answering this old question because this thread is at the top on engine searches for CURL_TIMEOUT .

Источник

Оцените статью