WeChat----网站接入微信登录(后端)

后端处理

扫码后,跳转到redirect_uri 指定url, 获取code, 类似
https://pc.xxx.com/#/weChartLogin?code=0814nWQI0wTnNd2r8VRI0raZQI04nWQg&state=41HSvAje89NJt9WN
1. 通过code获得 access_token + openid
$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $appid. "&secret=" . $secret . "&code=" . $code . "&grant_type=authorization_code";
$jsonResult = file_get_contents($url);
$resultArray = json_decode($jsonResult, true);
$access_token = $resultArray["access_token"];
$openid = $resultArray["openid"];
$unionid = $resultArray["unionid"];

根据unionid 去后台请求用户信息
用户存在,返回用户信息,前端绑定用户信息,完成登录
用户不存在,跳转到登录页面,在登录参数中增加unionid 字段,正常登录,即完成登录并绑定

2. 通过access_token + openid 获得用户基本信息

微信用户头像、用户名、性别、城市等一些基本参数

$infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $access_token . "&openid=" . $openid;
$infoResult = file_get_contents($infoUrl);
$infoArray = json_decode($infoResult, true);
3. 保存,写入session,跳转会员中心
echo '<script language="javascript">window.top.location.href="/ucenter/user"</script>';

使用openid (公众号)向后台请求登录,拿到用户信息
使用unionid 统一的标识,向后台请求登录,拿到用户信息

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值