php – 在GRAPH API中检查页面的用户风扇的方法是什么?

前端之家收集整理的这篇文章主要介绍了php – 在GRAPH API中检查页面的用户风扇的方法是什么?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在图api中,Pages.isFan方法不起作用,用图检查用户风扇的方法是什么?

谢谢.

更新2:
要检查当前用户是否在Facebook页面的粉丝登陆您的标签,请检查此 answer.

更新:
您可以使用喜欢的连接来检查用户是否是页面的粉丝:

https://graph.facebook.com/me/likes/PAGE_ID
&access_token=ACCESS_TOKEN

这将返回一个空数据数组:

Array
(
    [data] => Array
        (
        )

)

或者如果风扇:

Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [name] => Real Madrid C.F.
                    [category] => Professional sports team
                    [id] => 19034719952
                    [created_time] => 2011-05-03T20:53:26+0000
                )

        )

)

所以这是我们如何检查使用PHP-SDK:

<?PHP
require '../src/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/PAGE_ID");
    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'
  ));
}

// rest of code here
?>

使用JS-SDK的相似脚本:

FB.api('/me/likes/PAGE_ID',function(response) {
    if( response.data ) {
        if( !isEmpty(response.data) )
            alert('You are a fan!');
        else
            alert('Not a fan!');
    } else {
        alert('ERROR!');
    }
});

// function to check for an empty object
function isEmpty(obj) {
    for(var prop in obj) {
        if(obj.hasOwnProperty(prop))
            return false;
    }

    return true;
}

代码取自我的tutorial.

虽然pages.isFan仍然适用于我,您可以使用新的PHP-SDK的FQL page_fan表:

$result = $facebook->api(array(
    "method"    => "fql.query","query"     => "SELECT uid FROM page_fan WHERE uid=$user_id AND page_id=$page_id"
));
if(!empty($result)) // array is not empty,so the user is a fan!
    echo "$user_id is a fan!";

从文档:

To read the page_fan table you need:

  • any valid access_token if it is public (visible to anyone on Facebook).
  • user_likes permissions if querying the current user.
  • friends_likes permissions if querying a user’s friend.
原文链接:https://www.f2er.com/php/140141.html

猜你在找的PHP相关文章