无法使用json_decode读取json feed

时间:2013-02-12 21:57:58

标签: php json

在我在网址中提供访问令牌后,我使用Facebook开放图作为json feed格式来获取页面名称。

这是我的代码:

$get_pages = file_get_contents("https://graph.facebook.com/me/accounts?access_token=".$_POST['access_token']);
$get_pages = json_decode($get_pages);

foreach($get_pages->data as $page){
    echo $page[name];
    }

我试图回应页面的名称。 Facebook提供的Json代码如下所示:

 {
       "data": [
          {
             "category": "website",
             "name": "jjj",
             "access_token": "jjj",
             "id": "jjj",
             "perms": [
                "jjj",
                "jjj",
                "jjj",
                "jjj",
                "jjj",
                "jjj"
             ]
          },
          {
             "category": "Community",
             "name": "ggg",
             "access_token": "ggg",
             "id": "ggg",
             "perms": [
                "ggg",
                "ggg",
                "ggg",
                "ggg",
                "ggg",
                "ggg"
             ]
          }
],
   "paging": {
      "next": "hhh"
   }
}

我收到此错误:

    catchable fatal error: object of class stdclass could not be converted to string in
with this line of code :$get_pages = json_decode($get_pages);

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

我解决了:)我将代码更改为:

$get_pages = file_get_contents("https://graph.facebook.com/me/accounts?access_token=".$_POST['access_token']);
$get_pages = json_decode($get_pages, TRUE);
echo '<select id="select_page">';
foreach($get_pages[data] as $page_name){
echo '<option>'.$page_name[name].'</option>';
}
echo '</select>';