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!
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 datai 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.