Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 283
Dan Fabulich Avatar asked Jan 11 '23 14:01

Dan Fabulich


1 Answers

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.

like image 149
Niraj Shah Avatar answered Jan 13 '23 02:01

Niraj Shah