Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the facebook PHP sdk equivalent to FB.getLoginStatus?

What is the PHP equivalent to Facebook's JS SDK of:

FB.getLoginStatus

I'm looking for something like:

$facebook->getLoginStatus()

However the above does not exist in the PHP library.

I have already tried both of these:

$this->facebook->api('/me');
$this->facebook->getUser();

Both do not work because PHP errors out, saying that I need a valid access_token and I am left with a blank page. I am unable to even check for a null response for the 2 methods above.

I simply need to check if a user is a returning user or not. Please help!

like image 202
Rees Avatar asked Mar 01 '12 07:03

Rees


1 Answers


$facebook = new Facebook(array(
  'appId'  => 'YOUR_APP_ID',
  'secret' => 'YOUR_APP_SECRET',
));

// Get User ID
$user = $facebook->getUser();

To check if the user is logged in on facebook (similar to FB.getLoginStatus) you need to do:


if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}
else {
  $fbUrl = $facebook->getLoginUrl(array('scope' => '........'));
  header("Location:".$fbUrl);
  exit();
}

Hope it helps

like image 91
Sudhir Bastakoti Avatar answered Oct 28 '22 21:10

Sudhir Bastakoti