Skip to content

PSR-18 compatible HTTP client with middleware support

License

Notifications You must be signed in to change notification settings

zelenin/http-client

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

108c4e2 · Jul 6, 2019

History

39 Commits
Oct 31, 2018
Oct 31, 2018
Jul 6, 2019
Jan 11, 2017
Oct 31, 2018
Oct 31, 2018
May 30, 2017

Repository files navigation

HTTP client Build Status Coverage Status

PSR-18 compatible low-level HTTP client with middleware support.

Installation

Composer

The preferred way to install this extension is through Composer.

Either run

php composer.phar require zelenin/http-client "~4.0.0"

or add

"zelenin/http-client": "~4.0.0"

to the require section of your composer.json

Usage

use Zelenin\HttpClient\ClientFactory;
use Zend\Diactoros\Request;
use Zend\Diactoros\Uri;

$client = (new ClientFactory())->create();

$request = new Request(new Uri('https://example.com/'), 'GET');
$response = $client->sendRequest($request);

Full example with middleware stack

use Zelenin\HttpClient\Middleware\Cookie\CookieRequest;
use Zelenin\HttpClient\Middleware\Cookie\CookieResponse;
use Zelenin\HttpClient\Middleware\Cookie\FileStorage;
use Zelenin\HttpClient\Middleware\Deflate;
use Zelenin\HttpClient\Middleware\UserAgent;
use Zelenin\HttpClient\MiddlewareClient;
use Zelenin\HttpClient\RequestConfig;
use Zelenin\HttpClient\Transport\CurlTransport;
use Zend\Diactoros\Request;
use Zend\Diactoros\ResponseFactory;
use Zend\Diactoros\StreamFactory;
use Zend\Diactoros\Uri;

$streamFactory = new StreamFactory();
$responseFactory = new ResponseFactory();

$cookieStorage = new FileStorage('/tmp/http-client/cookies.storage');

$client = new MiddlewareClient([
    new CookieRequest($cookieStorage),
    new UserAgent(sprintf('HttpClient PHP/%s', PHP_VERSION)),
    new Deflate($streamFactory),
    new CookieResponse($cookieStorage),
    new CurlTransport($streamFactory, $responseFactory, new RequestConfig()),
], $responseFactory);

$request = new Request(new Uri('https://example.com/'), 'GET');
$response = $client->sendRequest($request);

Author

Aleksandr Zelenin, e-mail: aleksandr@zelenin.me

About

PSR-18 compatible HTTP client with middleware support

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages