0

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.

user1597438
  • 2,171
  • 5
  • 35
  • 78
  • Did you check this? http://stackoverflow.com/questions/5093398/how-to-check-if-a-user-likes-my-facebook-page-or-url-using-facebooks-api – jabbink May 16 '13 at 11:48
  • Yes, I've already checked that and it didn't work for me. – user1597438 May 16 '13 at 11:49
  • 1
    Is the user connected to your app and has given `user_likes` permission? – CBroe May 16 '13 at 11:51
  • Yes. I've set everything in order. I noticed I'm getting an error when I tried running it on local host. Something about invalid OAuth token. but when I remove the part that checks if the user liked the app, it works okay. – user1597438 May 16 '13 at 12:05
  • Well get a valid access token then … – CBroe May 16 '13 at 12:26
  • sorry but where do I get a valid access token? I'm not even sure in which part it's being called in my code. – user1597438 May 16 '13 at 12:29

2 Answers2

2

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);
Community
  • 1
  • 1
Bass Jobsen
  • 48,736
  • 16
  • 143
  • 224
0

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/

Simon Cross
  • 13,315
  • 3
  • 32
  • 26