9951 explained code solutions for 126 technologies


php-guzzleHow to use Promises with PHP Guzzle (with an example)?


Promises are a way to handle asynchronous operations in PHP Guzzle. Promises allow you to write code that is more readable and easier to maintain.

Example code

$client = new \GuzzleHttp\Client();
$promise = $client->getAsync('http://www.example.com');
$promise->then(
    function ($response) {
        echo $response->getStatusCode();
    },
    function ($exception) {
        echo $exception->getMessage();
    }
);

Output example

200

Code explanation

  • $client = new \GuzzleHttp\Client(); - creates a new Guzzle client
  • $promise = $client->getAsync('http://www.example.com'); - creates a promise for an asynchronous GET request
  • $promise->then(...) - registers a callback to be executed when the promise is fulfilled
  • function ($response) { ... } - callback to be executed when the promise is fulfilled
  • function ($exception) { ... } - callback to be executed when the promise is rejected

Helpful links

Edit this code on GitHub