Guzzle5 Adapter

An HTTPlug adapter for the Guzzle 5 HTTP client.

Installation

To install the Guzzle adapter, which will also install Guzzle itself (if it was not yet included in your project), run:

$ composer require php-http/guzzle5-adapter

This client does not come with a PSR-7 implementation out of the box, so you have to install one as well (for example Guzzle PSR-7):

$ composer require guzzlehttp/psr7

In order to provide full interoperability, message implementations are accessed through factories. Message factories for Laminas Diactoros (and its abandoned predecessor Zend Diactoros), Guzzle PSR-7 and Slim Framework are available in the message component:

$ composer require php-http/message

Alternatively you can install the discovery layer to be able to automatically find installed resources, like factories:

$ composer require php-http/discovery

Usage

Begin by creating a Guzzle client, passing any configuration parameters you like:

use GuzzleHttp\Client as GuzzleClient;

$config = [
    // Config params
];
$guzzle = new GuzzleClient($config);

Then create the adapter:

use Http\Adapter\Guzzle5\Client as GuzzleAdapter;
use Http\Message\MessageFactory\GuzzleMessageFactory;

$adapter = new GuzzleAdapter($guzzle, new GuzzleMessageFactory());

Or if you installed the discovery layer:

use Http\Adapter\Guzzle5\Client as GuzzleAdapter;

$adapter = new GuzzleAdapter($guzzle);

Warning

The message factory parameter is mandatory if the discovery layer is not installed.

Further reading

  • Use plugins to customize the way HTTP requests are sent and responses processed by following redirects, adding Authentication or Cookie headers and more.

  • Learn how you can decouple your code from any PSR-7 implementation by using the HTTP factories.