检查用户是否喜欢外部网页上的页面(脸谱)

时间:2012-05-26 14:43:50

标签: php jquery facebook api

我需要知道用户是否喜欢我的Facebook页面(不是在fb app中,而是在外部网站上)。 我试图使用这个片段: http://jsfiddle.net/alexgarry/qdfL6/5/

将其复制到文档后它没有用。 加载脚本后,会出现一个新的弹出窗口并显示错误:“再试一次”。

我的代码:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
<style>
#container_notlike, #container_like {
    display:none
}
</style>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
  FB.init({
    appId  : 'MY_APP_ID',
    status : true,
    cookie : true,
    xfbml  : true  
  });

$(document).ready(function(){
    FB.login(function(response) {
        if (response.status == 'connected') {
            var user_id = response.authResponse.userID;
            var page_id = "230305033727799"; // coca cola page https://www.facebook.com/cocacola
            var fql_query = "SELECT uid FROM page_fan WHERE page_id="+page_id+" and uid="+user_id;

            FB.api({
                method: 'fql.query',
                query: fql_query
            },
            function(response){
                if (response[0]) {
                    $("#container_like").show();
                } else {
                    $("#container_notlike").show();
                }
            }
            );    
        } else {
        // user is not logged in
        }
    });
});
</script>
</head>
<body>
<div id="fb-root"></div>
<div id="container_notlike">
YOU DONT LIKE
</div>
<div id="container_like">
YOU LIKE
</div>
</body>
</html>

任何想法?

问候。

1 个答案:

答案 0 :(得分:3)

<!DOCTYPE html>
<html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <style type="text/css">
        #container_notlike, #container_like {
            display:none
        }​
    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            FB.login(function(response) {
                if (response.status == 'connected') {
                    var user_id = response.authResponse.userID;
                    var page_id = "40796308305";  

                    FB.api('/me/likes/'+page_id, function(response) {
                        if (response.data[0]) {
                            $("#container_like").show();
                        } else {
                            $("#container_notlike").show();
                        }
                    });
                } else {
                    // user is not logged in                        
                }
            });
        });
    </script>
</head>
<body>
    <div id="fb-root"></div>
    <script src="https://connect.facebook.net/en_US/all.js"></script>
    <script>
        FB.init({appId: 'MY_APP_ID', status: true, cookie: true, xfbml: true});
    </script>
    <div>
        <div id="container_notlike">
            YOU DONT LIKE
        </div>
        <div id="container_like">
            YOU LIKE
        </div>​
    </div>
</body>

将MY_APP_ID替换为您的APP_ID。