2017 © Pedro Peláez
 

library http-client

Berlioz HTTP Client

image

berlioz/http-client

Berlioz HTTP Client

  • Tuesday, March 20, 2018
  • by ElGigi
  • Repository
  • 1 Watchers
  • 1 Stars
  • 362 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 1 Open issues
  • 1 Versions
  • 123 % Grown

The README.md

Berlioz HTTP Client

Latest Version Software license Build Status Quality Grade Total Downloads, (*1)

Berlioz HTTP Client is a PHP library to request HTTP server with continuous navigation, including cookies, sessions... Implements PSR-18 (HTTP Client), PSR-7 (HTTP message interfaces) and PSR-17 (HTTP Factories) standards., (*2)

Installation

Composer

You can install Berlioz HTTP Client with Composer, it's the recommended installation., (*3)

$ composer require berlioz/http-client

Dependencies

  • PHP ^8.0
  • PHP libraries:
    • curl
    • mbstring
    • zlib
  • Packages:
    • berlioz/http-message
    • elgigi/har-parser
    • psr/http-client
    • psr/log

Usage

Requests

With RequestInterface

You can construct your own request object whose implements RequestInterface interface (PSR-7)., (*4)

use Berlioz\Http\Client\Client;
use Berlioz\Http\Message\Request;

/** @var \Psr\Http\Message\RequestInterface $request */
$request = new Request(...);

$client = new Client();
$response = $client->sendRequest($request);

print $response->getBody();

Get/Post/Patch/Put/Delete/Options/Head/Connect/Trace

Methods are available to do request with defined HTTP method:, (*5)

  • Client::get(...)
  • Client::post(...)
  • Client::patch(...)
  • Client::put(...)
  • Client::delete(...)
  • Client::options(...)
  • Client::head(...)
  • Client::connect(...)
  • Client::trace(...)

Example with Client::get():, (*6)

use Berlioz\Http\Client\Client;

$client = new Client();
$response = $client->get('https://getberlioz.com');

print $response->getBody();

You also can pass HTTP method in argument to Client::request(...) method:, (*7)

use Berlioz\Http\Client\Client;

$client = new Client();
$response = $client->request('get', 'https://getberlioz.com');

print $response->getBody();

Each method accept an array of options with $options argument. List of options:, (*8)

  • baseUri (string): Base of URI if not given in requests
  • followLocation (bool): Follow redirections (default: true)
  • followLocationLimit (int): Limit location to follow
  • sleepTime (int): Sleep time between requests (ms) (default: 0)
  • logFile (string): Log file name (only file name, not path)
  • exceptions (bool): Throw exceptions on error (default: true)
  • cookies (null|false|CookiesManager): NULL: to use default cookie manager; FALSE: to not use cookies; a CookieManager object to use
  • callback (callable): Callback after each request
  • headers (array): Default headers

Options passed in argument replace default options of client., (*9)

Session

The session is accessible with method Client::getSession()., (*10)

History

The browsing history is saved in the session. If you serialize the object Session, the history is preserve., (*11)

The method Session::getHistory() returns an History object:, (*12)

use Berlioz\Http\Client\Client;

$client = new Client();
$history = $client->getSession()->getHistory();

Cookies

A cookie manager is available to manage cookies of session and between requests. The manager is available with Session::getCookies() method., (*13)

If you serialize the object Session, the cookies are preserves., (*14)

HAR file

HAR file of session is accessible with method Session::getHar()., (*15)

If you serialize the object Session, the HAR is preserved., (*16)

Refers to the documentation of elgigi/har-parser library: https://github.com/ElGigi/HarParser, (*17)

Adapters

Usage

Default adapter used by library is CurlAdapter (if CURL extension is installed), else the StreamAdapter is used., (*18)

You can specify adapters to the client constructor, with argument adapter:, (*19)

use Berlioz\Http\Client\Client;
use Berlioz\Http\Client\Adapter;

$client = new Client(adapter: new Adapter\CurlAdapter(), adapter: new Adapter\StreamAdapter());

The first specified adapter is the default adapter., (*20)

If you want force an adapter for a request, you can pass is name in the request options:, (*21)

use Berlioz\Http\Client\Client;
use Berlioz\Http\Client\Adapter;

$client = new Client(adapter: new Adapter\CurlAdapter(), adapter: new Adapter\StreamAdapter());
$client->get('https://getberlioz.com', options: ['adapter' => 'stream']);

List

List of adapters:, (*22)

  • curl: Berlioz\Http\Client\Adapter\CurlAdapter
  • stream: Berlioz\Http\Client\Adapter\StreamAdapter
  • har: Berlioz\Http\Client\Adapter\HarAdapter

HarAdapter

The HarAdapter is specially made to simulate a navigation, coming from a desktop browser for example., (*23)

It's very useful for test units. You only need to store your cleaned HAR file into your repository to launch tests with simulated HTTP dialogs., (*24)

use Berlioz\Http\Client\Client;
use Berlioz\Http\Client\Adapter\HarAdapter;
use ElGigi\HarParser\Parser;

// Create HAR object from library `elgigi/har-parser`
$har = (new Parser())->parse('/path/of/my/file.har', contentIsFile: true);

$client = new Client(adapter: new HarAdapter(har: $har));
$client->get('https://getberlioz.com'); // Get response from HAR object, without making an HTTP request

Har adapter accept an option strict (default: false) to force the way of navigation., (*25)

Create an adapter

You can create an adapter for your project. You must implement the interface Berlioz\Http\Client\Adapter\AdapterInterface., (*26)

The Versions

20/03 2018

dev-develop

dev-develop http://www.berlioz-framework.com

Berlioz HTTP Client

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ronan Giron