检查登录用户是否喜欢我的Facebook页面

时间:2013-06-11 10:51:55

标签: facebook facebook-like

行。所以我不知道如何做到这一点,但你将不得不像一个3岁的孩子一样对我说话。我知道PHP,HTML,CSS和一小撮javascript。我希望能够创建一个获取用户facebook的页面并检查他们是否喜欢我的Facebook页面(https://www.facebook.com/MrDarrenGriffin)。如果有,重定向将指向页面。但是,如果没有,它会告诉他们要去下载页面,他们必须喜欢这个页面。喜欢的方法可以是代码中嵌入的like按钮。在他们喜欢该页面后,它将再次通过检查,如果他们已经成功地喜欢我的页面,它会将它们重定向到下载部分(或指定的页面)。

提前致谢:D

2 个答案:

答案 0 :(得分:14)

您可以使用此代码,因为它非常简单:

FB.api({
    method:     "pages.isFan",
    page_id:        page_id,
},  function(response) {
        console.log(response);
        if(response){
            alert('You Likey');
        } else {
            alert('You not Likey :(');
        }
    }
);

此ll用户user_likes权限

希望这会对你有所帮助

答案 1 :(得分:9)

以下是执行此操作的步骤:

1)您需要使用此分步指南将用户连接到您的Facebook页面以获取用户基本信息

https://developers.facebook.com/docs/facebook-login/getting-started-web/

FB.api('/me', function(response) {
    console.log('Good to see you, ' + response.name + '.');
}

2)在知道用户使用Facebook连接后,您需要发出图表GET请求,以了解该用户是否喜欢您的网页

FB.api('/me/likes/YOUR_APP_ID', function(response) {
    console.log(response.data);
}

3)然后运行您的业务逻辑(是否让用户下载页面或其他页面)

上面的教程中的代码也在下面给出

<body>
<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
  FB.init({
  appId      : 'YOUR_APP_ID', // App ID
  channelUrl : '//www.example.com/channel.html', // Channel File
  status     : true, // check login status
  cookie     : true, // enable cookies to allow the server to access the session
  xfbml      : true  // parse XFBML
});

FB.Event.subscribe('auth.authResponseChange', function(response) {
    if (response.status === 'connected') {
      testAPI();
    } else if (response.status === 'not_authorized') {
  $("#btnFB").show();     
      FB.login();
    } else {
  $("#btnFB").show();         
      FB.login();
    }
});

};

// Load the SDK asynchronously
(function(d){
 var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
 if (d.getElementById(id)) {return;}
 js = d.createElement('script'); js.id = id; js.async = true;
 js.src = "//connect.facebook.net/en_US/all.js";
 ref.parentNode.insertBefore(js, ref);
}(document));

function testAPI() {
  FB.api('/me', function(response) {
    console.log('Good to see you, ' + response.name + '.');
  });
  FB.api('/me/likes/PAGE_ID', function(response) {
    console.log(response.data);
  });
}

如何获取PAGE_ID? 转到http://developers.facebook.com/tools/explorer/?method=GET&path=me%2Flikes%2F

这对我有用! :)