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:
( http://takestoolongexample was a real url, changed it here)
source share