微信网页授权snsapi_base,snsapi_userinfo
发布时间:2021-07-04, 04:13:27 分类:PHP | 编辑 off 网址 | 辅助
图集1/2
正文 3363字数 252,979阅读
准备:你自己需要有备案好的域名。在权限表-网页授权获取用户信息 里面可以设置该域名。
微信开发文档有四部:
1 第一步:用户同意授权,获取code
2 第二步:通过code换取网页授权access_token、openid
3 第三步:刷新access_token(如果需要)
4 第四步:拉取用户信息(需scope为 snsapi_userinfo)
snsapi_base: 到第二步就结束了,获取到openid,其他操作在这个基础上(比如记录该用户访问时间次数信息)
snsapi_userinfo: 获取openid和用户资料(昵称、头像、国、省、城市、性别、权限)
一、snsapi_base:
public function getBaseInfo(){
//1、获取code
$appid = 'wxe3ccbacbfdxxxxx';
$redirect_url = urlencode('http://wx.xx.com/index.php/Weixin/Index/getUserOpenId'); // 携带code跳转到该地址,也就是下面这个函数
$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);
}
/2、获取用户openid
public function getUserOpenId(){
$appid = 'wxe3ccbacbfdxxxxx';
$appsecret = '989b45dd6d2441ed01a5f5933aaaaaaa';
$code = $_GET['code']; //从上面函数getBaseInfo获取得到
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code ";
//获取openid
$res = $this->http_curl($url);
var_dump($res); //里面便是有openid数据
}
Run code
Cut to clipboard
打印出来了:
{ "access_token":"", "expires_in":7200, "refresh_token":"", "openid":"", "scope":"" }
Run code
Cut to clipboard
使用:
$res 里面便是有openid这个时候便是可以针对只需知道用户openid开发的小功能了:比如 用户记事本。
function getUserOpenId() 后面可以补充成:
$openid = $res['openid'];
$this->assign('openid',$openid);
$this->display('message');
Run code
Cut to clipboard
二、snsapi_userinfo
//1、获取code
public function getUserDetail(){
$appid = 'wxe3ccbacbfdxxxxxx';
$redirect_url = urlencode('http://wx.xx.com/index.php/Weixin/Index/getUserinfo'); //携带code跳转到该地址,即下面方法
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_url."&response_type=code&scope=snsapi_userinfo&state=333#wechat_redirect";
header('location:'.$url);
}
public function getUserinfo(){
$appid = 'wxe3ccbacbfdxxxxxx';
$appsecret = '989b45dd6d2441ed01a5f59336aaaaaa';
$code = $_GET['code']; //从上面函数 getUserDetail 获取得到
//2、获取openid和access_token
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code ";
$res = $this->http_curl($url);
$openid = $res['openid'];
$access_token = $res['access_token'];
//3、获取用户信息
$url2 = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
$res2 = $this->http_curl($url2);
var_dump($res2); //里面便含有用户信息
}
Run code
Cut to clipboard
注意:
1、redirect_uri 链接 xx.com 域名是要备案的,可以在开发-权限接口-网页授权获取用户信息 里面配置
2、注意获取code的函数(getBaseInfo)里面redirect_url链接是指向获取用户openid信息的函数(getUserOpenId),获取后的相应功能逻辑可以在这边实现,但菜单链接是先访问 获取code 函数(getBaseInfo)
3、整个逻辑的一个顺序是:
访问菜单上面链接 getBaseInfo函数 ,携带着 code 跳转到redirect_url 如: redirect_url (getUserOpenId) 或者redirect_url ( getUserinfo),里面获取到openid 或用户具体资料信息 后再利用这些信息做功能开发、逻辑跳转,如:
$this->assign('openid',$openid);$this->display('message');
Run code
Cut to clipboard
(支付宝)给作者钱财以资鼓励 (微信)→
有过 2 条评论 »
edge ghostery AppID 参数错误
此错误为 插件 Ghostery – 隐私广告拦截工具 引起,
信用网站或删除停用可解决
微信改了外链规则,如果你是点聊天界面的url进去分享出来的是 URL ,如果你是扫描二维码或别人分享进去分享就是 你设置的分享内容。
总结:必须是二维码或分享进去设置的分享内容才有效。
注意:
1、目前还可以从公众号菜单进入分享、访问网页后添加到收藏,从我的收藏进入分享正常。
2、其他入口应该分享都是链接了。