开发者

How to obtain if user likes page or not with API 3.0?

开发者 https://www.devze.com 2023-04-06 20:48 出处:网络
All what I found is related to old API not 3.0 :\\ I need 开发者_如何学Goto determine that before an application will display proper message.Your app will need to request the user_likes permission in

All what I found is related to old API not 3.0 :\ I need 开发者_如何学Goto determine that before an application will display proper message.


Your app will need to request the user_likes permission in order to do this.

Then, you can do the following using the JavaScript SDK:

<script type="text/javascript">
FB.getLoginStatus(function(response) {
    if(response.authResponse) {
        token = response.authResponse.accessToken;
        FB.api('/me/likes', function(response) {
          likes = response.data;
          for(x in likes) {
            page_id = likes[x].id;
            page_name = likes[x].name;
            // check if user is a fan of Coca-Cola
            if(page_id == '40796308305') {
                alert('You like the page:\n ' + page_name);
            }
          }
        }, {access_token: token});
    }
});
</script>

Or the following, using the PHP SDK:

<?php
$likes = $facebook->api('/me/likes');
foreach($likes[data] as $like) {
    if(array_key_exists('id', $like)) {
        $page_id = $like['id'];
        $page_name = $like['name'];
        // check if user is a fan of Coca-Cola
        if($page_id == '40796308305') {
            print 'You like the page: ' . $page_name;
        }
    }
}
?>


If this is for a Facebook fan page, Facebook has a post on how to do this with your iframe page. You need to decode the signed_request http post parameter that gets sent to your site. This page shows you how to decode it in PHP.


Check this.

http://fatalweb.com/articles/how+to+check+if+user+has+liked+facebook+fanpage-24.htm

0

精彩评论

暂无评论...
验证码 换一张
取 消