How to Determine if Facebook User 'Likes' a Certain Page
Asked Answered
R

4

5

Is there any way to determine if a Facebook user 'likes' a certain page with the API Facebook offers currently?

UPDATE

I have been trying to get this to work via the PHP graph API for a while now with this code.

$fbconfig['appid'] = '***';
$fbconfig['api'] = '***';
$fbconfig['secret'] = '***';

try {
    include_once "facebook.php";
} catch(Exception $o) {
    echo '<pre>';
    print_r($o);
    echo '</pre>';
}

$facebook = new Facebook(array(
  'appId'  => $fbconfig['appid'],
  'secret' => $fbconfig['secret'],
  'cookie' => true,
));

$session = $facebook->getSession();
$me = null;

if ($session) {
    try {
        $uid = $facebook->getUser();
        $me = $facebook->api('/me');
    } catch (FacebookApiException $e) {
        d($e);
    }
}

if($me) {
    try {
        $likes = $facebook->api('/me/likes');
    } catch(Exception $o) {
        d($o);
    }
}

However, the session is always null, even though I am logged in.

Apparently, the above code is only good for Facebook Connect "Canvas" applications, I am calling this code from an FBML tab on a Facebook "Page" through an AJAX call to my server.

I'm not very familiar with all of the Facebook development terminology.

How can I get the current user's 'likes' in my situation?

Reduplication answered 16/1, 2011 at 3:44 Comment(0)
B
11

There's a way to do that without requesting permission and without using any kind of API.

If you have PHP:

<?php
$signed_request = $_REQUEST["signed_request"];

list($encoded_sig, $payload) = explode('.', $signed_request, 2);

$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);

$app_data = isset($data["app_data"]) ? $data["app_data"] : '';
$_REQUEST["fb_page_id"] = $data["page"]["id"];
$access_admin = $data["page"]["admin"] == 1;
$has_liked = $data["page"]["liked"] == 1;
?>

You can use $has_liked to wrap your fan-specific content

<?php if($has_liked) : ?>
fan-specifc
<?php else : ?>
for non-fans only
<?php endif; ?>

replaces

<fb:visible-to-connection>
fan-specific content
<fb:else>
for non-fans only
</fb:else>
</fb:visible-to-connection>

It's very useful and convenient, since asking for permission is a turn-off.

Backsight answered 23/2, 2011 at 12:42 Comment(1)
In case anyone else is having problems with the signed_request parameter not coming through, try changing your Page Tab URL or Canvas URL to point to a specific file, eg. instead of setting it to example.com/app, set it to example.com/app/index.phpHolmic
C
2

It you are using graph API, then there is a likes object in the feed which contains Id of the users who like that particular post/feed item.

Also you can fire query using FQL apis.

Cordero answered 16/1, 2011 at 3:49 Comment(0)
A
0

Try using FQL. Check out this page, it lets you test FQL calls. http://developers.facebook.com/docs/reference/rest/fql.query

SELECT user_id FROM like WHERE object_id="fb_id"

but replace fb_id with the id of the object you want to know how many people like.

Algebraist answered 16/1, 2011 at 4:1 Comment(0)
A
0

To get the user likes, you need the user_likes permission.
Just add the permission to your fb:login-button perms attribute and next time the user logs-in, he will be asked to grant your application access to the new permission.

Agamemnon answered 16/1, 2011 at 22:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.