Reputation: 11
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!
Upvotes: 0
Views: 160
Reputation: 11
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
Upvotes: 1