frank_texti
frank_texti

Reputation: 97

Find out if a user has like a page

Hi I am using the following to check if a user has liked my app :

The problem is that the signed request returns without the page object. Where do I set up the permissions so it returns the full request?

       require_once('facebook.php');

        $facebook = new Facebook(array(
            'appId'=> FB_APP_ID, // replace with your value
            'secret'=> FB_APP_SECRET // replace with your value
        ));
        $signedRequest = $facebook->getSignedRequest();
        var_dump($signedRequest);
        // Inspect the signed request
        if($signedRequest['page']['liked'] == 1){

           echo 'like';

        } else {

           echo 'no like';

        }
    }

Upvotes: 0

Views: 820

Answers (1)

kwarunek
kwarunek

Reputation: 12597

Page is passed only on facebok pages (if application is configured and added as a Tab to fanpage), not in apps.facebook.com (canvas) nor any your domain.

On other hand i am using on apps per user (it changed recently so it may be outdated)

// scope for user
// $facebook <- assuming this is facebook sdk object
// $params = array(
//    'scope' => 'user_likes' <- to get likes we have to require this
//);

function fanGate() {
    $likes = null;
    try {
        $likes = $facebook->api('/me/likes');
    } catch (Exception $e) {}
    $likeUs = false;
    if ($likes)
        foreach ($likes['data'] as $like) {
            if ($like['id'] == 'some id of page')
                $likeUs = true;
        }
    return $likeUs;
}

edit

According to documentation to check if user likes Fanpage or any other GraphAPI object you only need to require user_likes and then

$arrResult = $facebook->api("/me/likes/${SOMEOBJECT_ID}")
// $arrResult = $facebook->api("/${USER_ID}/likes/${SOMEOBJECT_ID}");
if (empty($arrResult['data']))
    echo 'dont like';
else
    echo 'like';

Calling barely /me/likes returns all likes, but it will change - 2013-10-02

/USER_ID/likes default update Currently the API returns all likes by default. After the migration, fetching a user's likes via the Graph API will return 25 results at a time. We've added pagination to the results so you can page through to all of a user's likes.

Upvotes: 1

Related Questions