如何解析Facebook计数数据并在网站上回显它

时间:2014-11-16 15:40:09

标签: php json facebook facebook-graph-api facebook-like

我有以下代码向我展示了Facebook的特定帖子的数量。我如何获取该数据并将其显示在网站上?

<a href="http://graph.facebook.com/fql?q=SELECT url, total_count FROM link_stat WHERE url='http://gunstons.com/mistakes-when-entering-into-a-sectional-title-scheme/'"></a>

这是代码生成的内容

{
   "data": [
      {
         "url": "http://gunstons.com/mistakes-when-entering-into-a-sectional-title-scheme/",
     "total_count": 2
      }
           ]
}

如何获取总计数和回显在网站上

2 个答案:

答案 0 :(得分:0)

<?php
$contents = file_get_contents("http://graph.facebook.com/fql?q=SELECT url, total_count FROM link_stat WHERE url='http://gunstons.com/mistakes-when-entering-into-a-sectional-title-scheme/'"); //Grab the contents of the link
$data = json_decode($contents); //Parse the contents into a dictionary
echo $data->data->total_count; //Get the total count
?>

希望这有效!

答案 1 :(得分:0)

<a href="http://graph.facebook.com/fql?q=SELECT url, total_count FROM link_stat WHERE url='http://gunstons.com/mistakes-when-entering-into-a-sectional-title-scheme/'"></a>

我认为您将其放入发送到浏览器的HTML中。那对你没有任何好处。最好的解决方案是,当用户点击链接时,浏览器将打开该页面并向用户显示丑陋的JSON。


我相信您希望在服务器端进行处理并将最终计数发送给用户。在这种情况下,下面的内容会有所帮助。

<?php

$yourUrl = "http://graph.facebook.com/fql?q=";

#URL Encode is necessary!
$yourUrl .= urlencode("SELECT url, total_count FROM link_stat WHERE url='http://gunstons.com/mistakes-when-entering-into-a-sectional-title-scheme/'");

$v = file_get_contents($yourUrl);
$obj = json_decode($v,true);
echo $obj["data"][0]["total_count"];

?>