PHP code example of rx / http

1. Go to this page and download the library: Download rx/http library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rx / http example snippets


    
$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;
    }
);
    

    
$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;
    }
);
    


$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;
    }
);