您的位置:首页 > 移动开发 > 微信开发

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

2017-07-27 00:08 691 查看
做微信开发的,必不可少的都会涉及到微信网页授权,来获取用户信息的功能。想要实现公众号通过微信网页授权机制,来获取用户基本信息,必须先到公众平台官网中的“开发 - 接口权限 - 网页服务 - 网页帐号 - 网页授权获取用户基本信息”的配置选项中,填写授权回调域名。但是呢,在微信公众号后台,业务域名和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

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;  
}  

这次获取用户信息的代码如下:

[php]
view plain
copy

//第三方代理服务器授权登录  
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请求数据。
这个方案我亲测有效,虽然增加了一次重定向操作,但实际上不会对用户体验产生多大的影响,真实解决了公众号网页授权域名只能填写一个的问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐