微信授权

    public function getCodeUserInfo($tem)
    {

        if($_SERVER['REQUEST_METHOD']=='OPTIONS') exit;
        //获取code
        $redirect_uri = urlencode("http://******/控制器/方法(getWxUserInfo)");//回调地址
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $this->gappid . "&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=$tem&connect_redirect=1#wechat_redirect"; //state 可任意
        $this->redirect($url, 302);// tp5的重定向方法
    }

    public function getWxUserInfo()
    {
        if($_SERVER['REQUEST_METHOD']=='OPTIONS') exit;
        //换取网页授权access_token
        //获取openid
        $code = $this->request->param('code');
        $state = 123;
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->gappid . "&secret=" . $this->gappsecret . "&code=" . $code . "&grant_type=authorization_code";
        $rjson = $this->curl($url);
        $rjson = json_decode($rjson);

        $access_token = $rjson->access_token;//得到access_token ( 网页的)
        $openId = $rjson->openid;//得到openid

        $userInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=" . $this->gappid . "&lang=zh_CN";//获取用户信息
        $result = json_decode($this->curl($userInfoUrl));

      
        return json(['status' => 'ok', 'openid' => $result->openid]);
    }

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值