Buzz Adapter

An HTTPlug adapter for the Buzz HTTP client.

Installation

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

$ composer require php-http/buzz-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 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 Buzz client, you may pass any listener or configuration parameters to it like:

use Buzz\Browser;
use Buzz\Client\Curl;
use Buzz\Listener\CookieListener;

$browser = new Browser();

$client = new Curl();
$client->setMaxRedirects(0);
$browser->setClient($client);

// Create CookieListener
$listener = new CookieListener();
$browser->addListener($listener);

Then create the adapter:

use Http\Adapter\Buzz\Client as BuzzAdapter;
use Http\Message\MessageFactory\GuzzleMessageFactory;

$adapter = new BuzzAdapter($browser, new GuzzleMessageFactory());

Or if you installed the discovery layer:

use Http\Adapter\Buzz\Client as BuzzAdapter;

$adapter = new BuzzAdapter($browser);

Warning

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

Be Aware

This adapter violates the Liskov substitution principle in a rare edge case. When the adapter is configured to use Buzz’ Curl client, it does not send request bodies for request methods such as GET, HEAD and TRACE. A RequestException will be thrown if this ever happens.

If you need GET request with a body (e.g. for Elasticsearch) you need to use the Buzz FileGetContents client or choose a different HTTPlug client like Guzzle 6.

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.