Http Client for RxPHP
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
examples/http Updated React Http to v 0.5 Oct 8, 2017
src Updated React Http to v 0.5 Oct 8, 2017
tests
.gitignore Initial Commit Jan 27, 2016
.travis.yml Update dependencies and types for php 7 Mar 21, 2017
LICENSE Initial Commit Jan 27, 2016
README.md
composer.json Forward compatibility with voryx/event-loop 3.0 and while supporting 2.0 Apr 18, 2018
phpunit.xml Initial Commit Jan 27, 2016

README.md

Http Client for RxPHP

This library is a RxPHP wrapper for the ReactPHP's Http-client library. It allows you to make asynchronous http calls and emit the results through an RxPHP observable.

It uses the Voryx event-loop which behaves like the Javascript event-loop. ie. You don't need to start it.

##Installation

Install dependencies using composer

  $ php composer.phar require "rx/http"      

Usage

Get

    
$source = \Rx\React\Http::get('https://www.example.com/');

$source->subscribe(
    function ($data) {
        echo $data, PHP_EOL;
    },
    function (\Exception $e) {
        echo $e->getMessage(), PHP_EOL;
    },
    function () {
        echo "completed", PHP_EOL;
    }
);
    

Post

    
$postData = json_encode(["test" => "data"]);
$headers  = ['Content-Type' => 'application/json'];

$source = \Rx\React\Http::post('https://www.example.com/', $postData, $headers);

$source->subscribe(
    function ($data) {
        echo $data, PHP_EOL;
    },
    function (\Exception $e) {
        echo $e->getMessage(), PHP_EOL;
    },
    function () {
        echo "completed", PHP_EOL;
    }
);
    

Multiple Asynchronous Requests


$imageTypes = ["png", "jpeg", "webp"];

$images = \Rx\Observable::fromArray($imageTypes)
    ->flatMap(function ($type) {
        return \Rx\React\Http::get("http://httpbin.org/image/{$type}")->map(function ($image) use ($type) {
            return [$type => $image];
        });
    });

$images->subscribe(
    function ($data) {
        echo "Got Image: ", array_keys($data)[0], PHP_EOL;
    },
    function (\Exception $e) {
        echo $e->getMessage(), PHP_EOL;
    },
    function () {
        echo "completed", PHP_EOL;
    }
);

For more information, see the examples.