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
  • 2,778
  • 2
  • 30
  • 32

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.

Chris
  • 112,704
  • 77
  • 249
  • 231
Abhik Chakraborty
  • 43,914
  • 5
  • 48
  • 61
  • 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