PHPackages                             react/http-client - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. react/http-client

Abandoned → [react/http](/?search=react%2Fhttp)Library[HTTP &amp; Networking](/categories/http)

react/http-client
=================

Event-driven, streaming HTTP client for ReactPHP

v0.5.11(5y ago)2287.2M↓30.3%61[1 issues](https://github.com/reactphp/http-client/issues)20MITPHPPHP &gt;=5.3.0

Since Oct 28Pushed 3y ago19 watchersCompare

[ Source](https://github.com/reactphp/http-client)[ Packagist](https://packagist.org/packages/react/http-client)[ GitHub Sponsors](https://github.com/WyriHaximus)[ GitHub Sponsors](https://github.com/clue)[ RSS](/packages/react-http-client/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (9)Versions (39)Used By (20)

Deprecation notice
==================

[](#deprecation-notice)

This package has now been migrated over to [react/http](https://github.com/reactphp/http)and only exists for BC reasons.

```
$ 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:

```
// 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](https://github.com/reactphp/http#client-usage) for more details.

The below documentation applies to the last release of this package. Further development will take place in the updated [react/http](https://github.com/reactphp/http), so you're highly recommended to upgrade as soon as possible.

Deprecated HttpClient
=====================

[](#deprecated-httpclient)

[![Build Status](https://camo.githubusercontent.com/0acdf04e28f9270643150fa5ab2432876ffc64cfeed02453b60e8a8078dab643/68747470733a2f2f7472617669732d63692e6f72672f72656163747068702f687474702d636c69656e742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/reactphp/http-client)

Event-driven, streaming HTTP client for [ReactPHP](https://reactphp.org).

**Table of Contents**

- [Basic usage](#basic-usage)
    - [Client](#client)
    - [Example](#example)
- [Advanced usage](#advanced-usage)
    - [Unix domain sockets](#unix-domain-sockets)
- [Install](#install)
- [Tests](#tests)
- [License](#license)

Basic usage
-----------

[](#basic-usage)

### Client

[](#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`](https://github.com/reactphp/event-loop#usage).

```
$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`](https://github.com/reactphp/socket#connectorinterface):

```
$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.

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 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:

- `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

[](#example)

```
