All Projects → reactphp → Http Client

reactphp / Http Client

Licence: mit
[Deprecated] Event-driven, streaming HTTP client for ReactPHP.

Projects that are alternatives of or similar to Http Client

Http
Event-driven, streaming HTTP client and server implementation for ReactPHP.
Stars: ✭ 507 (+122.37%)
Mutual labels:  reactphp, http-client
Fluenthttpclient
A modern async HTTP client for REST APIs. Its fluent interface lets you send an HTTP request and parse the response in one go.
Stars: ✭ 208 (-8.77%)
Mutual labels:  http-client
Rehttp
Package rehttp implements a Go HTTP transport that handles retries.
Stars: ✭ 170 (-25.44%)
Mutual labels:  http-client
Httplug
HTTPlug, the HTTP client abstraction for PHP
Stars: ✭ 2,295 (+906.58%)
Mutual labels:  http-client
Crest
HTTP and REST client for Crystal
Stars: ✭ 174 (-23.68%)
Mutual labels:  http-client
Play Ws
Standalone Play WS, an async HTTP client with fluent API
Stars: ✭ 190 (-16.67%)
Mutual labels:  http-client
Libhv
🔥 比libevent、libuv更易用的国产网络库。A c/c++ network library for developing TCP/UDP/SSL/HTTP/WebSocket client/server.
Stars: ✭ 3,355 (+1371.49%)
Mutual labels:  http-client
Php Console Spinner
Colorful highly configurable spinner for php cli applications (suitable for async apps)
Stars: ✭ 225 (-1.32%)
Mutual labels:  reactphp
Donkey
Modern Clojure HTTP server and client built for ease of use and performance
Stars: ✭ 199 (-12.72%)
Mutual labels:  http-client
Wretch
A tiny wrapper built around fetch with an intuitive syntax. 🍬
Stars: ✭ 2,285 (+902.19%)
Mutual labels:  http-client
Fshttp
A lightweight F# HTTP library.
Stars: ✭ 181 (-20.61%)
Mutual labels:  http-client
Grab
Web Scraping Framework
Stars: ✭ 2,147 (+841.67%)
Mutual labels:  http-client
Ob Http
make http request within org-mode babel
Stars: ✭ 191 (-16.23%)
Mutual labels:  http-client
Resilient.js
Fault tolerant and reactive HTTP client for node.js and browsers
Stars: ✭ 172 (-24.56%)
Mutual labels:  http-client
Aleph
Asynchronous communication for Clojure
Stars: ✭ 2,389 (+947.81%)
Mutual labels:  http-client
Http4s
A minimal, idiomatic Scala interface for HTTP
Stars: ✭ 2,173 (+853.07%)
Mutual labels:  http-client
Hhvmcraft
📦 Minecraft Beta 1.7.3 Server implemented in PHP/HHVM, powered by ReactPHP.
Stars: ✭ 180 (-21.05%)
Mutual labels:  reactphp
Http Kit
http-kit is a minimalist, event-driven, high-performance Clojure HTTP server/client library with WebSocket and asynchronous support
Stars: ✭ 2,234 (+879.82%)
Mutual labels:  http-client
Child Process
Event-driven library for executing child processes with ReactPHP.
Stars: ✭ 225 (-1.32%)
Mutual labels:  reactphp
Http Client
An HTTP client engine, intended as a base layer for more user-friendly packages.
Stars: ✭ 214 (-6.14%)
Mutual labels:  http-client

Deprecation notice

This package has now been migrated over to react/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 for more details.

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.

Deprecated HttpClient

Build Status

Event-driven, streaming HTTP client for ReactPHP.

Table of Contents

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.

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

$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

<?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.

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.

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:

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

Install

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.10

See also the CHANGELOG for details about version upgrades.

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.

Tests

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

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:

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

License

MIT, see LICENSE file.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].