concurrent requests guzzle

Solutions on MaxInterview for concurrent requests guzzle by the best coders in the world

showing results for - "concurrent requests guzzle"
Juan Martín
11 May 2020
1use GuzzleHttp\Client;
2use GuzzleHttp\Promise;
3
4$client = new Client(['base_uri' => 'http://httpbin.org/']);
5
6// Initiate each request but do not block
7$promises = [
8    'image' => $client->getAsync('/image'),
9    'png'   => $client->getAsync('/image/png'),
10    'jpeg'  => $client->getAsync('/image/jpeg'),
11    'webp'  => $client->getAsync('/image/webp')
12];
13
14// Wait for the requests to complete; throws a ConnectException
15// if any of the requests fail
16$responses = Promise\unwrap($promises);
17
18// You can access each response using the key of the promise
19echo $responses['image']->getHeader('Content-Length')[0];
20echo $responses['png']->getHeader('Content-Length')[0];
21
22// Wait for the requests to complete, even if some of them fail
23$responses = Promise\settle($promises)->wait();
24
25// Values returned above are wrapped in an array with 2 keys: "state" (either fulfilled or rejected) and "value" (contains the response)
26echo $responses['image']['state']; // returns "fulfilled"
27echo $responses['image']['value']->getHeader('Content-Length')[0];
28echo $responses['png']['value']->getHeader('Content-Length')[0];
29
similar questions
queries leading to this page
concurrent requests guzzle