微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – Facebook getLoginUrl和下一个参数无法正常工作

我的代码

$url = $fb->getLoginUrl(array('scope'=>'email','next'=>'http://apps.facebook.com/APPID/'));
echo "<script> top.location=\"".$url."\"; </script>";

当验证成功时,我需要将用户重定向到我的应用程序的应用程序URL,但它总是重定向到我的redirect_uri页面.

我该怎么办呢?

谢谢.

解决方法:

您必须更改此URL以在身份验证后将应用程序重定向到您想要的位置.

或者你可以这样做

首先,您不必编辑PHP SDK,下面是验证用户身份然后重定向到您的登录页面的示例,

确保您替换:

你的app-id-HERE和你的facebook应用程序ID,

你的APP-API-SECRET-HERE和你的facebook应用程序密钥

您的REDIRECT-URL-HERE与您的目标网页网址

<?PHP

    // Requires Facebook PHP SDK 3.0.1: https://github.com/facebook/PHP-sdk/
    require ('facebook.PHP');

    define('FACEBOOK_APP_ID',"YOUR-app-id-HERE");
    define('FACEBOOK_SECRET',"YOUR-APP-API-SECRET-HERE");
    define('REDIRECT_URI',"YOUR-REDIRECT-URL-HERE");
    $user = null;

    $facebook = new Facebook(array(
        'appId' => FACEBOOK_APP_ID,
        'secret' => FACEBOOK_SECRET,
        'cookie' => true
    ));

    $user = $facebook->getUser(); // Get the UID of the connected user, or 0 if the Facebook user is not connected.

    if($user == 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>");

    } else {
        // if the user is already connected, then redirect them to landing page or show some content
        echo ("<script> window.location.href='".REDIRECT_URI."'</script>");
    }

?>

如果要获得扩展权限,只需在登录URL中添加一个“scope”参数,例如:

$login_url = $facebook->getLoginUrl($params = array('redirect_uri' => REDIRECT_URI,'scope' => 'comma-separated-list-of-requested-extended-perms'));

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐