本文作者:DurkBlue

PHP如何静默授权获取微信用户的 openid 和基本信息

DurkBlue 2022-07-23 1987 抢沙发
PHP如何静默授权获取微信用户的 openid 和基本信息摘要: 准备首先你要有一个公众号,类型为服务号,还有一个外网可访问的域名(如果要映射公网服务器,需备案)。获取openid需要的公众号的 appid 和 secret(登录微信公众平台,在...

准备

首先你要有一个公众号,类型为服务号,还有一个外网可访问的域名(如果要映射公网服务器,需备案)。获取openid需要的公众号的 appid 和 secret(登录微信公众平台,在【开发】——>【基本配置】中的开发者ID(AppID)和 开发者密码(AppSecret)就是)。其次是设置网页授权域名(登陆微信公众平台,在【设置】——>【公众号设置】——>【功能设置】——>网页授权域名中按步骤操作并设置就好),这个域名就是你获取openid的web项目发布的域名,这里注意服务器请一定跑在80端口。


参考代码

  function getOpenID(){
       header("Content-Type: text/html;charset=utf-8");
       $appid = 'appid';
$appsecret = 'secret';
$code = $_GET['code'];
       //第一步:取得openid
       $oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
       $oauth2 = $this->http_curl($oauth2Url);
       //accestoken
       $access_token = $oauth2["access_token"];
       //openid
       $openid = $oauth2['openid'];
       //第二步:根据全局access_token和openid查询用户信息
       $get_user_info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
       $userinfo = $this->http_curl($get_user_info_url);
       return $openid;
  }
  public function getCode(){
      //基本配置
      $appid='appid';
      $redirect_uri=urlencode("https://www.xxxx.com/Home/Test/getOpenID");
      $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
      header("location:".$url);
  }
  function http_curl($url){
      //用curl传参
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
      //关闭ssl验证
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
      curl_setopt($ch,CURLOPT_HEADER, 0);
      $output = curl_exec($ch);
      curl_close($ch);
      return json_decode($output, true);
  }

这个就是请求完微信后 微信重定向后访问的格式

https://www.xxxxxx.com/Home/Test/getOpenID?code=071jdyFa1c9Z1A0eaaGa1Y4K9W3jdyFf&state=123


备注

网页授权域名设置如果与提交的回调地址的域名不对应,微信是无法回调到的。


单纯的获取openid使用的授权方式是静态授权,不需要经过用户许可的(用户看不到授权的过程),而想要获取用户的头像昵称等信息是另一种授权(用户端会弹出授权窗口)。


授权回调域名配置规范为全域名并且不带http,比如需要网页授权的域名为:www.qq.com,

配置以后此域名下面的页面http://www.qq.com/music.html 、 http://www.qq.com/login.html 都可以进行鉴权。

但http://pay.qq.com 、 http://music.qq.com 、 http://qq.com无法进行鉴权。

这里我们填写我自己论坛的一个域名为 www.weixintuo.com。


可能是配置错误

错误配置:

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf0e81c3bee622d60&redirect_uri=http%3A%2F%2Fnba.bluewebgame.com%2Foauth_response.php&scope=snsapi_userinfo&response_type=code&state=STATE#wechat_redirect

正确的配置:

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf0e81c3bee622d60&redirect_uri=http%3A%2F%2Fnba.bluewebgame.com%2Foauth_response.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect

原因就是scope的位置不一样!!!

当 scope 为 snsapi_base的时候没有影响。但是scope为snsapi_userinfo 就会提示 scope参数错误或没有scope权限


全局返回码说明如下:

https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Global_Return_Code.html


关于网页授权回调域名的说明

此篇文章由DurkBlue发布,转载请注明来处
文章版权及转载声明

作者:DurkBlue本文地址:https://hepuhua.cn/post/864.html发布于 2022-07-23
文章转载或复制请以超链接形式并注明出处DurkBlue博客

赞(0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

阅读
分享

发表评论取消回复

快捷回复:
AddoilApplauseBadlaughBombCoffeeFabulousFacepalmFecesFrownHeyhaInsidiousKeepFightingNoProbPigHeadShockedSinistersmileSlapSocialSweatTolaughWatermelonWittyWowYeahYellowdog

评论列表 (暂无评论,1987人围观)参与讨论

还没有评论,来说两句吧...