8

ユーザーがログインボタンをクリックしたときにログインポップアップを表示するために、Facebook の JavaScript SDK を使用しています。

Facebookがドキュメントで提供しているように、コードは次のとおりです。

$(".loginButton").click(function(){
 FB.login(function(response) {
    FB.api('/me', function(response) {
       console.log(response.id);
       //User ID shows up so I can see that the user has accepted the app.
     });
 });

FB.getLoginStatus()また、ユーザーが実際にログインしてアプリケーションを受け入れたことを確認するためにも使用できます。

しかし、今は PHP で関数を実行したいと考えています。私が理解している限り、PHP には$user、ログインが成功した後に UserID が割り当てられます。

問題は、JS ログイン$userがまだ0. 立ち往生していて、正しいユーザー ID が割り当てられない理由がわかりません$user

ここに私のJSがあります:

    <script type="text/javascript">  

        window.fbAsyncInit = function() {
            FB.init({
                appId  : '<?php echo $AppId; ?>',
                status : true, // check login status
                cookie : true, // enable cookies to allow the server to access the session
                xfbml  : true,
                channelUrl  : '<?php echo $ServerPath; ?>channel.php' // custom channel
            });

    }; 


    //Get Login Status
    function amILoggedIn(){
        var toreturn = "";
        FB.getLoginStatus(function(response) {
            if (response.status === 'connected') {
                window.userId =  response.authResponse.userID;
            toreturn = "GetLoginStatus: logged in" + " " + "<?php echo $user; ?>";
            } else {
            toreturn = "GetLoginStatus: Logged Out";
            }
            console.log(toreturn);
        }); 


    };


    // Load the SDK asynchronously
    (function(d, s, id){
         var js, fjs = d.getElementsByTagName(s)[0];
         if (d.getElementById(id)) {return;}
         js = d.createElement(s); js.id = id;
         js.src = "//connect.facebook.net/en_US/all.js";
         fjs.parentNode.insertBefore(js, fjs);
       }(document, 'script', 'facebook-jssdk'));

            var obj = {
                    method: 'feed',
                    link: 'http://www.google.com'
                };


    $(".loginPopupButton").click(function(){
         FB.login(function(response) {
            FB.api('/me', function(response) { //user-specific stuff
               console.log(response.id);
             });
         });

    });

    </script>

そして、ここにPHPがあります:

    <?php
    include_once('fbapi/facebook.php');
    include_once('fbapi/Authenticated_User.php');
    include_once('config.php');


    // Create our Application instance.
    $facebook = new Facebook(array(
                    'appId'  => $AppId,
                    'secret' => $AppSecret
                ));
    //Facebook Authentication part
    $user = $facebook->getUser();


    $perms = "manage_pages";
    if(isset($_GET['backlink']))
        $redirectUrl   = urldecode($_GET['backlink']);
    else
        $redirectUrl   = $fullserverpath;

    $cancelUrl   = $ServerPath."index.php?page=loginWFb";

    //AA - where to go to get FB popup.    
    $loginUrl = $facebook->getLoginUrl(
            array(
                'scope' => $perms,
                'redirect_uri' => $redirectUrl,
                'cancel_uri' => $cancelUrl
            )
    );



    //AA- Defining that a powerUSER is someone who's logged in
    if(isset($user)){
        $_SESSION['loggedIn'] = $user;

    }

    ?>
4

2 に答える 2

4

JS SDK を使用したログインではログイン データが Facebook の Cookie に保存されますが、PHP SDK を使用したログインではデータが PHP セッションに保存されます。したがって、JS SDK でログを記録しても、PHP SDK はそれを認識しません。

JS SDK データを使用して PHP SDK でログインするには、クエリ パラメータで署名付きリクエストを使用して PHP ページを呼び出す必要があります。

FB.getLoginStatus(function(response) {
    if (response.status === 'connected') {
        // Full redirect or ajax request according to your needs
        window.location.href = 'login.php?signed_request='+response.authResponse.signedRequest;
    }
});

次に$facebook->getUser()、PHPコードでユーザーIDを提供します。

于 2013-10-08T08:25:14.603 に答える
0

PHP で Facebook オブジェクトを初期化するときは、sharedSessionパラメーターをtrue次のように設定してみてください。

$facebook = new Facebook(array(
    'appId'         => $AppId,
    'secret'        => $AppSecret,
    'sharedSession' => true
));
于 2013-10-08T10:12:39.117 に答える