微信网页授权获取用户基本信息

首先引导用户去授权页面:
地址:"https://open.weixin.qq.com/connect/oauth2/authorize?appid=appid&redirect_uri=redirect_uri&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
用户同意授权后,页面将跳转至 redirect_uri/?code=CODE&state=STATE

//通过code换取token
$code = $_GET['code'];
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=SECRET&code=$code&grant_type=authorization_code";
$json = file_get_contents($url);
$arr = json_decode($json,true);
$token = $arr['access_token'];
$openid = $arr['openid'];
//拿到token后就可以获取用户基本信息了
$url = "https://api.weixin.qq.com/sns/userinfo?access_token=$token&openid=$openid ";
$json = file_get_contents($url);//获取微信用户基本信息
$arr = json_decode($json,true);
$name = $arr['nickname'];//昵称
$imgURL = $arr['headimgurl'];//头像地址
$sex = $arr['sex'];//性别
$province = $arr['province'];//用户个人资料填写的省份
$city= $arr['city'];//普通用户个人资料填写的城市
$country= $arr['country'];//国家,如中国为CN








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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值