0

sorry if my question is silly but there's something I'm not getting. I'm using Guzzle in a PHP process to make an API call.

$response = $this->client->request('GET', 'droits_acces', [
            'stream' => true,
            'headers' => [
                'Content-Type' => 'application/json',
                'Accept' => 'application/x-ndjson',
            ]
        ]);

But I don't understand how to interpret the response. I found this library for interpret ndjson: https://github.com/sunaoka/ndjson

My logic was to take the response content, store it in a temporary file, and then read it using the library.

But when I use $response->getBody()->getContents() I get a response with a lot of line breaks. However, in NDJSON, the line breaks represent the separation between one object and another, if I understand correctly.

[edit] Here is a screenshot of the response I get when I use $response->getBody()->getContents(). Screnn of body response

I tried reading it with the ndjson library using the example in the documentation (with spaces only between the objects, and it works). The only difference between the two is the spaces...

When I remove the "extra" line breaks to have a file like the one below, it works well.

In this exemple: $content not working, but $content2 working

public function getAccessRights()
{
    $temp_file = tempnam(sys_get_temp_dir(), 'TMP_');
    $temp_file_with_extension = $temp_file . '.ndjson';
    rename($temp_file, $temp_file_with_extension);
    $file_handle = fopen($temp_file_with_extension, 'a+');

    // Content not working
    $content = <<<END
                {
                    "name": "John",
                    "active": true
                }
                {
                    "name": "Doe",
                    "active": true
                }

                END;

    // Content Working
    $content2 = <<<END
                {"name": "John", "active": true}
                {"name": "Doe", "active": true}

                END;

    fwrite($file_handle, $content);

    fclose($file_handle);


    $ndjson = new NDJSON($temp_file_with_extension);

    $result = [];

    while ($json = $ndjson->readline()) {
        $result[] = $json;
    }
    unlink($temp_file);
    dd($result);
}

Maybe i think my response NDJSON is not valid, I’m a bit confused right now. If anyone has any tips or suggestions on how to proceed, that would be really helpful =D

I hope I was clear enough in explaining my issue. Thank you in advance!

12
  • when I use $response->getBody()->getContents() I get a response with a lot of line breaks. However, in NDJSON, the line breaks represent the separation between one object and another, if I understand correctly...yep, that all sounds fine. So what's the problem? Feed that response to the NDJSON library and see if it can parse it. You might have to save it to a file first, since it looks like that simple class can only read from files on disk, rather than an in-memory string of data
    – ADyson
    Commented Jul 3 at 15:51
  • Or are you saying there are more line breaks in the JSON response than you expected to see? It's not very clear - and you didn't provide an example of what you mean! Possibly the JSON in your response is formatted with extra line breaks for some reason? We can't really tell, you need to show us an example of the raw output you want to process. See also How to Ask and how to make a minimal reproducible example of your issue, and remember we can't see your screen. You can edit your post. Thanks.
    – ADyson
    Commented Jul 3 at 15:53
  • I edit my post ;) I hope I was clear enough in explaining my issue.
    – Florian B
    Commented Jul 4 at 8:32
  • Thanks. i think my response NDJSON is not valid...yes, it looks like that to me. You should speak to the maintainers of the API first of all.
    – ADyson
    Commented Jul 4 at 8:37
  • Can the API response be read differently using Guzzle?
    – Florian B
    Commented Jul 4 at 8:48

1 Answer 1

1
private function formatJsonString($input)
{
    $input = preg_replace('~[\r\n]+~', '', $input);
    $input = str_replace('}', "}\n", $input);

    return $input;
}

I found this solution, a function that formats the response to remove the extra spaces.

If anyone has other ideas, I’m open to them. =D

Not the answer you're looking for? Browse other questions tagged or ask your own question.