3

我如何检查当前的 facebook 用户是否喜欢 facebook 页面?

我有这个代码,

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'
      ));
    }

但是如果用户喜欢该页面,则没有响应,请在此处查看页面

谢谢!

4

2 回答 2

6

您可以使用FQL

$isFan = $facebook->api(array(
  "method" => "fql.query",
  "query"  => "SELECT uid FROM page_fan WHERE page_id = '<YOUR PAGE ID>' AND uid = '<UID>"
));
于 2013-06-18T12:06:52.557 回答
3

我不知道您是否只需要使用 FQL 来执行此操作,但还有另一种更简单的方法:

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 将为1

我喜欢先检查变量,如下所示:

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
  }
}
于 2014-02-05T15:57:26.623 回答