检查用户是否喜欢Facebook页面

时间:2013-05-02 23:23:02

标签: javascript facebook

我有以下代码,它是一个像Facebook页面的按钮,并提醒:感谢你的喜欢, 我试图检查页面加载以查看用户是否已经喜欢该页面,并提醒“页面已经被喜欢”或“页面尚未被喜欢”。

<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function() {
    FB.init({appId: '637166706299573', status: true, cookie: true, xfbml: true});
    FB.Event.subscribe('edge.create', function(href, widget) {
        alert('Thanks for the Like');
    });
};
(function() {
    var e = document.createElement('script');
    e.type = 'text/javascript';
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    e.async = true;
    document.getElementById('fb-root').appendChild(e);
}());
</script>
<script type="text/javascript" src="facebook.js"></script>
<fb:like href="https://www.facebook.com/Tabyeed" layout="button_count" show-faces="true" width="450" action="like" colorscheme="light" font="arial"></fb:like>

我尝试了这个,但它不起作用:

<script type="text/javascript">
function liked() {
    FB.api("me/likes/348028968575501", function(response) {
        if (response.data.length == 1) {
            alert("page liked already");
        } else {
            alert("page is NOT liked already");
        }
    });
}
</script>

任何帮助人员?
感谢

2 个答案:

答案 0 :(得分:0)

我没有看到登录按钮/流程,所以最好的猜测是FB.api("me/likes/348028968575501"将失败,因为用户未经过身份验证,这与不依赖身份验证的事件订阅不同。

答案 1 :(得分:0)

我认为您正在寻找页面标签应用程序检查,如页面与否。

/**
 * get the signed request parameter and decode it to get the user's
 * 'liked' status for the current page
 */
if (!empty($_REQUEST['signed_request'])) {
  $signedRequest = $_REQUEST['signed_request'];
  list($sig, $payload) = explode('.', $signedRequest, 2);
  $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
}

/**
 * if the user has already 'liked' the page then render the 'liked' view
 * else, render the 'pre liked' view
 */
if (empty($data['page']['liked'])) {
  include 'views' . DIRECTORY_SEPARATOR . 'pre_liked.php';
} else {
  include 'views' . DIRECTORY_SEPARATOR . 'post_liked.php';
}

创建一个新的应用程序作为页面选项卡,并在index.php上使用此代码