0

every time i get 0 after getting permission from Facebook.where do i wrong ? and what is getAccessToken() ??

i had a working script but i lost it, anyone have a working script ?

This is my code:

<?PHP
include_once "src/facebook.php";


$app_id       = "525085840939034";
$app_secret   = "80b8a0095f0af94c1329c7142f11a68d";
$site_url = 'http://'.$_SERVER['SERVER_NAME'].'/Facebook/index.php';




$facebook = new Facebook(array(
  'appId'     => $app_id,
  'secret'    => $app_secret,
  'cookie' => TRUE
  ));

$user = $facebook->getUser();

if($user){
  // Get logout URL
  $logoutUrl = $facebook->getLogoutUrl();
}else{
  // Get login URL
  $loginUrl = $facebook->getLoginUrl(array(
      'scope'         => 'read_stream, publish_stream, user_birthday, user_location, user_work_history, user_hometown, user_photos',
      'redirect_uri'  => $site_url,
      ));
}


echo $user;

?>
<a href="<?=$loginUrl?>">
Supun Praneeth
  • 3,087
  • 2
  • 30
  • 33

1 Answers1

1

To get the user you need to use getUser which is missing in your code. It should be as

$facebook = new Facebook(array(
  'appId'     => $app_id,
  'secret'    => $app_secret,
  'cookie' => TRUE
  ));

$user = $facebook->getUser();

if($user){
  // Get logout URL
  $logoutUrl = $facebook->getLogoutUrl();
}else{
  // Get login URL
  $loginUrl = $facebook->getLoginUrl(array(
      'scope'         => 'read_stream, publish_stream, user_birthday, user_location, user_work_history, user_hometown, user_photos',
      'redirect_uri'  => $site_url,
      ));
}

getAccessToken() will give you the current access token being used by the SDK.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
  • sory, i had $user = $facebook->getUser(); part forget to type here. – Supun Praneeth Feb 01 '14 at 16:22
  • well this could happen for various reason in the settings. You may check http://stackoverflow.com/questions/19039869/facebook-php-sdk-uid-facebook-getuser-always-returns-0 http://stackoverflow.com/questions/16793058/solvedfacebook-php-sdk-getuser-return-always-0 http://stackoverflow.com/questions/13375576/facebook-getuser-returns-zero http://stackoverflow.com/questions/15168764/facebook-getuser-always-returns-0-php-sdk-3-1-1 – Abhik Chakraborty Feb 01 '14 at 18:39