Re: [請益] JSON編碼"{"編碼有問題

看板PHP作者 (某人睡醒就發文)時間9年前 (2014/10/27 17:28), 編輯推噓2(202)
留言4則, 2人參與, 最新討論串4/4 (看更多)
※ 引述《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://www.ptt.cc/bbs/PHP/M.1414402128.A.0CE.html

10/27 20:01, , 1F
推 很多編輯器用UTF8預設就是有BOM且UN*X下就是會抓
10/27 20:01, 1F

10/27 20:02, , 2F
但就是在Win環境下會習慣忘掉移掉使得輸出前會有一個空字
10/27 20:02, 2F

10/27 21:56, , 3F
我用notepad++,一開始就設定utf-8 無bom
10/27 21:56, 3F

10/27 21:57, , 4F
只是不知道為什麼還是會有bom, 可能是我沒有注意....
10/27 21:57, 4F
文章代碼(AID): #1KJX1G3E (PHP)
文章代碼(AID): #1KJX1G3E (PHP)