1

I'm trying to receive a JSON Response from a webservice using GuzzleHttp Client but I'm unable to decode the received json data with json_decode which returns null.

I noticed that on the response headers the Content-Type is application/json; charset=windows-1253

If I convert the json string using iconv from windows-1253 to utf-8 it decodes OK.

So my question is how can I reliably determine which charset the response uses in order to convert correctly and then parse it with json_decode?

For example a function

getCharsetFromResponse(\GuzzleHttp\Psr7\Response $response){
 //detect charset here
 .....
 return $charset;
}
gabtzi
  • 573
  • 3
  • 8
  • 24
  • you gotta do the exact thing that you noticed, check response headers using php, $http_response_header contains associative array of headers get charset from content-type http://php.net/manual/en/reserved.variables.httpresponseheader.php do you find this useful ? – moghya Mar 09 '18 at 12:09
  • I was thinking to do that but couldn't figure out how to best extract it from the header. For example preg_split on "/charset=/" and see if I get a match? It didn't seem so reliable so I'm wondering if there's a better way I'm missing – gabtzi Mar 09 '18 at 12:17
  • maybe this will help ? https://stackoverflow.com/questions/12433958/how-to-parse-response-headers-in-php – moghya Mar 09 '18 at 12:24
  • You could also file a bug report with the service. JSON is supposed to be one of the Unicode encodings and charset should not be needed. – Tom Blodget Mar 11 '18 at 02:01

0 Answers0