1

下面的代码工作正常,除了我想用 FB 按钮获取$_GET['code']$_GET['state']返回 REDIRECT_URI

使用<script>top.location.href='".$login_url."'</script>没问题,我可以在 URL 中看到“代码”和“状态”值

但是当我使用<fb:login-button size="medium" onlogin="Log.info(\'onlogin callback\')">Connect with Facebook</fb:login-button>(在打开的窗口弹出窗口中)重定向时,URL 中没有“代码”和“状态”值!

我正在寻找类似于登录页面中两种情况的内容:

http://www.example.com/test/signin.php?code=AQD1NEVmQPPB8Ed1oV2Ifiuzf5lgFvPoQUL-mNdvrW8w54eOZbrfo28vO9ES0wwTSrMpOQE3GwNriE8CQoEd7nJPuuykXZdglGn162RujKL9LSgVgUusDxmnXOYD0TXCI4pi2UTfLaYPQ8fyrwTgijGTQwZagenJL2JFDnhVkHYPDjz3S38szoKDJApoSfBfhD6_hEawyouGTtHXT0Cm0-CxyzYoOk0mcmmX-iaXVFnWgdvEGB1a6dPUCi3Cw_S4NqJkj8IKh-lI8pGkNjFbVR693dDre0J4BDwiPtWCzofyrpmj_jVwAIYX5SwEvbowAE&state=5e809fa0f911d39ff99e4e279b48356#_=_

谢谢你的帮助...

<?php
    define('YOUR_APP_ID', 'xxx');
    define('YOUR_APP_SECRET', 'xxx');
    define('REDIRECT_URI',"http://www.example.com/test/signin.php");
    $userId = null; 
    //
    // new facebook object to interact with facebook
    $facebook = new Facebook(array(
     'appId' => YOUR_APP_ID,
     'secret' => YOUR_APP_SECRET,
     'cookie' => true
    ));
    //
    // if user is logged in on facebook and already gave permissions
    // to your app, get his data:
    $userId = $facebook->getUser();

    if($userId == 0) {
        // If the user is not connected to your application, redirect the user to authentication page
        /**
         * Get a Login URL for use with redirects. By default, full page redirect is
         * assumed. If you are using the generated URL with a window.open() call in
         * JavaScript, you can pass in display=popup as part of the $params.
         * 
         * The parameters:
         * - redirect_uri: the url to go to after a successful login
         * - scope: comma separated list of requested extended perms
         */

        $login_url = $facebook->getLoginUrl($params = array('redirect_uri' => REDIRECT_URI));
        echo ("<script> top.location.href='".$login_url."'</script>");
        // Using the button doesn't show code and state value !
        //print ('<fb:login-button size="medium" onlogin="Log.info(\'onlogin callback\')">Connect with Facebook</fb:login-button>');


    } else {
        // if the user is already connected, then redirect them to landing page or show some content
        //if ( $_GET['code'] != '' && $_GET['state'] != '' ) { echo 'Code : '.$_GET['code']; exit; }
        $userInfo = $facebook->api('/' + $userId);
        echo ("<script> window.location.href='".REDIRECT_URI."'</script>");
    }
?>
4

0 回答 0