use GuzzleHttp\Client; $client = new Client(); $promises = [ 'request1' => $client->getAsync('http://example.com/api/endpoint1'), 'request2' => $client->getAsync('http://example.com/api/endpoint2'), ]; $results = \GuzzleHttp\Promise\Utils::all($promises); $data1 = $results['request1']->getBody()->getContents(); $data2 = $results['request2']->getBody()->getContents();
use GuzzleHttp\Client; use GuzzleHttp\Promise\PromiseInterface; $client = new Client(); $promises = [ 'request1' => $client->getAsync('http://example.com/api/endpoint1'), 'request2' => $client->getAsync('http://example.com/api/endpoint2'), ]; $responseData = []; \GuzzleHttp\Promise\each($promises, function (PromiseInterface $promise, $key) use (&$responseData) { $response = $promise->wait(); $responseData[$key] = $response->getBody()->getContents(); });In this example, we use the each method of the PromiseUtils to process each request as soon as it completes. We pass a callback function that extracts the data from the response and stores it in the $responseData array using the request key as the index. In conclusion, the GuzzleHttp Client getAsync method is a very useful tool for making asynchronous HTTP requests in PHP. It allows you to perform multiple requests at the same time and wait for them to complete without blocking the main thread. The GuzzleHttp package library provides a reliable and flexible interface for building advanced HTTP clients in PHP.