• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php微信开发的时候 如何把access_token长期有效的保存,求高手解答

php 搞代码 3年前 (2022-01-25) 11次浏览 已收录 0个评论
文章目录[隐藏]
<code><?php  $appid="wx44a58ea3509d09fa"; $appsecret="d4624c36b6795d1d99dcf0547af5443d"; $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}"; //1、初始化CURL回话 $ch=curl_init(); //2、设置CURL选项 curl_setopt($ch, CURLOPT_URL, $url); //将请求的结果以文件流的形式返回 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //执行一个curl $outopt=curl_exec($ch); //4、关闭curl回话 curl_close($ch);$access_token_arr=json_decode($outopt,true);$access_token= $access_token_arr['access_token']; echo $access_token; ?></code>

回复内容:

<code><?php  $appid="wx44a58ea3509d09fa"; $appsecret="d4624c36b6795d1d99dcf0547af5443d"; $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}"; //1、初始化CURL回话 $ch=curl_init(); //2、设置CURL选项 curl_setopt($ch, CURLOPT_URL, $url); //将请求的结果以文件流的形式返回 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //执行一个curl $outopt=curl_exec($ch); //4、关闭curl回话 curl_close($ch);$access_token_arr=json_decode($outopt,true);$access_token= $access_token_arr['access_token']; echo $access_token; ?></code>

计划任务,定时刷新

你需要https://github.com/dodgepudding/wechat-php-sdk

https://github.com/thenbsp/wechat/blob/master/src/AccessToken.php

抖个机灵,用Redis,设置一个过期时间
之后做微信相关开发时打算这么干

access_token 你可以长期保存但是只有7200秒的有效期.

保存方式:

  1. 文件

  2. 数据库

    本文#来源gaodai.ma#com搞##代!^码7网
    搞代gaodaima码
  3. redis

我来说个土方法,数据库建一个表,字段有id,content,updated_at
初始化放一条数据进去:1,accesstoken,1440121354
然后需要获取access_token的时候,用现在的时间戳time()去数据库里查where id=1 and updated_at> time()-失效时间
要是获取过来有值,那就直接拿来用,要是没有说明时间太久了,重新获取下accesstoken,然后存到数据库里,更新时间戳


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php微信开发的时候 如何把access_token长期有效的保存,求高手解答

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址