javascript 如何检查用户是否已经喜欢 Facebook 页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7397724/
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
How to check IF user has ALREADY liked the facebook page?
提问by Gazzzzza
I have a like button on my website (which is for users to like the fb fan page).
我的网站上有一个喜欢按钮(供用户喜欢 fb 粉丝页面)。
If the user clicks like, I check to see if the even has been fired (event subscribe), and then display some content to them.
如果用户点击喜欢,我会检查偶数是否已被触发(事件订阅),然后向他们显示一些内容。
What I would really appreciate help with is the following:
我非常感谢您的帮助如下:
If the user is logged in to facebookAND they already like the page, I want it to display "You're already a fan!" and show the content. (rather than displaying the greyed out like button)
如果用户登录到 facebook并且他们已经喜欢该页面,我希望它显示“你已经是粉丝了!” 并显示内容。(而不是像按钮一样显示灰色)
OR
或者
If the user is not logged in to facebookand they click the like button, I want it to display "You're already a fan!" and show the content. (rather than displaying the greyed out like button)
如果用户未登录 Facebook并单击“赞”按钮,我希望它显示“您已经是粉丝!” 并显示内容。(而不是像按钮一样显示灰色)
edit: Guys, I've researched this on SO and have found similar questions but not quite what I'm after. Maybe I'm mistaken but if someone can provide a link to one which describes my exact problem, it would be a lot more helpful than a - on the question. I have checked the following:
Check if the user is connected to facebook and then check if he liked a page
Facebook Like Box: How to detect if user already liked the page?
Facebook LIKE button hiding when page is already LIKED by user
How to check whether user has liked the page or not using php/javascript
Check if user already likes fanpage
编辑:伙计们,我在 SO 上对此进行了研究,并发现了类似的问题,但并不是我所追求的。也许我错了,但如果有人可以提供一个链接来描述我的确切问题,那么在问题上它会比 - 更有帮助。我检查了以下内容:
检查用户是否已连接到 facebook,然后检查他是否喜欢某个页面
Facebook Like Box:如何检测用户是否已喜欢该页面?
Facebook LIKE 按钮在页面已被用户
喜欢时隐藏如何使用 php/javascript检查用户是否喜欢该页面
检查用户是否已经喜欢粉丝专页
回答by gabo bernal
If you use the facebook php API. i've came up with this short function that u can include inside the base_facebook.php file into the class BaseFacebook.
如果您使用 facebook php API。我想出了这个简短的函数,您可以将它包含在 base_facebook.php 文件中的 BaseFacebook 类中。
public function userIsFan() {
$sr = $this->getSignedRequest();
if($sr && is_array($sr)){
if(array_key_exists('page', $sr)){
return $sr['page']['liked'] == 1;
}
}
return 0;
}
回答by gfivehost
Make sure you are using xfbml, this one works for me
确保您使用的是 xfbml,这个对我有用
FB.Event.subscribe('auth.authResponseChange', function(response) {
FB.api('/me/likes/[page_id]',
function(response) {
//console.log(response); - see what the response is on your console.
if(response.data[0])
//use script to display your content alert("Liked");
else
// just do nothing, display like button alert("Not yet liked");
}
);
});
page_id will be the id not the name of your page, to find out the id, just visit http://graph.facebook.com/[your fb page name]
page_id 将是 id 而不是您的页面名称,要查找 id,只需访问 http://graph.facebook.com/[your fb page name]
hope this helps someone in the future.
希望这有助于将来的某人。