user1348777
user1348777

Reputation: 11

Check if the user is a fan of my facebook page?

I used to use the flowing code to check if the user is a fan of my page or not while he is on my facebook page. Now I want to check if the user is a fan of my facebook page while he is on my WEBSITE.

<?php
            $signed_request = $_REQUEST["signed_request"];
            list($encoded_sig, $payload) = explode('.', $signed_request, 2);
            $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);

            if (empty($data["page"]["liked"])) {
                echo "You are not a fan!";
            } else {
                echo "Welcome back fan!";
            }
    ?>

I read through the facebook documentation, but I was not able to find suitable answer. Help ? I do not want to deal with any application permissions with facebook. How can I approach this through only php ?

Upvotes: 0

Views: 5118

Answers (2)

VikasG
VikasG

Reputation: 579

A better approach to the problem is:

// to generate user access token after the user is logged in
$USER_ACCESS_TOKEN = $facebook->getExtendedAccessToken();

$isFan = curl("https://api.facebook.com/method/pages.isFan?format=json&access_token=" . $USER_ACCESS_TOKEN . "&page_id=" . $FACEBOOK_PAGE_ID);

        function curl($url) {

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            $data = curl_exec($ch);
            curl_close($ch);
            return $data;
        }

Upvotes: 3

Juicy Scripter
Juicy Scripter

Reputation: 25918

You will not get page details if visiting application not in Page Tab.

You may achieve that with simple FQL query to page_fan table (knowing the id of the Facebook Page of course):

SELECT uid, page_id FROM page_fan WHERE uid=me() AND page_id=PAGE_ID

Or by querying Graph API for likes connection of user object:

https://graph.facebook.com/me/likes/PAGE_ID

Both of those ways require user_likes permission granted by user.

To get the same details for user's friends ask for friends_likes permission and substitute me/me() with friend id.

Update: (just to describe what you asked in comments)
There are cases that requiring user_likes may be unnecessary due to nature of flow, if you only need to know that user will/need to like some URL and/or Facebook Page.

You may do so by subscribing (FB.subscribe) to edge.create event which will be triggered once user will like the page (for un-like there is edge.remove event).

Beware that this is only reliable in cases user didn't liked that content before since edge.create only fired on time of user's action.

Upvotes: 4

Related Questions