PHP 微信授權地址設置多個 多域名授權

public function get_code() {
        $appid = '';
        $scope = 'snsapi_userinfo';
        $state = '';
        $code = '';
        $redirect_uri = '';
        $device = '';
        $protocol = 'https';

        if (isset($_GET['device'])) {
            $device = $_GET['device'];
        }

        if (isset($_GET['appid'])) {
            $appid = $_GET['appid'];
        }
        if (isset($_GET['state'])) {
            $state = $_GET['state'];
        }
        if (isset($_GET['redirect_uri'])) {
            $redirect_uri = $_GET['redirect_uri'];
        }
        if (isset($_GET['code'])) {
            $code = $_GET['code'];
        }
        if (isset($_GET['scope'])) {
            $scope = $_GET['scope'];
        }

        if ($code == 'test') {
            exit;
        }

        if (empty($code)) {
            $authUrl = '';
            if ($device == 'pc') {
                $authUrl = 'https://open.weixin.qq.com/connect/qrconnect';
            } else {
                $authUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize';
            }

            $options = [
                $authUrl,
                '?appid=' . $appid,
                '&redirect_uri=' . urlencode($protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '/'),
                '&response_type=code',
                '&scope=' . $scope,
                '&state=' . $state,
                '#wechat_redirect'
            ];

            //把redirect_uri先寫到 session
            $this->session->set_userdata('redirect_uri', $redirect_uri);

            header('Location: ' . implode('', $options));
        } else {
            if (isset($_COOKIE['redirect_uri'])) {
                $back_url = $this->session->userdata('redirect_uri');
                header('Location: ' . implode('', [
                            $back_url,
                            strpos($back_url, '?') ? '&' : '?',
                            'code=' . $code,
                            '&state=' . $state
                ]));
            }
        }
    }

調用代碼

http://****.com?appid=***&redirect_uri=******&response_type=code&scope=snsapi_userinfo&state=****

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章