Blogger Information
Blog 16
fans 0
comment 2
visits 13449
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
微信公众号网页授权-2018-06-02
Alan_繁华
Original
997 people have browsed it

微信网页授权登录流程如下:

1、获取code

2、通过code获取access_token及openid

3、通过access_token和openid获取用户详细信息

具体操作步骤如下:

1、获取code

实例

<?php
//用户同意授权
    public function auth(){
//公众号的appid和appsecret
        $appid = config('app.weixin_appid');
        $secret = config('app.weixin_appsecret');
//REDIRECT_URI 当appid和appsecret验证成功后要跳转的URL,并带有code
        $REDIRECT_URI = 'http://f2f6c25d.ngrok.io/index.php/index/weixin/get_userinfo';
        $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.urlencode($REDIRECT_URI).'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
        header('Location:'.$url);
    }

运行实例 »

点击 "运行实例" 按钮查看在线实例

2、通过code获取access_token及openid,并获取用户详细信息

实例

<?php
 //获取用户信息
    public function get_userinfo(){
//获取微信返回的code
        $code = input('get.code');

        //通过code获取access_token
        $res = $this->WinxinMode->auth_access_token($code);
       //如果获取access_token不成功或者用户刷新导致code丢失,则重定向再次获取code
        if(!$res){
            header('Location:http://f2f6c25d.ngrok.io/index.php/index/weixin/auth');
            exit();
        }

        $access_token = $res['access_token'];
        $openid = $res['openid'];
//通过access_token和openid获取用户基本信息
        $result = $this->WinxinMode->get_userinfo($access_token,$openid);
        dump($result);

    }

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例中用到的auth_access_token和get_userinfo方法如下

实例

<?php
  /医院
     * 获取用户access_token和openid
     * @param $code    微信返回的code参数
     * @return          如果获取成功,则返回结果,如果失败则返回false
     */
    public function auth_access_token($code){
        $appid = config('app.weixin_appid');
        $secret = config('app.weixin_appsecret');
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
        $res = http_Get($url);
        $res = json_decode($res,true);
        if(!isset($res['access_token'])){
            return false;
        }
        return $res;
    }

    /医院
     * @param $access_token     微信返回的access_token
     * @param $openid           微信返回的openid
     * @return mixed|string     如果获取成功,则返回用户信息,如果失败,则返回false
     */
    public function get_userinfo($access_token,$openid){
        $url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
        $res = http_Get($url);
        $res = json_decode($res,true);
        if(isset($res['errcode'])){
            return false;
        }
        return $res;
    }

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments