在进入我的应用程序之前,我使用此代码来检查用户是否"喜欢"了该页面。
require_once 'facebook-php-sdk/src/facebook.php';
// Create our Application instance.
$this->facebook = new Facebook(array(
'appId' => 'APPID',
'secret' => 'APPSECRET',
'cookie' => true,
));
$session = $this->facebook->getSession();
if(!empty($session)) {
$access_token = $this->facebook->getAccessToken();
$fql_multiquery_url = 'https://graph.facebook.com/me/likes?access_token='.$access_token;
$fql_multiquery_result = file_get_contents($fql_multiquery_url);
$fql_multiquery_obj = json_decode($fql_multiquery_result, true);
$liked = false;
foreach($fql_multiquery_obj['data'] as $like){
if($like['id'] == 'PageID'){
$liked = true;
}
}
if($liked){
$data['main_content'] = 'welcome_message';
} else {
$data['main_content'] = 'before_like';
}
$this->load->view('includes/template', $data);
} else {
$req_perms = "publish_stream,offline_access,user_status,email,read_stream,user_likes";
$login_url = $this->facebook->getLoginUrl(array('canvas'=> 1,'fbconnect' => 0,'req_perms' => $req_perms, 'redirect_uri' => 'APP REDIRECT URL'));
echo "<script type='text/javascript'>top.location.href = '$login_url';</script>";exit;
}
(我知道循环浏览你的点赞不是最好的解决方案,但对我来说,这似乎是最一致的)。
它对我来说很好(办公室里的其他几个用户也很好),但对一些用户来说却失败了(当然他们不会给我发错误消息)。有没有更好的方法可以检查点赞并保持一致?
您应该能够访问这样的特定页面:
$resp = $this->api('/me/likes/'.$page_id);
$is_fan = (count($resp['data']) !== 0);
然而,如果facebook的内部缓存出现问题,这也无济于事,而且如果此请求在facebook选项卡上运行,则签名的请求也应该在signed_request
中包含fan/no_filan信息(请参阅有关字段和值的部分)。根据我的经验,signed_request
似乎是最可靠的。
P.S.:
您似乎使用了一个相当旧的php-sdk版本,getSession()
方法已被弃用。
function parsePageSignedRequest() {
if (isset($_REQUEST['signed_request'])) {
$encoded_sig = null;
$payload = null;
list($encoded_sig, $payload) = explode('.', $_REQUEST['signed_request'], 2);
$sig = base64_decode(strtr($encoded_sig, '-_', '+/'));
$data = json_decode(base64_decode(strtr($payload, '-_', '+/'), true));
return $data;
}
return false;
}
$signed_request = $_REQUEST['signed_request'];
//check for page liked or not
if($signed_request = parsePageSignedRequest())
{
if($signed_request->page->liked) {
echo "<link rel='stylesheet' type='text/css' href='style.css' />";
} else {
echo "<link rel='stylesheet' type='text/css' href='notfanstyle.css' />";
}
}
:)试试这个,它对我有效。