Php – Getting Facebook user from access token (PHP SDK 3.0.1)

apifacebookPHPsdk

I have an application that utilizes it's own login and facebook login combined.
I use my own sessions and cookie to remember the user.
I want to remember facebook user but without offline_access, so i figured to set my own cookie for facebook users and when they return just get new access_token from facebook because they have already authorized the app.

I managed to do all of that when the user logins for the first time but when they come back and I detect a cookie and I get a valid new access_token I can't put it inside facebook session. I use PHP SDK 3.0.1 and there is a function

$this->fb->getUserFromAccessToken($token)

inside base_facebook.php but it just throws me a server error when I use it. (fb is my class that extends Facebook class and $token is the valid new token)

When I use

$this->fb->getUser()

it just returns 0.

I know that I could get the information from

https://graph.facebook.com/me?access_token=$access_token

but I need to put it somehow inside facebook session

Also, there is a function inside base_facebook.php that works for me

$this->fb->setAccessToken($access_token);

So I can put the token inside the class and it works but when I call getUser() afterwards it still returns 0

Any help appreciated
Thanks!

Best Answer

getUserFromAccessToken() function doesn't take any arguments. The function returns the user ID from already set access token. The function definition of getUserFromAccessToken() is shown below.

protected function getUserFromAccessToken() {
    try {
        $user_info = $this->api('/me');
        return $user_info['id'];
    } catch (FacebookApiException $e) {
        return 0;
    }
}

If you wish to find out the user ID from the access token you will need to set the access token first and then call the function getUser(). The code is shown below.

$facebook->setAccessToken($access_token);
$user_id = $facebook->getUser();