一、说明

1、这里不详细讲解网页授权接口开发流程,主要给出主要的代码
2、详细开发请参考开发手册:
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842
3、慕课上的视频教程做了纤细讲解:
https://www.imooc.com/video/11345
4、可以参考我前面几篇关于微信公众号开发的博客,有的地方衔接需要了解
5、代码部分讲了snsapi_base和snsapi_userinfo两种授权方式

二、代码实现

/*
    *$url 接口url string
    *$type 请求类型 string
    *$res 返回数据类型 string
    *%$arr post 请求参数 string
    */
    public function http_curl($url,$type='get',$res='json',$arr=''){
        //1.初始化curl
        $ch = curl_init();
        //2.设置curl的参数
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if($type == 'post'){
            curl_setopt($ch, CURLOPT_POST,1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
        }
        //3.采集
        $output = curl_exec($ch);
        //4.关闭
        curl_close($ch);
        if($res=='json'){
            if(curl_error($ch)){
                //请求失败,返回错误信息
                return curl_error($ch);
            }else{
                //请求成功,返回错误信息
                return json_decode($output,true);
            }
        }
    }

    //获取用户的openid(snsapi_base)
    public function getBaseInfo(){
        //1、获取code
        $appid = "wxb20bebb764546087";
        $redirect_uri = urlencode("http://www.cxf001.top/weixin.php/Index/getUserOpenId");
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
        header('location:'.$url);
    }
    
    public function getUserOpenId(){
        //2、获取到网页授权的access_token
        $appid = "wxb20bebb764546087";
        $appsecret = "64e22d215895d52af21c8fcbf6da3241";
        $code = $_GET['code'];
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
        //3、拉去用户的openid
        $res = $this->http_curl($url,'get');
        var_dump($res);
    }
    
    //获得详细授权(snsapi_userinfo)
    public function getUserDetail(){
        //1、获取code
        $appid = "wxb20bebb764546087";
        $redirect_uri = urlencode("http://www.cxf001.top/weixin.php/Index/getUserInfo");
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
        header('location:'.$url);
    }
    public function getUserInfo(){
        //2、获取到网页授权的access_token
        $appid = "wxb20bebb764546087";
        $appsecret = "64e22d215895d52af21c8fcbf6da3241";
        $code = $_GET['code'];
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
        $res = $this->http_curl($url,'get');
        $openid = $res['openid'];
        $access_token = $res['access_token'];
        //3、拉去用户的详细信息
        $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
        $res = $this->http_curl($url);
        var_dump($res);
    }

本文由 一切随风 创作,可自由转载、引用,但需署名作者且注明文章出处。

还不快抢沙发

添加新评论