PHP解决微信公众号网页授权域名只能填写一个的问题

做微信开发的,必不可少的都会涉及到微信网页授权,来获取用户信息的功能。想要实现公众号通过微信网页授权机制,来获取用户基本信息,必须先到公众平台官网中的“开发 - 接口权限 - 网页服务 - 网页帐号 - 网页授权获取用户基本信息”的配置选项中,填写授权回调域名。但是呢,在微信公众号后台,业务域名和JS安全域名都可以填写3个,唯独 网页授权域名只能填写一个

现在我只有一个公众号,由于业务需要,我在不同的场景下不同的域名都使用这个公众号,而且都需要获取用户信息。但是网页授权域名只能填写一个,并且只有回调地址的域名与该设置完全相同,才能成功发起微信授权,否则就会提示rediret_uri参数错误。

那么,就来讲讲怎么突破这个限制,实现多个域名使用一个公众号同时发起网页授权获取到用户基本信息。

它只能填写一个授权域名,那我们就从这一个域名下手,既然这个授权域名可以顺利拿到网页请求的数据,那我们 其他的域名可以先去请求授权域名,然后让授权域名再去微信服务器请求数据,这样就完美解决了。这个授权域名,起到了类似于中介和代理人的作用。实现方法如下:
1.在公众号后台设置一个授权回调页面域名,比如叫:wx.agency.com,我们可以称其为代理域名。
2.在wx.agency.com指向的网站根目录下部署一个index.php文件。

工作原理如下:
(1)当你的其他域名需要发起微信授权时,将授权请求先发到代理域名wx.agency.com,然后wx.agency.com会把这个请求转发到微信服务器; 
(2)当用户同意授权后,wx.agency.com会收到微信的授权回调,并把回调结果(code、state参数)原封不动地再返回给最开始发起授权的域名。

代码实现:
之前有写过 微信网页授权获取用户基本信息,那是常规方法授权获取用户信息,代码如下:

public function _userInfoAuth($redirect_url){
	
	//1.准备scope为snsapi_userInfo网页授权页面
	$redirecturl = urlencode($redirect_url);
	$snsapi_userInfo_url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->_appid.'&redirect_uri='.$redirecturl.'&response_type=code&scope=snsapi_userinfo&state=YQJ#wechat_redirect';
	
	//2.用户手动同意授权,同意之后,获取code
	//页面跳转至redirect_uri/?code=CODE&state=STATE
	$code = $_GET['code'];
	if( !isset($code) ){
		header('Location:'.$snsapi_userInfo_url);
	}
	
	//3.通过code换取网页授权access_token
	$curl = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->_appid.'&secret='.$this->_appsecret.'&code='.$code.'&grant_type=authorization_code';
	$content = $this->_request($curl);
	$result = json_decode($content);
	
	//4.通过access_token和openid拉取用户信息
	$webAccess_token = $result->access_token;
	$openid = $result->openid;
	$userInfourl = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$webAccess_token.'&openid='.$openid.'&lang=zh_CN ';
	
	$recontent = $this->_request($userInfourl);
	$userInfo = json_decode($recontent,true);
	return $userInfo;
}
这次获取用户信息的代码如下:

//第三方代理服务器授权登录
public function agencyInfoAuth($redirect_url){
	
	//1.准备scope为snsapi_userInfo网页授权页面
	$redirecturl = urlencode($redirect_url);
	$snsapi_userInfo_url = 'http://wx.agency.com/index.php?appid='.$this->_appid.'&redirect_uri='.$redirecturl.'&response_type=code&scope=snsapi_userinfo&state=YQJ#wechat_redirect';
	
	//2.用户手动同意授权,同意之后,获取code
	//页面跳转至redirect_uri/?code=CODE&state=STATE
	$code = $_GET['code'];
	if( !isset($code) ){
		header('Location:'.$snsapi_userInfo_url);
	}
	
	//3.通过code换取网页授权access_token
	$curl = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->_appid.'&secret='.$this->_appsecret.'&code='.$code.'&grant_type=authorization_code';
	$content = $this->_request($curl);
	$result = json_decode($content);
	
	//4.通过access_token和openid拉取用户信息
	$webAccess_token = $result->access_token;
	$openid = $result->openid;
	$userInfourl = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$webAccess_token.'&openid='.$openid.'&lang=zh_CN ';
	
	$recontent = $this->_request($userInfourl);
	$userInfo = json_decode($recontent,true);
	return $userInfo;
}
可以卡看到两种方法唯一的区别就在第一步 准备scope为snsapi_userInfo网页授权页面链接不同,一个是去open.weixin.qq.com请求数据,另一个是去wx.agency.com请求数据。

这个方案我亲测有效,虽然增加了一次重定向操作,但实际上不会对用户体验产生多大的影响,真实解决了公众号网页授权域名只能填写一个的问题。

实现思路来自:http://www.cnblogs.com/lyzg/

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值