您现在的位置是:首页 >技术教程 >php获取微信小程序全局唯一后台接口调用凭证网站首页技术教程

php获取微信小程序全局唯一后台接口调用凭证

salestina 2025-08-16 12:01:04
简介php获取微信小程序全局唯一后台接口调用凭证
public function getWxAccessToken(){
        $url = 'https://api.weixin.qq.com/cgi-bin/token';
        
        $appid = ''; //替换为小程序appid
        $secret = ''; //替换为小程序secret

        $url .= '?grant_type=client_credential&appid='.$appid.'&secret='.$secret;

        $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);
        $output = json_decode($output,true);
        if(isset($output['access_token']) && $output['access_token'] != ''){
            echo $output['access_token'].'
';
        }
        
}

生成的token有效期7200秒,既2小时。

因此请求该生成token的方法应使用定时任务2小时获取。将获取的token保存到服务器的指定文件中,其他接口需要使用时直接从文件里获取即可,避免了频繁调用,也能保证token不过期。

微信小程序官方文档地址:获取接口调用凭据 | 微信开放文档

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。