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.