简体   繁体   中英

How to check if current facebook user like a page using PHP SDK?

How can i check if the current facebook user like a facebook page?

I have this code,

require_once("/path/to/sdk/facebook.php");


    // Create our Application instance (replace this with your appId and secret).
    $facebook = new Facebook(array(
      'appId' => 'APP_ID',
      'secret' => 'APP_SECRET',
    ));

    $user = $facebook->getUser();

    if ($user) {
      try {
        $likes = $facebook->api("/me/likes/153649677989200");
        if( !empty($likes['data']) )
            echo "I like!";
        else
            echo "not a fan!";
      } catch (FacebookApiException $e) {
        error_log($e);
        $user = null;
      }
    }

    if ($user) {
      $logoutUrl = $facebook->getLogoutUrl();
    } else {
      $loginUrl = $facebook->getLoginUrl(array(
        'scope' => 'user_likes'
      ));
    }

But is not responding if the user like the page, please see the page here

Thanks!

You can use FQL

$isFan = $facebook->api(array(
  "method" => "fql.query",
  "query"  => "SELECT uid FROM page_fan WHERE page_id = '<YOUR PAGE ID>' AND uid = '<UID>"
));

I don't know if you need to do this only using FQL, but there is another easier way:

require_once("/path/to/sdk/facebook.php");

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId' => 'APP_ID',
  'secret' => 'APP_SECRET',
));

$signed_request = $facebook->getSignedRequest();

$like = $signed_request['page']['liked'];

$like will be empty or 1 .

I like to check variables first, like this:

if(is_array($signed_request) && isset($signed_request['page']) {
  if($signed_request['page']['liked']) {
    // do whatever you want to do if the user likes the page
  } else {
    // do whatever you want to do if the user don't like the page yet
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM