thinkphp5微信授权获取用户信息

备忘使用

首先第一步注册微信公众号,填写必要信息,如果没有可以先申请测试公众号调试使用
1.申请测试公众号地址:http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
2.进入后扫码关注,如下图在这里插入图片描述
3.扫描关注公众号
4.在后台填写回调地址,如下图:在这里插入图片描述
在这里插入图片描述
5.接下来就是进行访问了

namespace app\index\controller;
use think\Controller;
class Test extends Controller
{
	//首页
    public function index()
    {
        $appid = "你的appid";
        $redirect_uri = urlencode("回调地址");//刚才填在后台的域名
        $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";//拼接访问地址
		//上边的授权分两种snsapi_userinfo是显式授权   snsapi_base为静默授权
        $this->redirect($url);//tp中重定向访问地址
    }
	
	//回调地址
    public function huid()
    {
        $appid = "你的appid";
        $appsecrt = "你的appsecret";
        $code = $_GET['code'];//接受微信返回的code参数
   	
   		//再次拼接访问地址
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecrt."&code=".$code."&grant_type=authorization_code";

        $detail = file_get_contents($url);//获取上边url地址返回的内容,json格式
        $temdetail = json_decode($detail);

        $openid = $temdetail->openid;//用户openid
        $access_token = $temdetail->access_token;//获取token
        
        //拼接地址
        $url1 = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
		
		//获取返回信息 openid、nikename、headpic等
        $userdetail = file_get_contents($url1);
        var_dump($userdetail);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值