简体   繁体   中英

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();

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

$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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM