user1597438
user1597438

Reputation: 2221

Check fan page like to access application in Facebook

I'm currently trying to develop a facebook app which I want only fans to access. So, to check if a user liked the fan page I linked to the game, I'm using this code:

<?php
$our_page_id = 'page_id';
$user_is_fan = false;
$likes = $facebook->api( '/me/likes?fields=id' );
foreach( $likes['data'] as $page ) {
    if( $page['id'] === $our_page_id ) {
        $user_is_fan = true;
                break;
    }
}

echo "likes:" . $likes;
echo "fan:".$user_is_fan;?>

And this is my like button which I generated through the documentation:

<div id="fb-root"></div>
    <script>(function(d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) return;
        js = d.createElement(s); js.id = id;
        js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=14XXXX";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
    </script>
</div>
<div class="fb-like" data-href="http://www.facebook.com/pages/xx/xx?fref=ts" data-send="false" data-width="450" data-show-faces="true"></div>

I'm currently getting an error about an invalid OAuth token but when I remove the part where I check for facebook likes, it works okay. Also, I tried the FB.Event.subscribe('edge.create') function to try to check for events when the like button is clicked but it didn't show the alert.

Earlier, this worked like a charm however now, no matter how much I click the like button, $user_is_fan returns false. Did I do something wrong here? Or am I implementing this wrongly?

Is there a different and effective way of checking for likes in facebook? Help please. NOTE: I'm using the Facebook PHP SDK.

Upvotes: 0

Views: 3962

Answers (2)

Simon Cross
Simon Cross

Reputation: 13345

It is now against Facebook's policies to gate an app or content within an app based on if someone has liked your page.

The user_likes permission will not be granted for the purposed of gating an app or app content.

In addition, FQL is now deprecated and should no longer be used for new apps.

See the announcement here: https://developers.facebook.com/blog/post/2014/08/07/Graph-API-v2.1/

Upvotes: 0

Bass Jobsen
Bass Jobsen

Reputation: 49054

You can use a fgl query to do this. See: http://developers.facebook.com/docs/reference/fql/page_fan/. Don't forget to set the user_likes permissions. You also need a valid fanpageid for your query, see: Get Facebook fan page ID

In the example code below: If the user liked the page $response should contain the created_time else $response will be empty.

Example:

<?php
error_reporting(E_ALL);
ini_set('display_errors','on');
require 'facebook-php-sdk-master/src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => '***************',
  'secret' => '******************************',
));

$applicationurl = 'http://testdrive.nl/facebooklikes.php';

// Get User ID
$user = $facebook->getUser();

if(empty($user))
{
    $params = array(
  'scope' => 'user_likes',
  'redirect_uri' => $applicationurl
   );   

$loginUrl = $facebook->getLoginUrl($params);
header('Location: ' . $loginUrl ."\r\n");
exit;
}

$response = $facebook->api(array(
     'method' => 'fql.query',
     'query' =>'SELECT created_time FROM page_fan WHERE uid = '.$user.' AND page_id =  202225796470556' //minijoule
 ));

var_dump($response);

$response = $facebook->api(array(
     'method' => 'fql.query',
     'query' =>'SELECT created_time FROM page_fan WHERE uid = '.$user.' AND page_id =  418784251474680' //webvrouw.nl
 ));

var_dump($response);

Upvotes: 2

Related Questions