Dan Fabulich
Dan Fabulich

Reputation: 39603

Get Facebook user ID in FB PHP SDK 4.0

In FB SDK 3.0, you could run $facebook->getUser() to get the user's Facebook UID.

SDK 4.0 doesn't have a BaseFacebook object any more; I can't see how to get the user ID off of a FacebookSession created by FacebookJavaScriptLoginHelper. How do you do it?

Upvotes: 4

Views: 8597

Answers (4)

Niraj Shah
Niraj Shah

Reputation: 15457

If you already have an active session or an access token, you can do the following to get the user_id:

// set session from cookie or via helper
$session = new FacebookSession( $session->getToken() );
$user_id = $session->getSessionInfo()->asArray()['user_id']
echo $user_id

See this tutorial for information on using a session saved in a cookie or creating a new session. It's a long solution but this the easiest way to retrieve the user_id using the new SDK. It's advisable to save it in a session for easier retrieval.

Upvotes: 7

BrandoAZ
BrandoAZ

Reputation: 11

If you already have an active session called '$session' then you just use:

$session->getUserId();

In my code I log on with Javascript SDK, then in PHP:

FacebookSession::setDefaultApplication($api_key, $api_secret);
$helper = new FacebookJavaScriptLoginHelper();
try {
    $session = $helper->getSession();
    $output = $session->getUserId();
} catch(FacebookRequestException $ex) {
    // When Facebook returns an error
    $output = '-1';
} catch(\Exception $ex) {
    // When validation fails or other local issues
    $output = '-2';
}

Upvotes: 0

ep0
ep0

Reputation: 700

I use Facebook SDK 4.0.9. I needed to get the user id when making AJAX calls and my setup is as follows:

  • when constructing fb sdk wrapper, call FacebookSession::setDefaultApplication(<ID>, <SECRET>

  • defined a function get_user_id as follows:

    public function get_user_id() {
        $sr = new SignedRequest((new FacebookJavaScriptLoginHelper())->getRawSignedRequestFromCookie());
        return $sr->getUserId();
    }
    

getRawSignedRequestFromCookie is defined here https://github.com/facebook/facebook-php-sdk-v4/blob/master/src/Facebook/Helpers/FacebookSignedRequestFromInputHelper.php#L159-L165

I chose FacebookJavaScriptLoginHelper for convenience, but I think you can opt for any (I guess): https://github.com/facebook/facebook-php-sdk-v4/tree/master/src/Facebook/Helpers

Upvotes: 0

Dan Fabulich
Dan Fabulich

Reputation: 39603

As of PHP SDK v4.0.3 there's no way to get the user ID without doing a network request. This is unfortunate because the signed request already contains the user_id, so it seems silly to do a network request just for that one piece of data.

But, if you're willing to do the network request, $session->getSessionInfo()->getId() will retrieve the full session info from /debug_token, including the user ID.

You might prefer to do a FacebookRequest for /me, which will provide the user ID and the user's profile info. (But it's more typing.)

$me = (new FacebookRequest(
  $session, 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className());
echo $me->getId();

Upvotes: 2

Related Questions