dongsheng1238 2018-03-09 12:04
浏览 51

如何在php中检测来自json响应的charset

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;
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求daily translation(DT)偏差订正方法的代码
    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 牛顿斯科特系数表表示
    • ¥15 arduino 步进电机
    • ¥20 程序进入HardFault_Handler
    • ¥15 关于#python#的问题:自动化测试