HTTP Client

This library supports almost all available HTTP clients. There are several ways to configure the HTTP client that should be used.

If not passed to the Firstred\PostNL\PostNL constructor or set with the Firstred\PostNL\PostNL::setHttpClient method, the library will automatically check if HTTP clients are available in the following order:

  1. Guzzle client

  2. HTTPlug client, an Asynchronous HTTP client, PSR-18 HTTP clients

  3. Built-in cURL client

Setting the HTTP client manually

By default the library will use cURL or Guzzle when available. You can always switch HTTP clients as follows:

$postnl = new PostNL(...);
$postnl->setHttpClient(\Firstred\PostNL\HttpClient\CurlClient::getInstance());

You can create a custom HTTP Client by implementing the \Firstred\PostNL\HttpClient\ClientInterface interface.

Setting the HTTP client with HTTPlug

Using auto-discovery with HTTPlug.

The following packages are required if you want to use HTTPlug:

composer require php-http/discovery php-http/httplug php-http/message-factory psr/http-factory

HTTPlug + Symfony HTTP Client example

This package already requires a Message factory and HTTP factory implementation (guzzlehttp/psr7), so all we need to do is to install the client and its requirements according to the official Symfony documentation:

composer require symfony/http-client

If you haven’t installed Guzzle itself, this library should now auto-detect the Symfony HTTP Client and use it through the HTTPlug bridge.

If you have installed Guzzle already, then you can still configure the PostNL library to use the Symfony HTTP Client. To do this, pass the new client to the Firstred\PostNL\PostNL constructor or Firstred\PostNL\PostNL::setHttpClient method. Example:

use Symfony\Component\HttpClient\HttplugClient;
use Firstred\PostNL\HttpClient\HTTPlugClient;

$symfonyClient = new HttplugClient();

$httpPlugBridgeClient = new HTTPlugClient($symfonyClient);

$postnl = new PostNL($customer, null, $httpClient);

Note

A list of HTTP Client supported by HTTPlug can be found on the following page: