在Joomla文章博客视图中显示facebook评论数

时间:2016-03-11 08:08:35

标签: php facebook joomla

注意:由于stackoverflow的严格规则,我在http://之后放置一个空格故意打破了我在这里发布的网址。

如果您输入此网址

  

https:// graph.facebook.com/v2.4/?fields=share{comment_count}&id=http:// kclau.com/investment/where-klci-is-heading /

直接在浏览器中显示:

  

{" share":{         " comment_count":3}," id":" http:// kclau.com/investment/where-klci-is-heading/" }

精细。所以我准备php如下:

<?php 
$url = "http:// kclau.com/investment/where-klci-is-heading/";
$response = file_get_contents("https:// graph.facebook.com/v2.4/?fields=share{comment_count}&id=".$url);
$obj = json_decode($response);
echo $obj->share->comment_count;
?>

它什么都没显示。 我试过var_dump($ obj)display NULL !!

它有什么问题?有人可以帮忙吗?感谢

1 个答案:

答案 0 :(得分:0)

我不确定为什么之前的代码无效,但有人向我建议了CURL,它正在运行: -

<?php
$url = "http://kclau.com/investment/where-klci-is-heading/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/v2.4/?fields=share{comment_count}&id=".$url);
$result = curl_exec($ch);
curl_close($ch);

$obj = json_decode($result);
echo $obj->share->comment_count;
?>