facebook->getUser returns 0
I have a Facebook fan page and I want to check if the user visiting the page LIKES the page in order to show the content. It works when I am logged in as the administrator but when another account visits the page the facebook->getUser()
always return 0
require 'src/facebook.php';
$facebook = new Facebook(array开发者_开发技巧(
'appId' => 'ID',
'secret' => 'HASH',
'cookie' => true,
));
$user = $facebook->getUser();
//Page id
$page = '215010578537112';
$likeID = $facebook->api(array( 'method' => 'fql.query', 'query' =>
'SELECT uid FROM page_fan WHERE uid =' . $user . 'AND page_id =' .$page ));
if ( $likeID ) {
}else{
}
Try this version, you shouldn't need anything special to check if they like your page:
// find out if the user likes the page or not
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
// content to show if they don't like the page
if (empty($data["page"]["liked"])) {
?>
<p>Not a fan</p>
<?php
// content to show if they do like the page
} else {
<div>Is a fan</div>
Since this is a fan page, you don't need to call an FQL query, since an FQL query requires that the user has first authorized your application. Facebook will send a signed_request post parameter to your iframe fan page. See this answer on how to decode that signed_request parameter and check to see if they are a fan of your page or not.
精彩评论