2017 © Pedro Peláez
 

library http-client-react

image

legionth/http-client-react

  • Friday, August 18, 2017
  • by legionth
  • Repository
  • 1 Watchers
  • 1 Stars
  • 4 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

legionth/http-client-react

HTTP client written in PHP on top of ReactPHP., (*1)

Table of Contents * Usage * Request body * Install * License, (*2)

Usage

The client is responsible to send HTTP requests and receive the HTTP response from the server., (*3)

This library uses PSR-7 objects to make it easier to handle the HTTP-Messsages., (*4)

$uri = 'tcp://httpbin.org:80';
$request = new Request('GET', 'http://httpbin.org');

$promise = $client->request($uri, $request);

It could take some time until the response is transferred from the server to the client. For this reason the request-method will return a ReactPHP promise., (*5)

The promise will result in a PSR-7 response object, (*6)

$promise = $client->request($uri, $request);
$promise->then(
    function (\Psr\Http\Message\ResponseInterface $response) {
        echo 'Successfully received a response from the server:' . PHP_EOL;
        echo RingCentral\Psr7\str($response);
    },
    function (\Exception $exception) {
        echo $exception->getMessage() . PHP_EOL;
    }
);

The body of the response will always be an asynchronous ReactPHP Stream., (*7)

$promise = $client->request($uri, $request);
$promise->then(
    function (ResponseInterface $response) {
        echo 'Successfully received a response from the server:' . PHP_EOL;
        echo RingCentral\Psr7\str($response);

        $body = $response->getBody();
        $body->on('data', function ($data) {
            echo "Body-Data: " . $data . PHP_EOL;
        });

        $body->on('end', function () {
            exit(0);
        });
    },
    function (\Exception $exception) {
        echo $exception->getMessage() . PHP_EOL;
    }
);

The end-Event will be emmitted when the complete body of the HTTP response has been transferred to the client. In the example above it will exit the current script., (*8)

Request body

You can add also add a ReactPHP Stream as the request body to stream data with it. The body will always be transferred chunked encoded if you use this method, any header like Content-Length or other Transfer-Encoding headers will be replaced., (*9)

$stream = new ReadableStream();

$timer = $loop->addPeriodicTimer(0.5, function () use ($stream) {
    $stream->emit('data', array(microtime(true) . PHP_EOL));
});

$loop->addTimer(5, function() use ($loop, $timer, $stream) {
    $loop->cancelTimer($timer);
    $stream->emit('end');
});

$request = new Request(
    'POST',
    'http://127.0.0.1:10000',
    array(
        'Host' => '127.0.0.1',
        'Content-Type' => 'text/plain'
    ),
    $stream
);
$promise = $client->request($uri, $request);

This example will transfer every 0.5 seconds a chunked encoded data to the server. The transmission of the body will end after 5 seconds., (*10)

Checkout the examples folder to try it yourself., (*11)

Install

New to Composer?, (*12)

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

$ composer require legionth/http-client-react:^0.1

License

MIT, (*14)

The Versions