1. Go to this page and download the library: Download react/promise-stream 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/ */
react / promise-stream example snippets
React\Promise\Stream\buffer(…);
use function React\Promise\Stream\buffer;
buffer(…);
$stream = accessSomeToLargeStream();
React\Promise\Stream\buffer($stream, 1024)->then(function ($contents) {
var_dump(json_decode($contents));
}, function ($error) {
// Reaching here when the stream buffer goes above the max size,
// in this example that is 1024 bytes,
// or when the stream emits an error.
});
$stream = accessSomeJsonStream();
React\Promise\Stream\first($stream)->then(function (string $chunk) {
echo 'The first chunk arrived: ' . $chunk;
});
$stream = accessSomeJsonStream();
React\Promise\Stream\all($stream)->then(function (array $chunks) {
echo 'The stream consists of ' . count($chunks) . ' chunk(s)';
});