微信静默登录

public function jingmo(){
            //配置参数的数组
            $CONF =  array(
                '__APPID__' =>'appid',
                '__SERECT__' =>'serect',
                '__CALL_URL__' =>'http://m.tenghuo.cn/Home/Accounts/weixincallback' //当前页地址
            );

            //没有传递code的情况下,先登录一下
            if(!isset($_GET['code']) || empty($_GET['code'])){

                $getCodeUrl  =  "https://open.weixin.qq.com/connect/oauth2/authorize".
                                "?appid=" . $CONF['__APPID__'] .
                                "&redirect_uri=" . $CONF['__CALL_URL__']  . 
                                "&response_type=code".
                                "&scope=snsapi_base". #!!!scope设置为snsapi_base !!!
                                "&state=1";

                //跳转微信获取code值,去登陆   
                header('Location:' . $getCodeUrl);
                exit;
            }

            $code =    trim($_GET['code']);
            //使用code,拼凑链接获取用户openid 
            $getTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$CONF['__APPID__']."&secret=".$CONF['__SERECT__']."&code=".$code."&grant_type=authorization_code";

            //拿到openid,下面就可以继续调起支付啦                 
            $openid =    $token_get_all->openid;         
        }
        public function weixincallback() {
            $code = $_GET['code'];
            $state = $_GET['state'];
            $appid = 'wx610e783826ad389f';
            $appsecret = 'a0395af84a7a7e1ea28fd8483fca125e';
            if (empty($code)) {
                $this->error('授权失败');
            }
            //获取access_token
            $token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $appsecret . '&code=' . $code . '&grant_type=authorization_code';
            $token = json_decode(file_get_contents($token_url));
        
            if (isset($token->errcode)) {
                echo '<h1>错误:</h1>' . $token->errcode;
                echo '<br/><h2>错误信息:</h2>' . $token->errmsg;
                exit;
            }
            $access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=' . $appid . '&grant_type=refresh_token&refresh_token=' . $token->refresh_token;
            //认证令牌合法性
            $access_token = json_decode(file_get_contents($access_token_url));
            if (isset($access_token->errcode)) {
                echo '<h1>错误:</h1>' . $access_token->errcode;
                echo '<br/><h2>错误信息:</h2>' . $access_token->errmsg;
                exit;
            }
            $is = A('Hezuoaccount')->login($access_token->openid);
            if ($is == 2) {
                A('Hezuoaccount')->registers($access_token->openid);
            }
 

转载于:https://my.oschina.net/u/3630946/blog/1503940

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值