Press "Enter" to skip to content

php 通过网络获取的json数据最好先转换为utf8编码 | json_last_error 返回编解码失败信息

Make sure you pass in utf8 content, or json_decode may error out and just return a null value.  For a particular web service I was using, I had to do the following:

<?php
$contents 
file_get_contents($url);
$contents utf8_encode($contents);
$results json_decode($contents);
?>

Hope this helps!

//bom头去除

function removeBOM($data) {

if (0 === strpos(bin2hex($data), ‘efbbbf’)) {

return substr($data, 3);

}

return $data;

}

作者:隐藏之海
链接:https://www.jianshu.com/p/e924cb9f05b3
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

 

发表评论