0

The FIRST time my index.php loads I get a value of 0 when calling:

$facebook->getUser();//returning 0 instead of id on first page load

The call works fine on any subsequent page load.

This is my code:

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

Has anyone else experienced this? Maybe I should automatically reload the page if $facebook->getUser(); does not return a valid user?

4

1 回答 1

-1

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

<?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('/me','GET');

对此:

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

希望这能解决您的问题。

于 2013-02-22T15:09:26.023 回答