avien
avien

Reputation: 177

Facebook sdk using php: how to get theuserid of the connected user

how can i get the current logged in userid ?

im pretty sure my appId is correct

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

im using this method but i returns 0;

$facebook->getUser();

Upvotes: 1

Views: 273

Answers (3)

Gema Megantara
Gema Megantara

Reputation: 520

If you want to get facebook user id with this code

$facebook = new Facebook(array(
   'appId'  => 'your_app_id',
   'secret' => 'your_app_secret',
));     
$user = $facebook->getUser();

You have condition to should have done before, the first condition is you put that code in the page that redirected after the user login via fb provide by this code

$params = array(
    'display'=>'popup',
    'redirect_uri' => 'http://example.com'
);
$loginUrl = $facebook->getLoginUrl($params);

OR the second condition is you already have the user 'Access Token', so the code will be like this

$facebook = new Facebook(array(
   'appId'  => 'your_app_id',
   'secret' => 'your_app_secret',
));     
$facebook->setAccessToken('user_access_token');
$user = $facebook->getUser();

If you don't clear about the Facebook Login flow using PHP SDK, you can visit this page

Upvotes: 2

Mahmut Duman
Mahmut Duman

Reputation: 427

$myData=$facebook->api('/me');

$myid=myData['id'];

$myName=myData['name'];

$myUsername=myData['username'];

[id] => your id
[name] => your full name
[first_name] => your firs name
[last_name] => lastname
[link] => profil link
[username] => username
[gender] => your gender (male,female)
[email] => your mail (if have email permission)
[timezone] => 2
[locale] => your locale
[verified] => 1

Upvotes: 1

vcampitelli
vcampitelli

Reputation: 3252

Follow this guide: Login for Server-side Apps. Basically, you have to generate a login URL, receive a code generated by Facebook, and with this code, request for an access token.

Upvotes: 0

Related Questions