开发者

Facebook Graph API : Parsing the output

I am trying to get the names of my friends using FB Graph API with this call :

$friends = file_get_contents('https://graph.facebook.com/me/friendsaccess_token='.$session["access_token"]);

echo "Friends : $friends\n";

This gives me a list of the form :

{"data":[{"name":"ABC XYZ","id":"12212839"},{"name":"PQR GHI","id":"5004678"}]}

I want to be able to store only the NAMES in an array. How do I use $friends to get the nam开发者_如何学JAVAes ? Something like $friends['name'] doesn't seem to work.

Please help. Thank you.


$friends = json_decode($friends);
foreach($friends['data'] as $friend)
{
     echo $friend['name'];
}

The return is a json object, you need to decode it. Although I strongly urge you to use an SDK such as http://github.com/facebook/php-sdk/

If this doesn't work try:

$friends = json_decode($friends);
foreach($friends->data as $friend)
{
     echo $friend->name;
}


Here is what I did to get the posts information.. crude but it works. Note that the shares likes comments and reactions are one level deeper in the JSON object

$posts = json_decode($output); // from FB Graph v2.8 API call
foreach($posts->data as $post)
{
  echo "MESSAGE: ", $post->message, "<br>";
  echo "NAME: ", $post->name, "<br>";
  echo "TYPE: ", $post->type, "<br>";
  echo "ID: ", $post->id, "<br>";
  echo "LINK: ", $post->link, "<br>";
  echo "PERMALINK: ", $post->permalink_url, "<br>";
  echo "CREATED: ", $post->created_time, "<br>";
  if($post->shares->count == "") { $shares = "0"; } else { $shares = $post->shares->count; }  
  echo "SHARES: ", $shares, "<br>";
  if($post->reactions->summary->total_count == "") { $reactions = "0"; } else { $reactions = $post->reactions->summary->total_count; }  
  echo "REACTIONS: ", $reactions, "<br>";
  if($post->comments->summary->total_count == "") { $comments = "0"; } else { $comments = $post->comments->summary->total_count; }  
  echo "COMMENTS: ", $comments, "<br>";
  if($post->likes->summary->total_count == "") { $likes = "0"; } else { $likes = $post->likes->summary->total_count; }  
  echo "LIKES: ", $likes, "<br>";
  echo "<br><br>";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜