請教,php怎麼列印出從伺服器返回的原始json?
我用一個php從伺服器請求,伺服器返回了標準格式的json,然後賦值到$a,我現在想把原始json資料列印出來,怎麼辦?
echo只顯示一個Array,print,prinf是空,print_r是顯示出了,但已經不是json格式的資料了。
------解決方案--------------------
顯然$a儲存的不是json字串,而是經過json_encode()後的PHP的數組。
相應的,你可以echo json_decode($a);
------解決方案--------------------
echo json_encode($a);即可,$a明顯是decode過的字串……
------解決方案--------------------
你使用了新浪的SDK,而SDK的作用即在開發便捷。雖然/statuses/home timeline的返回格式是json,但還是經過了SDK內部處理。
PHP不能直接操作json字串,因此這個SDK幫你預先把json轉換為PHP的數組。
------解決方案--------------------
PHP code
$ch = curl_init(); $ch = curl_init($url) ; curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); $out = curl_exec($ch); $out = trim($out); curl_close($ch); $out = json_decode($out, true); var_dump($out);
------解決方案--------------------
非同步直接用ajax不就行了嗎,對json支援很好探討
非常感謝你提供了一個另外的解決方案,但是我用的前端html+js的非同步處理方式,所以非常好用但是用不上。學習了!引用:
PHP code
$ch = curl_init();
$ch = curl_init($url) ;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ;
curl_setopt($ch, CU……