Facebook API: output analysis

I am trying to get the names of my friends using the 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 NAMES in an array. How to use $ friends to get names? Something like $ friends ['name'] doesn't seem to work.

Please, help. Thanks.

+4
source share
2 answers
 $friends = json_decode($friends); foreach($friends['data'] as $friend) { echo $friend['name']; } 

Return is a json object, you need to decode it. Although I urge you to use the SDK, for example http://github.com/facebook/php-sdk/

If this does not work, try:

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

Here is what I did to get message information .. rude, but it works. Note that stakes like comments and reactions 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
source

All Articles