我打算在我的新博客网站上使用facebook评论插件,并希望在文章标题中显示一些统计信息(日期,视图,评论时间等),但我无法显示Facebook视图数量起来。这是我目前的代码:
<div id="fb-root"></div>
<script>
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_GB/sdk.js#xfbml=1&appId=277028005786031&version=v2.0";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
在显示位置
<b>Commented On:</b> <fb:comments-count href="http://example.com/"></fb:comments-count> Times
页面上显示的所有内容均为:
评论:时间
如何让它显示实际的帖子数?
答案 0 :(得分:1)
从API 2.0
开始,每个API请求都需要有效的access_token
,并且从您的代码中我看到您正在使用2.0
,这意味着将敏感数据发送到图表,这是不安全的完全,如果你正在使用JS
有效access_token
应用access_token
,app app&amp; amp;这种形式的秘密{app_id}|{app_secret}
(推荐因为它没有过期)
通过授权应用程序颁发的用户access_token
(发布后2小时到期,延长后60天到期)
切换回API v1.0,可用,直到** 2015年4月30日
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_GB/sdk.js#xfbml=1&appId={app_id}&version=v1.0";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
您标记了PHP
,因此我发布的解决方案将来发布,
<强> comments-count.php
强>
<?php
header('Content-Type: application/json');
$config = array('appId' => '{Your-app-Id}',
'secret' => '{Your-app-secret}');
/** If the $_GET['href'] is set and not empty **/
if( isset($_GET['href']) && !empty($_GET['href']) ) {
$href = $_GET['href'];
$commentsCount = getCommentsCount($href);
// check if the HREF has comments count
if ( array_key_exists('comments', $commentsCount) ) {
$comments = $commentsCount['comments'];
} else {
$comments = 0;
}
echo json_encode( array('meta' => array('code' => 200),
'id' => $commentsCount['id'],
'comments' => $comments,
'shares' => $commentsCount['shares']), JSON_PRETTY_PRINT);
} else {
/** else print the error below (JOSN encoded) **/
header('HTTP/1.1 400');
echo json_encode( array('meta' => array('code' => 400),
'message' => 'href is not provided'), JSON_PRETTY_PRINT);
// JSON_PRETTY_PRINT requires PHP 5.4 or higher, you should remove it if you have 5.3 or lower
}
function getCommentsCount($href) {
global $config;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/v2.0/?id='. $href . '&access_token=' . $config['appId'] . '|' . $config['secret']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$r = curl_exec($ch);
return json_decode($r, true);
}
<强> comments-count.js
强>
;(function($) {
$.fn.facebookCommentsCount = function( options ) {
var $target = this;
/** Plugin default settings **/
var settings = $.extend({
apiPath : 'fb-comments-count/comments-count.php', // Path to `comments-count.php`
zeroComments : null,
oneComment : null,
multipleComments : null
}, options);
return $target.each( function() {
var href = $(this).attr('data-href');
var $this = $(this)
$.ajax({
url: settings.apiPath,
type: 'GET',
dataType: 'json',
data: { href : href },
success: function(data) {
if (data.comments === 0) {
if(settings.zeroComments) {
$this.html(data.comments + ' ' + settings.zeroComments);
} eles {
$this.html(data.comments);
}
} else if (data.comments === 1 ) {
if(settings.oneComment) {
$this.html( data.comments + ' ' + settings.oneComment);
} else {
$this.html(data.comments);
}
} else {
if(settings.multipleComments) {
$this.html( data.comments + ' ' + settings.multipleComments);
} else {
$this.html(data.comments);
}
}
},
error: function(error) {
console.log(error);
}
});
});
};
}(jQuery));
在您的网页中添加div
,span
或p
(内联元素更好,div
为阻止)
<div class="fb-comments-count" data-href="{URL}">0</dvi>
<script>
$('.fb-comments-count').facebookCommentsCount();
</script>