[英]php : Parse json on Google app engine
我使用下面的代碼來獲取一個url,這個工作成功,它返回一個json數據
$url = "https://graph.facebook.com/me?access_token=".$access_token;
function fetch_url($url){
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
$java_url = new URL($url);
$java_bufferreader = new BufferedReader(new InputStreamReader($java_url->openStream()));
while (($line = $java_bufferreader->readLine()) != null) {
$content .= $line;
}
return $content;
}
// Sample usage:
$friends = fetch_url($url);
當我回復$ friends時,我得到以下json代碼,
{
"id": "100003185423323",
"name": "hnu Chid",
"first_name": "hnu",
"last_name": "Chid",
"link": "http://www.facebook.com/ilovaddddd",
"username": "ilddddd",
"education": [
{
"school": {
"id": "182225911846032",
"name": "Sunean"
},
"type": "High School"
},
{
"school": {
"id": "105504952817559",
"name": "Sathya University"
},
"type": "College"
}
],
"gender": "female",
"timezone": 5.5,
"locale": "en_US",
"verified": true,
"updated_time": "2012-09-18T18:37:57+0000"
}
我不能在GAE上使用json解碼,當我嘗試json解碼我得到服務器錯誤,有沒有其他方法在PHP上解析它,請幫助我。
1.確保json_decode存在(已啟用)。
<?php
if (function_exists('json_decode')) {
echo "json_decode functions is available.<br />\n";
} else {
echo "json_decode functions is not available.<br />\n";
}
?>
2.使用try catch來捕獲解析錯誤。
<?php
try {
$response = json_decode($friends, true);
print_r($response);
} catch (Exception $ex) {
echo 'Caught exception: ', $ex->getMessage(), "<br/>\n";
var_dump($friends);
}
?>
3.閱讀更多信息,請訪問http://php.net/manual/en/function.json-decode.php
如果函數正常,也嘗試驗證JSON響應。 http://jsonformatter.curiousconcept.com/我試過發布的,看起來很有效。 但我建議你出於安全原因改變一些東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.