Can Guzzle send asynchronous requests?
Yes. You can use the requestAsync
, sendAsync
, getAsync
, headAsync
, putAsync
, postAsync
, deleteAsync
, and patchAsync
methods of a client to send an asynchronous request. The client will return a GuzzleHttp\Promise\PromiseInterface
object. You can chain then
functions off of the promise.
$promise = $client->requestAsync('GET', 'http://httpbin.org/get');
$promise->then(function ($response) {
echo 'Got a response! ' . $response->getStatusCode();
});
You can force an asynchronous response to complete using the wait()
method of the returned promise.
$promise = $client->requestAsync('GET', 'http://httpbin.org/get');
$response = $promise->wait();