如何检查当前的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'
));
}
但如果用户喜欢该页面,则无法回复,请参阅页面here
谢谢!
答案 0 :(得分:6)
您可以使用FQL
$isFan = $facebook->api(array(
"method" => "fql.query",
"query" => "SELECT uid FROM page_fan WHERE page_id = '<YOUR PAGE ID>' AND uid = '<UID>"
));
答案 1 :(得分: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
}
}