0

私の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();有効なユーザーが返されない場合は、ページを自動的にリロードする必要がありますか?

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 に答える