How do I use Guzzle 6 to create 5 async requests with the following conditions:
- All requests start at the same time
- I want a 500ms timeout value for all requests. If a request times out I DONT want it to interrupt other requests
- If a request returns non-200 I DONT want it to interrupt other requests.
- All requests are on different domains... (so I'm not sure how that fits in with the
base_uri
setting...
If all 5 requests return 200OK < 500ms then I want to be able to loop through their responses...
BUT, if say 2 of them have non-200 and 1 of them times out (over 500ms), I want to still be able to access the responses for the 2 successful ones.
EDIT So far everything works except timeouts are still raising an exception
Here is what I had so far:
<?php
require __DIR__.'/../vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Promise;
$client = new Client([
'http_errors' => false,
'connect_timeout' => 1.50, //////////////// 0.50
'timeout' => 2.00, //////////////// 1.00
'headers' => [
'User-Agent' => 'Test/1.0'
]
]);
// initiate each request but do not block
$promises = [
'success' => $client->getAsync('https://httpbin.org/get'),
'success' => $client->getAsync('https://httpbin.org/delay/1'),
'failconnecttimeout' => $client->getAsync('https://httpbin.org/delay/2'),
'fail500' => $client->getAsync('https://httpbin.org/status/500'),
];
// wait on all of the requests to complete. Throws a ConnectException if any
// of the requests fail
$results = Promise\unwrap($promises);
// wait for the requests to complete, even if some of them fail
$results = Promise\settle($promises)->wait();
Guzzle provides
fulfilled
andrejected
callabcks in the pool. here I performed a test by your values, read more at Guzzle docs:response
if you want to use your code above you can also pass response status in your $promises, here is an example: