php-guzzleHow to make an asynchronous send with PHP Guzzle?
Using PHP Guzzle, you can make asynchronous requests with the requestAsync()
method.
Example code
$client = new GuzzleHttp\Client();
$promise = $client->requestAsync('GET', 'http://www.example.com');
$promise->then(
function ($response) {
echo 'I completed! ' . $response->getBody();
},
function ($exception) {
echo 'I failed! ' . $exception->getMessage();
}
);
Output example
I completed! <html>...</html>
Code explanation
$client = new GuzzleHttp\Client();
: creates a new Guzzle client.$promise = $client->requestAsync('GET', 'http://www.example.com');
: makes an asynchronous request to the specified URL.$promise->then(...)
: sets up a callback to be executed when the request is completed.echo 'I completed! ' . $response->getBody();
: prints the response body when the request is successful.echo 'I failed! ' . $exception->getMessage();
: prints the exception message when the request fails.
Helpful links
More of Php Guzzle
- How to use PHP Guzzle to make a batch request?
- How to send multipart requests with Guzzle in PHP?
- How to update PHP Guzzle?
- How to post form data with PHP Guzzle?
- How to troubleshoot cURL error 60 with Guzzle in PHP?
- What version of PHP is required for Guzzle?
- How to use a proxy with Guzzle in PHP?
- PHP Guzzle get request example
- How to use cookies with Guzzle in PHP?
- How to set a user agent in PHP Guzzle?
See more codes...