我计划在我的新博客网站上使用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
页面上显示的所有内容都是:
评论:Times
我怎样才能让它显示帖子的实际数量?
从API 2.0
开始,每个API请求都需要一个有效的access_token
,从您的代码中我看到您正在使用2.0
,这意味着如果您正在使用JS
,将敏感数据发送到根本不安全的Graph
有效的access_token
-
应用程序
access_token
,应用程序内id的组合;此形式的机密{app_id}|{app_secret}
(推荐使用,因为它不会过期( -
用户
access_token
,通过授权应用程序发布(发布2小时后到期,延期60天后到期(
解决方案#1(简单(
切换回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'));
解决方案#2(高级(
你标记了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>
GitHub
演示