userinfo.html源码,微信网页授权snsapi_base,snsapi_userinfo区别和实现(示例代码)

微信开发文档有四部:

1 第一步:用户同意授权,获取code

2 第二步:通过code换取网页授权access_token、openid

3 第三步:刷新access_token(如果需要)

4 第四步:拉取用户信息(需scope为 snsapi_userinfo)

snsapi_base: 到第二步就结束了,获取到openid,其他操作在这个基础上(比如记录他信息)

snsapi_userinfo: 获取openid和用户资料(昵称、头像、国、省、城市、性别、权限)

public function getBaseInfo(){

//1、获取code

$appid = ‘wxe3ccbacbfd93fdeb‘;

$redirect_url = urlencode(‘http://wx.cnfxportal.com/index.php/Weixin/Index/getUserOpenId‘);

$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_url."&response_type=code&scope=snsapi_base&state=123#wechat_redirect";

header(‘location:‘.$url);

}

//获取用户openid

public function getUserOpenId(){

//2、获取网页授权的access_token

$appid = ‘wxe3ccbacbfd93fdeb‘;

$appsecret = ‘989b45dd6d2441ed01a5f59336ba2060‘;

$code = $_GET[‘code‘]; //从上面函数getBaseInfo获取得到

$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);

var_dump($res); //里面便是有openid数据

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值