Facebook PHP SDK在第一页加载时失败

Facebook PHP SDK在第一页加载时失败,php,facebook,facebook-php-sdk,Php,Facebook,Facebook Php Sdk,第一次加载my index.php时,调用时得到的值为0: $facebook->getUser();//returning 0 instead of id on first page load 该调用在任何后续页面加载中都能正常工作 这是我的代码: $facebook = new Facebook(array( 'appId' => $FB_APP_ID, 'secret' => $FB_APP_SECRET )); $user_

第一次加载my index.php时,调用时得到的值为0:

$facebook->getUser();//returning 0 instead of id on first page load
该调用在任何后续页面加载中都能正常工作

这是我的代码:

$facebook = new Facebook(array(
        'appId'  => $FB_APP_ID, 
        'secret' => $FB_APP_SECRET
    ));
$user_id = $facebook->getUser();//Returns 0 on first load.

还有其他人经历过吗?如果
$facebook->getUser(),也许我应该自动重新加载页面未返回有效用户?

可能不是第一次对其进行身份验证,您可以执行以下操作:

<?php

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

    $user_id = $facebook->getUser();

    if($user_id) {

      // We have a user ID, so probably a logged in user.
      // If not, we'll get an exception, which we handle below.
      try {

        $me = $facebook->api('/me','GET');
        echo "Name: " . $me['name'];

      } catch(FacebookApiException $e) {
        // If the user is logged out, you can have a 
        // user ID even though the access token is invalid.
        // In this case, we'll get an exception, so we'll redirect and
        // just ask the user to login again here.
        $login_url = $facebook->getLoginUrl(); 
        echo("<script>top.location.href = '" . $loginUrl . "';</script>");
      }   
    } else {

      // No user, redirect for the user to login
      $login_url = $facebook->getLoginUrl();
      echo("<script>top.location.href = '" . $loginUrl . "';</script>");

    }

  ?>
为此:

$me= $facebook->api('/'.$user_id, 'GET');

希望这能解决您的问题。

试试捕捉怎么样?我捕捉到了错误,为了简洁起见,我没有显示。您是否检查了getUser首先返回的内容?Get user返回0(更新了问题)我尝试了检查错误,然后再次调用new Facebook(),但这并不能解决问题,只有重新加载页面似乎有效。谢谢,但这没有帮助,因为用户id为0:)$me=$facebook->api('/'.$user_id,'GET');我不想将用户发送到facebook登录页面,因为他们可能不是facebook用户。但访问令牌只授予facebook用户!显然,非facebook用户将不会使用该页面的facebook功能!
$me= $facebook->api('/'.$user_id, 'GET');