php 在 GRAPH API 中检查页面的用户粉丝的方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4970439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's method for checking user fan of a page in GRAPH API?
提问by John
In graph api, Pages.isFanmethod not working, what's method for checking user fan of a page in graph api?
在图形 api 中,Pages.isFan方法不起作用,在图形 api中检查页面的用户粉丝的方法是什么?
Thanks.
谢谢。
回答by ifaour
UPDATE 2:
To check if the current user is a fan of the Facebook page on landing at your tab check this answer.
更新 2:
要检查当前用户是否是登陆您的标签时 Facebook 页面的粉丝,请检查此答案。
UPDATE:
You can use the likes
connection to check if a user is a fan of a page:
更新:
您可以使用likes
连接来检查用户是否是某个页面的粉丝:
https://graph.facebook.com/me/likes/PAGE_ID
&access_token=ACCESS_TOKEN
This would return either an empty data array:
这将返回一个空数据数组:
Array
(
[data] => Array
(
)
)
Or if fan:
或者如果风扇:
Array
(
[data] => Array
(
[0] => Array
(
[name] => Real Madrid C.F.
[category] => Professional sports team
[id] => 19034719952
[created_time] => 2011-05-03T20:53:26+0000
)
)
)
So this is how we check using the PHP-SDK:
所以这就是我们如何使用 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
?>
Similar script usingJS-SDK:
使用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;
}
Code taken from my tutorial.
代码取自我的教程。
While pages.isFan
is still working for me, you can use the FQL page_fan
table with the new PHP-SDK:
虽然pages.isFan
仍在为我工作,但您可以将 FQLpage_fan
表与新的 PHP-SDK 一起使用:
$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!";
From the documentation:
从文档:
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.
要阅读 page_fan 表,您需要:
- 任何有效,
access_token
如果它是公开的(Facebook 上的任何人都可以看到)。user_likes
如果查询当前用户,则权限。friends_likes
如果查询用户的朋友,则权限。
回答by Jason Siffring
Here's another approachthat relies on the signed_request POST variable that Facebook sends to every tab app page (if you have the "OAuth 2.0 for Canvas" advanced option turned on.
这是另一种方法,它依赖于 Facebook 发送到每个标签应用程序页面的 signed_request POST 变量(如果您打开了“OAuth 2.0 for Canvas”高级选项。
The nice thing about this is you don't need to send another fql.query.
这样做的好处是您不需要发送另一个 fql.query。
回答by Agent_x
Or you can use fbml if you whant for example show some content if they are fan or made some dinamic stuff.
或者你可以使用 fbml,如果你想显示一些内容,如果他们是粉丝或制作了一些动态的东西。
example:
例子:
<fb:fbml version="1.1">
<fb:visible-to-connection>This part is visible for fans only!
<fb:else>This part is visible for non-fans</fb:else>
</fb:visible-to-connection>
</fb:fbml>