Facebook Graph API:解析输出

发布于 2024-10-05 14:25:22 字数 426 浏览 3 评论 0原文

我试图通过此调用使用 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"}]}

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

请帮忙。 谢谢。

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 names ? Something like $friends['name'] doesn't seem to work.

Please help.
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥抱没勇气 2024-10-12 14:25:22
$friends = json_decode($friends);
foreach($friends['data'] as $friend)
{
     echo $friend['name'];
}

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

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

$friends = json_decode($friends);
foreach($friends->data as $friend)
{
     echo $friend->name;
}
$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;
}
对不⑦ 2024-10-12 14:25:22

这是我为获取帖子信息所做的事情。虽然很粗糙,但它有效。请注意,点赞评论和反应的分享在 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>";
}

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>";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文