Facebook api empty response and exception

 $facebook = new Facebook(array(
                'appId'  =>Yii::app()->params['FBappId'],
                'secret' =>Yii::app()->params['secret'] ,
                ));

                // Get User ID
                $user = $facebook->getUser();
              if ($user) {
              try {
                // Proceed knowing you have a logged in user who authenticated.
                echo $accessToken=$facebook->getAccessToken();
                echo '<br/>';
                $user_profile = $facebook->api('/me');
                print_r($user_profile); echo "normal sdk"; exit;
              } catch (FacebookApiException $e) {
                  echo $e; exit;
                $user = null;
              }
            }
            echo "normal sdk end";exit;

      

I tried with php sdk 3.3.2 and user login via facebook, it prints the token and user info before the token expires. after about an hour I check the token debug session has expired and it gives an exception once and then an empty response. But in another tab it accesses facebook. my question is:

1.When the token has expired, is there any inbuild method to get a valid token again?

2.Why an empty answer? it should throw an exception every time

0


source to share


1 answer


You can use the Facebook Debug Tool to check if your access token is correct.

The normal access token expires in 2 hours, and the extended access token lasts 2 months.

If you want an extended token, go though this: How to extend token expiration from offline_access cancellation



Well if you get empty for $ facebook-> api ('/ me'); it simply means that the user has not yet authorized the application. Try something like this -

$user_id = $facebook->getUser();
echo $uid;

if($user_id){
  try {
    $user_profile = $facebook->api('/me');

  } catch (FacebookApiException $e) {
    error_log($e);
    $user_id = null;
  }
}else{
    $login_url = $facebook->getLoginUrl();
    echo("<br>login url=".$login_url);
};

      

+1


source







All Articles