PHP code example of clue / ndjson-react

1. Go to this page and download the library: Download clue/ndjson-react 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/ */

    

clue / ndjson-react example snippets


$stdin = new React\Stream\ReadableResourceStream(STDIN);

$ndjson = new Clue\React\NDJson\Decoder($stdin);

$ndjson->on('data', function ($data) {
    // $data is a parsed element from the JSON stream
    // line 1: $data = (object)array('name' => 'test', 'active' => true);
    // line 2: $data = (object)array('name' => 'hello wörld', 'active' => true);
    var_dump($data);
});

$ndjson = new Clue\React\NDJson\Decoder($stdin, true);

$ndjson->on('data', function ($data) {
    // JSON objects will be emitted as assoc arrays now
});

$ndjson = new Clue\React\NDJson\Decoder($stdin, false, 512, 0, 64 * 1024);

$ndjson->on('error', function (Exception $error) {
    // an error occurred, stream will close next
});

$ndjson->on('end', function () {
    // stream successfully ended, stream will close next
});

$ndjson->on('close', function () {
    // stream closed
    // possibly after an "end" event or due to an "error" event
});

$ndjson->close();

$ndjson->pipe($logger);

$stdout = new React\Stream\WritableResourceStream(STDOUT);

$ndjson = new Clue\React\NDJson\Encoder($stdout);

$ndjson->write(array('name' => 'test', 'active' => true));
$ndjson->write(array('name' => 'hello wörld', 'active' => true));

$ndjson = new Clue\React\NDJson\Encoder($stdout, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

$ndjson->write('hello wörld');

$ndjson->on('error', function (Exception $error) {
    // an error occurred, stream will close next
});

$ndjson->on('close', function () {
    // stream closed
    // possibly after an "end" event or due to an "error" event
});

$ndjson->end();

$ndjson->close();