2

How can we check user liked the facebook application or not?

I am trying to create a Facebook app, in it there are two different pages , if user liked then page_1 will be show, otherwise page_2 show.

Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
Amir
  • 2,028
  • 6
  • 19
  • 28

3 Answers3

5
<?php 
$user = $facebook->getUser(); 
if ($user) { 

try{ 
$user_profile = $facebook->api('/me');
 $likeID = $facebook->api(array( 'method' => 'fql.query',
 'query' => 'SELECT url FROM url_like WHERE user_id = "'.$user_profile['id'].'" AND url="myurl/"'; )); 
}
 catch(FacebookApiException $e) { }
 } 
if(count($likeID)>0) echo "User likes this page"; 
else echo "User doesn\'t like this page";

?>

credible and/or official sources:

  1. http://www.zuberi.me/2011/how-to-check-if-user-liked-the-application-or-page.html
  2. http://www.chilipepperdesign.com/2011/02/15/reveal-fan-gate-like-gate-facebook-iframe-tab-tutorial-with-php
  3. How to check if a user likes my Facebook Page or URL using Facebook's API
  4. Facebook how to check if user has liked page and show content?
  5. Facebook Check if user "Liked" the page
Community
  • 1
  • 1
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
  • getUser(); if ($user) { try{ $user_profile = $facebook->api('/me'); $likeID = $facebook->api(array( 'method' => 'fql.query', 'query' => 'SELECT url FROM url_like WHERE user_id = "'.$user_profile['id'].'" AND url="http://myurl/"' )); } catch(FacebookApiException $e) { } } if(count($likeID)>0) echo "User likes this page"; else echo "User doesn\'t like this page"; ?> – Amir Jun 06 '12 at 13:46
  • 1
    You can also update my answer. If you feels it should will be better for other users. And then on you also can accept answer, if it was helpful to you. – Somnath Muluk Jun 06 '12 at 13:57
2

A better approach to the problem can be:

// 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;
        }
VikasG
  • 579
  • 6
  • 14
1
require 'facebook.php';

$app_id = "your app id";
$app_secret = "your app secret";
$facebook = new Facebook(array(
    'appId' => $app_id,
    'secret' => $app_secret,
    'cookie' => true,
));

$signed_request = $facebook->getSignedRequest();


$like_status = $signed_request["app"]["liked"];


if ($like_status == 1){
echo 'User likes this page';
}
else{
echo 'User doesn\'t like this page';
}

Maybe you should try somethng like this, check Facebook's PHP SDK reference page for the exact approach.

RandomGuy
  • 338
  • 3
  • 9
  • `getUser(); if ($user) { try{ $user_profile = $facebook->api('/me'); $likeID = $facebook->api(array( 'method' => 'fql.query', 'query' => 'SELECT url FROM url_like WHERE user_id = "'.$user_profile['id'].'" AND url="http://myurl/"' )); }catch(FacebookApiException $e){ } } if(count($likeID)>0) echo "User likes this page"; else echo "User doesn\'t like this page"; ?>` Did it by above method – Amir Apr 10 '12 at 07:12
  • Hi Frank, I dont know php. Can you convert it javascript or asp.net for me? – Jeyhun Rahimov Nov 20 '12 at 09:34