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参数)原封不动地再返回给最开始发起授权的域名。

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

[php]  view plain  copy
  1. public function _userInfoAuth($redirect_url){  
  2.       
  3.     //1.准备scope为snsapi_userInfo网页授权页面  
  4.     $redirecturl = urlencode($redirect_url);  
  5.     $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';  
  6.       
  7.     //2.用户手动同意授权,同意之后,获取code  
  8.     //页面跳转至redirect_uri/?code=CODE&state=STATE  
  9.     $code = $_GET['code'];  
  10.     if( !isset($code) ){  
  11.         header('Location:'.$snsapi_userInfo_url);  
  12.     }  
  13.       
  14.     //3.通过code换取网页授权access_token  
  15.     $curl = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->_appid.'&secret='.$this->_appsecret.'&code='.$code.'&grant_type=authorization_code';  
  16.     $content = $this->_request($curl);  
  17.     $result = json_decode($content);  
  18.       
  19.     //4.通过access_token和openid拉取用户信息  
  20.     $webAccess_token = $result->access_token;  
  21.     $openid = $result->openid;  
  22.     $userInfourl = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$webAccess_token.'&openid='.$openid.'&lang=zh_CN ';  
  23.       
  24.     $recontent = $this->_request($userInfourl);  
  25.     $userInfo = json_decode($recontent,true);  
  26.     return $userInfo;  
  27. }  
这次获取用户信息的代码如下:

[php]  view plain  copy
  1. //第三方代理服务器授权登录  
  2. public function agencyInfoAuth($redirect_url){  
  3.       
  4.     //1.准备scope为snsapi_userInfo网页授权页面  
  5.     $redirecturl = urlencode($redirect_url);  
  6.     $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';  
  7.       
  8.     //2.用户手动同意授权,同意之后,获取code  
  9.     //页面跳转至redirect_uri/?code=CODE&state=STATE  
  10.     $code = $_GET['code'];  
  11.     if( !isset($code) ){  
  12.         header('Location:'.$snsapi_userInfo_url);  
  13.     }  
  14.       
  15.     //3.通过code换取网页授权access_token  
  16.     $curl = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->_appid.'&secret='.$this->_appsecret.'&code='.$code.'&grant_type=authorization_code';  
  17.     $content = $this->_request($curl);  
  18.     $result = json_decode($content);  
  19.       
  20.     //4.通过access_token和openid拉取用户信息  
  21.     $webAccess_token = $result->access_token;  
  22.     $openid = $result->openid;  
  23.     $userInfourl = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$webAccess_token.'&openid='.$openid.'&lang=zh_CN ';  
  24.       
  25.     $recontent = $this->_request($userInfourl);  
  26.     $userInfo = json_decode($recontent,true);  
  27.     return $userInfo;  
  28. }  
可以卡看到两种方法唯一的区别就在第一步 准备scope为snsapi_userInfo网页授权页面链接 不同,一个是去open.weixin.qq.com请求数据,另一个是去wx.agency.com请求数据。

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

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

猜你喜欢

转载自blog.csdn.net/qq_29058883/article/details/80781923