}); $measurementsSubscription = $urls->filter(function ($url) { return substr($url->expanded_url, 0, 36) == 'https://atlas.ripe.net/measurements/'; })->map(function ($url) { return trim(substr($url->expanded_url, 36), '/'); })->flatMap(function ($id) { return \Rx\React\Http::get("https://atlas.ripe.net/api/v1/measurement/{$id}/"); })->map(function ($data) { return json_decode($data); })->subscribeCallback(function ($json) { echo 'Measurement #', $json->msm_id, ' "', $json->description, '" had ', $json->participant_count, ' nodes involved', PHP_EOL; }, function (\Rx\React\HttpResponseException $e) { echo "Error: ", $e->getMessage(), PHP_EOL; }, function () { echo "complete", PHP_EOL; }); $probesSubscription = $urls->filter(function ($url) { return substr($url->expanded_url, 0, 30) == 'https://atlas.ripe.net/probes/'; })->map(function ($url) { return trim(substr($url->expanded_url, 30), '/'); })->flatMap(function ($id) { return \Rx\React\Http::get("https://atlas.ripe.net/api/v1/probe/{$id}/"); })->map(function ($data) { return json_decode($data); })->subscribeCallback(function ($json) { echo 'Probe #', $json->id, ' connected since ' . date('r', $json->status_since), PHP_EOL; }, function (\Exception $e) { echo "Error: ", $e->getMessage(), PHP_EOL; }, function () { echo "complete", PHP_EOL; });
<?php include __DIR__ . '/../../vendor/autoload.php'; $source = \Rx\React\Http::get('https://www.google.com/'); $source->subscribeCallback(function ($data) { echo $data, PHP_EOL; }, function (\Exception $e) { echo $e->getMessage(), PHP_EOL; }, function () { echo "completed", PHP_EOL; });
<?php include __DIR__ . '/../../vendor/autoload.php'; $source = \Rx\React\Http::get('http://download.xs4all.nl/test/100MiB.bin')->streamResults(); $start = time(); $size = 0; $source->subscribeCallback(function ($data) use(&$size) { $size += strlen($data); echo "[1A", 'Downloaded size: ', number_format($size / 1024 / 1024, 2, '.', ''), 'MB', PHP_EOL; }, function (\Exception $e) { echo $e->getMessage(); }, function () use(&$size, $start) { $end = time(); $duration = $end - $start; echo round($size / 1024 / 1024, 2), 'MB downloaded in ', $duration, ' seconds at ', round($size / $duration / 1024 / 1024, 2), 'MB/s', PHP_EOL; });
<?php include __DIR__ . '/../../vendor/autoload.php'; $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->subscribeCallback(function ($data) { echo "Got Image: ", array_keys($data)[0], PHP_EOL; }, function (\Exception $e) { echo $e->getMessage(), PHP_EOL; }, function () { echo "completed", PHP_EOL; });
<?php //This example requires https://github.com/RxPHP/RxHttp require __DIR__ . '/../vendor/autoload.php'; $terms = ["rxphp", "php", "make php great again"]; $search = \Rx\Observable::fromArray($terms)->map(function ($term) { return urlencode($term); })->flatMap(function ($term) { return \Rx\React\Http::get("http://www.google.com/search?q={$term}")->map(function ($result) use($term) { return ["term" => $term, "page" => $result]; }); }); $generator = \Rx\await($search); echo "BLOCKING", PHP_EOL; foreach ($generator as $item) { echo "Result page for: {$item['term']}", PHP_EOL, $item['page']; } echo "DONE";