Limit connection time using the Guzzle HTTP client

I use Guzzle to open a list of URLs and get the headers. Some of the URLs take too long to respond and cannot be opened, and I want to ignore them. It will take me up to 20+ seconds before Guzzle throws an exception, and I want to change this and limit the connection time to 2 seconds. I have this code, but it still takes much longer:

<?php include 'vendor/autoload.php'; $start = new \DateTime("now"); $start = $start->format("dmY H:i:s"); echo $start."\n"; $client = new Guzzle\Http\Client(); Guzzle\Http\StaticClient::mount(); try { $request = $client->get('http://takestoolongexample', [], ['connect_timeout' => 2, 'timeout' => 3, 'debug' => true]); $response = $request->send(); var_dump($response->getStatusCode()); } catch (Exception $e) { echo "\n".$e->getMessage()."\n"; } $end = new \DateTime("now"); $end = $end->format("dmY H:i:s"); echo "\n".$end."\n"; ?> 

Here is an example of the result. As you can see, it took 13 seconds.

 $ php test.php 30.12.2013 22:00:07 * getaddrinfo(3) failed for takestoolongexample:80 * Couldn't resolve host 'takestoolongexample' * Closing connection 0 [curl] 6: Couldn't resolve host 'http://takestoolongexample' http://takestoolongexample 30.12.2013 22:00:20 

( http://takestoolongexample was a real url, changed it here)

+6
source share
5 answers

Here's an updated solution to this problem for the Guzzle version (Guzzle 4).

 $request = $client->get(sprintf("%s/noisesize.api", $this->noiseConfig->url), array( 'timeout' => 5, // Response timeout 'connect_timeout' => 5, // Connection timeout )); 

Guzzle\Http\Exception\RequestException Throws Guzzle\Http\Exception\RequestException

The documentation for the latest version is here: Guzzle request parameters - connect_timeout , timeout .

+10
source

The only way I know how to do this in Guzzle is:

 $params = array( 'command.request_options' = array( 'timeout' => 5, 'connect_timeout' => 2 ) ); $client = new Client(); $description = ServiceDescription::factory('/path/to/service/description/file'); $client->setDescription($description); $command = $client->getCommand('commandName', $params); $command->prepare(); $client->execute($command); 

At first glance, Guzzle's documentation seems very good, but I find it poor, confusing, and incomplete. So, it’s hard for me to understand if your code is really correct, and if it should work.

+3
source

A little accuracy, you can also define timeouts in the client constructor

 $client = new Guzzle\Http\Client('', array( 'request.options' => array ( 'timeout' => 6, 'connect_timeout' => 6 ) )); 

It will be valid for all requests made from this client.

+3
source

Your example is correct, but it will always fail.

The error occurs at the cURL level, and not at Guzzle. Before sending an HTTP request (Guzzle job), you need to establish the appropriate IP session (cURL one). In order to get an IP session, a DNS transfer must occur before sending packets.

In your example, DNS resolution is not performed. This happens in cURL code, not Guzzle. Thus, your timeout value will not be used.

If you still have this error with your real URL, you can add a test in front of your buzz request that will check if DNS is resolved. Or you can define the following cURL parameter: CURLOPT_CONNECTTIMEOUT or CURLOPT_CONNECTTIMEOUT_MS (see http://php.net/manual/en/function.curl-setopt.php )

0
source

Set DNS timeout before using guzzle client

 putenv('RES_OPTIONS=retrans:1 retry:1 timeout:1 attempts:1'); //dns resolve params $request = $client->get(sprintf("%s/noisesize.api", $this->noiseConfig->url), array( 'timeout' => 5, // Response timeout 'connect_timeout' => 5, // Connection timeout )); 
0
source

All Articles