最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check if the user likes the facebook page or not - Stack Overflow

programmeradmin6浏览0评论

I'm trying to check if the user liked my page before use my app on it with the following code

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

</body>

determine that the user is authnticated in another page and logged in properly

I'm trying to check if the user liked my page before use my app on it with the following code

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

</body>

determine that the user is authnticated in another page and logged in properly

Share Improve this question edited Oct 29, 2014 at 16:27 Stéphane Bruckert 23k14 gold badges99 silver badges135 bronze badges asked Sep 20, 2013 at 13:53 Mohammed Fayez AbulkasMohammed Fayez Abulkas 251 gold badge2 silver badges6 bronze badges 1
  • Always alert page is NOT liked – Mohammed Fayez Abulkas Commented Sep 20, 2013 at 14:37
Add a ment  | 

1 Answer 1

Reset to default 4

Simple Approach use this method

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

But,This code only works if the user has granted an extended permission for that which is not ideal.

Here's another approach.

In a nutshell, if you turn on the "OAuth 2.0 for Canvas" advanced option, Facebook will send a $_REQUEST['signed_request'] along with every page requested within your tab app. If you parse that signed_request you can get some info about the user including if they've liked the page or not.

function parsePageSignedRequest() {
    if (isset($_REQUEST['signed_request'])) {
      $encoded_sig = null;
      $payload = null;
      list($encoded_sig, $payload) = explode('.', $_REQUEST['signed_request'], 2);
      $sig = base64_decode(strtr($encoded_sig, '-_', '+/'));
      $data = json_decode(base64_decode(strtr($payload, '-_', '+/'), true));
      return $data;
    }
    return false;
  }
  if($signed_request = parsePageSignedRequest()) {
    if($signed_request->page->liked) {
      echo "This content is for Fans only!";
    } else {
      echo "Please click on the Like button to view this tab!";
    }
  }
发布评论

评论列表(0)

  1. 暂无评论