2017 © Pedro Peláez
 

library http-client

Event-driven, streaming HTTP client for ReactPHP

image

react/http-client

Event-driven, streaming HTTP client for ReactPHP

  • Tuesday, April 10, 2018
  • by WyriHaximus
  • Repository
  • 23 Watchers
  • 154 Stars
  • 1,502,298 Installations
  • PHP
  • 41 Dependents
  • 0 Suggesters
  • 58 Forks
  • 14 Open issues
  • 39 Versions
  • 7 % Grown

The README.md

Deprecation notice

This package has now been migrated over to react/http and only exists for BC reasons., (*1)

$ composer require react/http

If you've previously used this package, upgrading may take a moment or two. The new API has been updated to use Promises and PSR-7 message abstractions. This means it's now more powerful and easier to use than ever:, (*2)

// old
$client = new React\HttpClient\Client($loop);
$request = $client->request('GET', 'https://example.com/');
$request->on('response', function ($response) {
    $response->on('data', function ($chunk) {
        echo $chunk;
    });
});
$request->end();

// new
$browser = new React\Http\Browser($loop);
$browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) {
    echo $response->getBody();
});

See react/http for more details., (*3)

The below documentation applies to the last release of this package. Further development will take place in the updated react/http, so you're highly recommended to upgrade as soon as possible., (*4)

Deprecated HttpClient

Build Status, (*5)

Event-driven, streaming HTTP client for ReactPHP., (*6)

Table of Contents, (*7)

Basic usage

Client

The Client is responsible for communicating with HTTP servers, managing the connection state and sending your HTTP requests. It also registers everything with the main EventLoop., (*8)

$loop = React\EventLoop\Factory::create();
$client = new Client($loop);

If you need custom connector settings (DNS resolution, TLS parameters, timeouts, proxy servers etc.), you can explicitly pass a custom instance of the ConnectorInterface:, (*9)

$connector = new \React\Socket\Connector($loop, array(
    'dns' => '127.0.0.1',
    'tcp' => array(
        'bindto' => '192.168.10.1:0'
    ),
    'tls' => array(
        'verify_peer' => false,
        'verify_peer_name' => false
    )
));

$client = new Client($loop, $connector);

The request(string $method, string $uri, array $headers = array(), string $version = '1.0'): Request method can be used to prepare new Request objects., (*10)

The optional $headers parameter can be used to pass additional request headers. You can use an associative array (key=value) or an array for each header value (key=values). The Request will automatically include an appropriate Host, User-Agent: react/alpha and Connection: close header if applicable. You can pass custom header values or use an empty array to omit any of these., (*11)

The Request#write(string $data) method can be used to write data to the request body. Data will be buffered until the underlying connection is established, at which point buffered data will be sent and all further data will be passed to the underlying connection immediately., (*12)

The Request#end(?string $data = null) method can be used to finish sending the request. You may optionally pass a last request body data chunk that will be sent just like a write() call. Calling this method finalizes the outgoing request body (which may be empty). Data will be buffered until the underlying connection is established, at which point buffered data will be sent and all further data will be ignored., (*13)

The Request#close() method can be used to forefully close sending the request. Unlike the end() method, this method discards any buffers and closes the underlying connection if it is already established or cancels the pending connection attempt otherwise., (*14)

Request implements WritableStreamInterface, so a Stream can be piped to it. Interesting events emitted by Request:, (*15)

  • response: The response headers were received from the server and successfully parsed. The first argument is a Response instance.
  • drain: The outgoing buffer drained and the response is ready to accept more data for the next write() call.
  • error: An error occurred, an Exception is passed as first argument. If the response emits an error event, this will also be emitted here.
  • close: The request is closed. If an error occurred, this event will be preceeded by an error event. For a successful response, this will be emitted only once the response emits the close event.

Response implements ReadableStreamInterface. Interesting events emitted by Response:, (*16)

  • data: Passes a chunk of the response body as first argument. When a response encounters a chunked encoded response it will parse it transparently for the user and removing the Transfer-Encoding header.
  • error: An error occurred, an Exception is passed as first argument. This will also be forwarded to the request and emit an error event there.
  • end: The response has been fully received.
  • close: The response is closed. If an error occured, this event will be preceeded by an error event. This will also be forwarded to the request and emit a close event there.

Example

<?php

$loop = React\EventLoop\Factory::create();
$client = new React\HttpClient\Client($loop);

$request = $client->request('GET', 'https://github.com/');
$request->on('response', function ($response) {
    $response->on('data', function ($chunk) {
        echo $chunk;
    });
    $response->on('end', function() {
        echo 'DONE';
    });
});
$request->on('error', function (\Exception $e) {
    echo $e;
});
$request->end();
$loop->run();

See also the examples., (*17)

Advanced Usage

Unix domain sockets

By default, this library supports transport over plaintext TCP/IP and secure TLS connections for the http:// and https:// URI schemes respectively. This library also supports Unix domain sockets (UDS) when explicitly configured., (*18)

In order to use a UDS path, you have to explicitly configure the connector to override the destination URI so that the hostname given in the request URI will no longer be used to establish the connection:, (*19)

$connector = new FixedUriConnector(
    'unix:///var/run/docker.sock',
    new UnixConnector($loop)
);

$client = new Client($loop, $connector);

$request = $client->request('GET', 'http://localhost/info');

See also example #11., (*20)

Install

The recommended way to install this library is through Composer. New to Composer?, (*21)

This will install the latest supported version:, (*22)

$ composer require react/http-client:^0.5.10

See also the CHANGELOG for details about version upgrades., (*23)

This project aims to run on any platform and thus does not require any PHP extensions and supports running on legacy PHP 5.3 through current PHP 7+ and HHVM. It's highly recommended to use PHP 7+ for this project., (*24)

Tests

To run the test suite, you first need to clone this repo and then install all dependencies through Composer:, (*25)

$ composer install

To run the test suite, go to the project root and run:, (*26)

$ php vendor/bin/phpunit

The test suite also contains a number of functional integration tests that send test HTTP requests against the online service http://httpbin.org and thus rely on a stable internet connection. If you do not want to run these, they can simply be skipped like this:, (*27)

$ php vendor/bin/phpunit --exclude-group internet

License

MIT, see LICENSE file., (*28)

The Versions

24/03 2016

0.3.x-dev

0.3.9999999.9999999-dev

Asynchronous HTTP client library.

  Sources   Download

MIT

The Requires

 

http

24/03 2016

v0.3.2

0.3.2.0

Asynchronous HTTP client library.

  Sources   Download

MIT

The Requires

 

http

16/06 2015
15/06 2015
14/05 2015
23/11 2014
01/06 2014

dev-compat-parser-deps

dev-compat-parser-deps

Asynchronous HTTP client library.

  Sources   Download

MIT

The Requires

 

http

02/02 2014

v0.4.0

0.4.0.0

Asynchronous HTTP client library.

  Sources   Download

MIT

The Requires

 

http

21/04 2013

v0.3.1

0.3.1.0

Asynchronous HTTP client library.

  Sources   Download

MIT

The Requires

 

http

14/04 2013

v0.3.0

0.3.0.0

Asynchronous HTTP client library.

  Sources   Download

MIT

The Requires

 

http

26/12 2012

v0.2.6

0.2.6.0

Asynchronous HTTP client library.

  Sources   Download

MIT

The Requires

 

http

26/11 2012

v0.2.5

0.2.5.0

Asynchronous HTTP client library.

  Sources   Download

MIT

The Requires

 

http

05/11 2012

v0.2.3

0.2.3.0

Asynchronous HTTP client library.

  Sources   Download

MIT

The Requires

 

http

28/10 2012

v0.2.2

0.2.2.0

Asynchronous HTTP client library.

  Sources   Download

MIT

The Requires

 

http