从Facebook读取大型JSON

时间:2014-01-22 11:27:18

标签: php json facebook facebook-graph-api

当我使用此代码时,如何从Facebook的JSON中读取多个具有相同名称的值:

$urlamandar = 'https://graph.facebook.com/'.$user."?access_token=".$access_token;
$content = file_get_contents($urlamandar);
$obj = json_decode($content, true);
$obj = json_decode($content);

显示:

{
   "id": "XXXXXXXX",
   "name": "Ricardo Capistran",
   "first_name": "Ricardo",
   "last_name": "Capistran",
   "gender": "male",
   "locale": "es_MX",
   "username": "richycapy"
}

因为因为只有一个ID,一个NAME,一个USERNAME等,所以非常简单地将它们放在vars中就像这样:

$fbuserid = $obj->{'id'};
$fbname = $obj->{'name'};
$fbusername = $obj->{'username'};
$fbemail = $obj->{'email'};
$fbbirthday = $obj->{'birthday'};

但是当谈到像这段代码这样的大文件时:

$urlamandar = 'https://graph.facebook.com/'.$user.'/albums?access_token='.$access_token;

它显示了一个更大的数组:

{
   "data": [
      {
         "id": "10150732237818223",
         "from": {
            "name": "Ricardo Capistran",
            "id": "XXXXXXX"
         },
         "name": "EnterateNorte.com Photos",
         "link": "https://www.facebook.com/album.php?fbid=10150732237818223&id=743158222&aid=457026",
         "privacy": "custom",
         "count": 31,
         "type": "app",
         "created_time": "2012-03-11T02:44:42+0000",
         "updated_time": "2014-01-07T03:13:24+0000",
         "can_upload": false
      },
      {
         "id": "440168313222",
         "from": {
            "name": "Ricardo Capistran",
            "id": "743158222"
         },
         "name": "Timeline Photos",
         "link": "https://www.facebook.com/album.php?fbid=440168313222&id=743158222&aid=220377",
         "cover_photo": "10151730849598223",
         "privacy": "everyone",
         "count": 175,
         "type": "wall",
         "created_time": "2010-06-30T22:38:45+0000",
         "updated_time": "2014-01-01T02:09:11+0000",
         "can_upload": false
      },
      {
         "id": "10150797320378223",
         "from": {
            "name": "Ricardo Capistran",
            "id": "743158222"
         },
         "name": "Instagram Photos",
         "link": "https://www.facebook.com/album.php?fbid=10150797320378223&id=743158222&aid=466555",
         "cover_photo": "10151695050098223",
         "privacy": "friends",
         "count": 37,
         "type": "app",
         "created_time": "2012-04-09T23:50:08+0000",
         "updated_time": "2013-12-29T08:29:15+0000",
         "can_upload": false
      }
   ],
   "paging": {
      "cursors": {
         "after": "NDM1NjY5NjI4MjIy",
         "before": "MTAxNTA3MzIyMzc4MTgyMjM="
      },
      "next": "https://graph.facebook.com/*my_id*/albums?access_token=*access_token*&limit=25&after=NDM1NjY5NjI4MjIy"
   }
}

我需要关闭所有专辑的“名称”和“id”,所以我可以用包含的图片重复这个相同的程序

显然它有更多的专辑,我只是​​为了解释我自己而在3之后剪掉它......

有没有办法将它们放在vars中?用PHP“为每个”一些如何

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以执行类似

的操作
$obj = json_decode($content, true);

$array_album = array();
foreach($obj['data'] as $key=>$val){
    $array_album[] = array("id"=>$val["id"],"name"=>$val["name"]);
    echo "ID : ".$val["id"]. " NAME : ".$val["name"] ;
    echo "<br />";
}
print_r($array_album); // This will have all the id and names

如果您不想存储在数组中,那么名称和ID将出现在上面的循环中,您可以使用这些值执行任何操作。或者使用数组$ array_album并循环浏览,如果你想稍后使用它。