facebook->getUser() returns 0

2020-03-26 06:36发布

问题:

I assume that this is because users have to grant some sort of access to my facebook application, for getUser to be available to me? Here is my code:

 <? require 'facebook.php'; ?>
 <?php

 $facebook = new Facebook(array(
 'appId' => '[//fb app id]',
 'secret' => '[//fb app secret]',
 'cookie' => true
 ));

 $uid = $facebook->getUser();
 echo $uid;

 ?>

So I guess my question is, short of prompting the user to give permission to my app, is there any other sort of unique identifier that I can grab from a fb user to prevent them from submitting more than 1 entry to my app. I have user's add submissions, but I only want to allow 1 submission per user. Thanks for the help!

回答1:

To get user id, user have to grant permission

if($uid){
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');

  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}else{
    $loginUrl = $facebook->getLoginUrl();
    echo("<br>login url=".$loginUrl);
};

After the user grant permission (see the loginUrl), you may access user id