简体   繁体   English

Facebook PHP在“页面”选项卡中获取用户信息,重定向到登录后,getUser返回0

[英]Facebook PHP Get User information in Page Tab, getUser returns 0 after redirect to login

I am trying to create a competition entry which checks to see if the user likes a page before entering. 我正在尝试创建一个竞赛条目,以检查用户在进入之前是否喜欢该页面。 If they do, it will fetch their name, email and birthday for the competition entry details. 如果他们这样做,它将获取他们的姓名,电子邮件和生日以获取比赛报名详情。

For some reason however, I cannot get my PHP code to work. 但是由于某种原因,我无法使我的PHP代码正常工作。

    <?php
    require 'src/facebook.php';

    // Create our Application instance (replace this with your appId and secret).
  $facebook = new Facebook(array(
    'appId'  => 'XXX',
    'secret' => 'XXXX',
  'cooke' => true
));

// Get User ID

$user = $facebook->getUser();

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $me = $facebook->api('/me?fields=likes.target_id(215359398497970),birthday,first_name,last_name,email');
    if($me){
        $first_name = $me['first_name'];
        $last_name = $me['last_name'];
        $email = $me['email'];
        $dob = $me['birthday'];
        die($me);
    } else {
        $likes = false;
    }
  } catch (FacebookApiException $e) {
    die($e);
    error_log($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl();
  //echo $loginUrl;
}
?>

For some reason, it gets stuck in a redirect loop because the loginURL doesn't seem to log my user in. 由于某种原因,它被卡在重定向循环中,因为loginURL似乎无法登录我的用户。

For anyone who's still looking. 对于仍在寻找的任何人。 This is how I fixed my problem. 这就是我解决问题的方式。 I had to ensure that theis code was executed BEFORE any other code so I plonked it to the very top of my php file and it worked. 我必须确保在所有其他代码之前执行了theis代码,因此我将其插到了php文件的最顶部,并且它起作用了。 I think the problem was that the headers had already been sent but I was unaware because my PHP warnings / errors are not enabled to show up. 我认为问题在于标头已经发送,但是我没有意识到,因为未启用我的PHP警告/错误。

I've encountered the same problem. 我遇到了同样的问题。 I used an example project, and couldn't figure out why it keeps returning 0. tried nearly all related links here but none solved it. 我使用了一个示例项目,却无法弄清为什么它总是返回0。这里几乎尝试了所有相关链接,但都没有解决。

The solution was to update Facebook's SDK files in the example project. 解决方案是在示例项目中更新Facebook的SDK文件。 Simple and easily missed, 简单容易错过

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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