作者shadowjohn (某人睡醒就发文)
看板PHP
标题Re: [请益] JSON编码"{"编码有问题
时间Mon Oct 27 17:28:44 2014
※ 引述《wild0921 (我要幸福)》之铭言:
: ※ 引述《wild0921 (我要幸福)》之铭言:
: : 有用nodepad++检查过, 档案是utf-8 无Bom档头
: : 产json那一个档案也是utf-8 无Bom档头
: : A.php 网页内容(捞json的网页):
: : $ch = curl_init();
: : curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
: : curl_setopt($ch, CURLOPT_URL, $str_url);
: : curl_setopt($ch, CURLOPT_POST, true); // 启用POST
: : curl_setopt($ch, CURLOPT_POSTFIELDS, $_GET);
: : curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
: : curl_setopt($ch, CURLOPT_USERPWD, '帐号:密码');
: : curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
: : curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
: : $output = curl_exec($ch);
: : B.php 网页内容(产json string 的网页)
: : die(json_encode($array));
: : A.php的部分code是我用curl捞资料的方式,
: : 不过那边json_decode($output); dump 出来是null,
: : 检查之後 原因是json格式有问题,
: : 我把所有程式码, 产新的档案在重新上传,
: : 有改善的是在B.php 那一页dump出来, string检查结果格式是 通过 的
: : 但是在A.php var_dump($output); 贴到检查json网页
: : 格式就会错误, 死在第一个大括号
: : 所以想要请问有没有人遇到过一样的状况, 然後提供一下解决方法?
: json_string:贴不上去, 所以给网址
: http://wildhuang.blogspot.tw/p/jsonstring.html
: 我用来检查json string的网址:
: http://www.freeformatter.com/json-formatter.html
: 以上
function removeBOM($str=""){
if(substr($str, 0,3) == pack("CCC",0xef,0xbb,0xbf)) {
$str=substr($str, 3);
}
return $str;
}
反正你正 json 先 removeBOM 再试看看 decode :)
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.134.48.253
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/PHP/M.1414402128.A.0CE.html
1F:推 noxhsu: 推 很多编辑器用UTF8预设就是有BOM且UN*X下就是会抓 10/27 20:01
2F:→ noxhsu: 但就是在Win环境下会习惯忘掉移掉使得输出前会有一个空字 10/27 20:02
3F:推 wild0921: 我用notepad++,一开始就设定utf-8 无bom 10/27 21:56
4F:→ wild0921: 只是不知道为什麽还是会有bom, 可能是我没有注意.... 10/27 21:57