关于php:微信公众号网页授权多域名解决方案

一个公众号的域名受权数量是无限的,如果一个公众号须要绑定多个域名时怎么解决呢?这时候就须要用到域名直达

实现微信受权域名直达实例

  • 微信受权回调域名(直达域名):www.test.com
  • 须要受权回调域名1:www.test1.com
  • 须要受权回调域名2:www.test2.com

    在直达域名www.test.com目录下新建index.php,test1.php,test2.php三个文件


  • 在index.php中编写发动受权代码

    if(isset($_GET['type']) && !empty($_GET['type'])){
      //发动受权
      $appId = "微信APPID";
      $redirectUrl =  $_SERVER['REQUEST_SCHEME'] . '://'.  $_SERVER['SERVER_NAME'] . '/' . $_GET['type'] . '.php';
      $codeUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appId."&redirect_uri=".urlencode($redirectUrl)."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
      header("location: ".$codeUrl);
      die;
    }else{
      echo "no";
    }
  • 在test1.php中编写获取受权信息并跳转业务地址(www.test1.com)

    if(isset($_GET['code']) && !empty($_GET['code'])){
      $code = $_GET['code'];
      $url = "http://www.test1.com/";
      header("location:".$url."?code=".$code);
    }else{
      echo 'no';
    }
  • 在test2.php中编写获取受权信息并跳转业务地址(www.test2.com)

    if(isset($_GET['code']) && !empty($_GET['code'])){
      $code = $_GET['code'];
      $url = "http://www.test2.com/";
      header("location:".$url."?code=".$code);
    }else{
      echo 'no';
    }

    依据如上形式就能够实现微信受权域名直达
    拜访 www.test1.com 站点时只需拜访 www.test.com?type=test1 即可
    拜访 www.test2.com 站点时只需拜访 www.test.com?type=test2 即可

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理