Facebook Graph API:解析输出

时间:2010-11-30 16:53:39

标签: php facebook facebook-graph-api

我试图通过此调用使用FB Graph API获取我朋友的名字:

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

echo "Friends : $friends\n";

这给了我一个表格列表:

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

我希望能够只在数组中存储NAMES。如何使用$ friends获取名称?像$ friends ['name']这样的东西似乎不起作用。

请帮忙。 谢谢。

2 个答案:

答案 0 :(得分:5)

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

返回是一个json对象,需要对其进行解码。虽然我强烈建议您使用http://github.com/facebook/php-sdk/

等SDK

如果这不起作用,请尝试:

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

答案 1 :(得分:0)

以下是我为了获取帖子信息所做的工作..粗略但是有效。请注意,共享喜欢评论和反应在JSON对象中更深层次

$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>";
}