Parsing ndjson stream API’s with PHP

Readable Streams are API responses that are broken into small chunks and sent to the client. As the client should be able to parse the chunks of received data as they arrive the conventional JSON responses are not ideal to be used for streams.

So In most cases, streamable API endpoints should send the data line by line. ndjson is a good option for sending JSON structured data in a streamable way. if you have not heard about ndjson before, it stands for new line delimited json.

First thing first we should call the API via Guzzle, by default guzzle uses PSR-7 stream object to represent the responses.

Here is a sample call to a streaming API that returns a stream response object:

<?php
$stream = $guzzleClient->get("http://stream-api.com/api",
    [
        'stream' => true,
    ]
)->getBody();

Now that we have stored the stream object in $stream variable we can use the “eof” method to verify we have not reached the end of the streamed data. by utilizing the helper readline we will read the output line by line. which should give us a single JSON object.

while (! $stream->eof()) {
  $line = Utils::readLine($stream);    
}

as simple as that we can read chunked received data from a streaming API with php.

Leave a comment

Leave a Reply