Event-driven, streaming HTTP client for ReactPHP
Table of Contents
The request(string $method, string $uri, array $headers = array(), string $version = '1.0'): Request
method can be used to prepare new Request objects.
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.
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.
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.
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.
Request implements WritableStreamInterface, so a Stream can be piped to it. Interesting events emitted by Request:
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 nextwrite()
call.error
: An error occurred, anException
is passed as first argument. If the response emits anerror
event, this will also be emitted here.close
: The request is closed. If an error occurred, this event will be preceeded by anerror
event. For a successful response, this will be emitted only once the response emits theclose
event.
Response implements ReadableStreamInterface. Interesting events emitted by Response:
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 theTransfer-Encoding
header.error
: An error occurred, anException
is passed as first argument. This will also be forwarded to the request and emit anerror
event there.end
: The response has been fully received.close
: The response is closed. If an error occured, this event will be preceeded by anerror
event. This will also be forwarded to the request and emit aclose
event there.
<?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.
The recommended way to install this library is through Composer. New to Composer?
This will install the latest supported version:
$ composer require react/http-client:^0.5.2
More details about version upgrades can be found in the CHANGELOG.
To run the test suite, you first need to clone this repo and then install all dependencies through Composer:
$ composer install
To run the test suite, go to the project root and run:
$ php vendor/bin/phpunit
MIT, see LICENSE file.