PHP微信公众平台开发高级篇—网页授权接口

一、说明

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);
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值