微信获取access_token授权的两种不同情况

1.网页授权:需要页面调用授权的sdk,首先需要获取到code参数

(A.网页版的获取code参考另一篇文章:https://blog.csdn.net/ettamei/article/details/148763361?spm=1011.2415.3001.5331

B.前端sdk提供:code只有5分钟的有效期

)

2A.第一种的获取access_token就是在获取code之后获取用户信息:

只做获取用户信息,如果有之后需要js-sdk的操作这个access_token都是无效的,之后js-sdk使用的access_token也无法使用到获取用户信息中

php 复制代码
function usrAccessToken(string $code){
        $appid = $this->wechatConfig['appid'];
        $secret = $this->wechatConfig['app_secret'];
        //获取请求地址
        //获取code后,请求以下链接获取access_token
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$secret}&code={$code}&grant_type=authorization_code";
        $res_data = $this->curl_get($url);
        Tools::log_to_write_txt(['通过code换取网页授权access_token$res_data'=>$res_data]);
        $data = json_decode($res_data['data'], true);
        if(isset($data['errcode']) && ($data['errcode'] == 40163 || $data['errcode'] == 40029)){
            throw new \Exception('code已失效,请重新授权');
        }
        cache($data['openid'].'wx_access_token', $data["access_token"],7000);//保存
        return $data;
    }

获取到的信息例子:

php 复制代码
{  
"access_token":"94_S4F2QWpTjFMJhQ",//访问接口的令牌,用于调用受保护的资源(如用户信息)。 
"expires_in":7200,//有效时间,单位为秒。 
"refresh_token":"94_2wXpmw7nRpZ9Vi-w27O",//作用:刷新 access_token 的令牌。当access_token 过期后,可以用 refresh_token 获取新的access_token。 
"openid":"oC2wtxPWLc",//用户的唯一标识 ID,用于识别用户身份。 
"scope":"snsapi_userinfo"//表示当前授权的权限范围。snsapi_userinfo 表示可以调用接口获取用户详细信息(如昵称、头像等)。  
}

2B.获取js-sdk中使用的access_token:获取这个access_token是需要在微信公众号中设置ip白名单,如果使用的服务器ip地址不稳定需要设置静态ip或者使用云服务器

php 复制代码
 function getAccessTokenNow(){
        try{
            //实时获取
            $appid = $this->wechatConfig['appid'];
            $appsecret = $this->wechatConfig['app_secret'];
            //获取请求地址
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($ch);
            curl_close($ch);
            $jsoninfo = json_decode($output, true);
            Tools::log_to_write_txt(['网页授权access_token$res_data'=>$jsoninfo]);
            cache('wx_access_token', $jsoninfo["access_token"],7000);//保存
            return $jsoninfo["access_token"];
        }catch (\Exception $e){
            Tools::log_to_write_txt(['error_title'=>'[错误]获取票据出错:'.$e->getMessage(),'error_info'=>$e->getTraceAsString()]);
            return false;
        }
    }
相关推荐
wangxiaoxuan21734 分钟前
大模型怎样调用机器人能力:目标落地、工具调用与执行反馈
数据库·机器人·php
Risehuxyc3 小时前
PHP中如何将字符串转成字节数组,再异或成一个字节
开发语言·php
Risehuxyc6 小时前
为什么php结束符?>省略不写
开发语言·php
FungLeo8 小时前
FengCMS · PHP+MySQL 升级实录· 依赖生命周期与来源核验:jQuery 4.0 迁移 + SHA-256 核验脚本
mysql·php·jquery·依赖管理·fengcms
kitsch0x978 小时前
MITRE Engage 中的网络欺骗
安全·web安全·php
FungLeo8 小时前
FengCMS · PHP+MySQL 升级实录 · CI 落地:PHP 8.4/8.5 矩阵、隔离凭证、诊断门槛失败
mysql·ci/cd·php·持续集成·fengcms
FungLeo1 天前
FengCMS · PHP+MySQL 升级实录· 回归测试从 69 项到 478 项:HTTP/数据库回归套件怎么搭
数据库·mysql·php·回归测试·fengcms
FungLeo1 天前
FengCMS · PHP+MySQL 升级实录· PHP Session 锁:什么时候释放能救活并发
android·mysql·php·session·fengcms
bufengwangluo2 天前
企业上网行为管理:代理网关部署位置怎么选,策略怎么落
开发语言·php
FungLeo2 天前
FengCMS · PHP+MySQL 升级实录 · 输出转义的度:面包屑双重编码、默认转义与 cms_html()信任声明
mysql·html·php·fengcms·输出转义