facebook->getUser() returns 0

2020-03-26 06:37发布

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条回答
Root(大扎)
2楼-- · 2020-03-26 07:13

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

查看更多
登录 后发表回答