评论

微信access_token多业务统一调用解决方案

微信access_token多业务统一调用解决方案

1.access_token.json文件 用于存储access_token

{"access_token": "15_DUMf8LJODBpD7q8yTQpDFZ1kHEzs6o-pbSzPtMVIgahBJTuIWv4tsgB8Boq5uYAbM_YAb-0eg-A2DNUPOeih-XQHzH9uMfFZ7DU2MLuV9fT4oQxOPgCd47zlDGJhAFAGJM", "expires_time": 1543041788}


2.token.php

用于读取access_token和刷新access_token


<?php header("Content-Type: text/html;charset=utf-8");
$ip=$_SERVER['REMOTE_ADDR'];
//设置安全ip
if($ip=="115.181.78.94"){
$res = file_get_contents('access_token.json');
$result = json_decode($res, true);
$expires_time = $result["expires_time"];
$access_token = $result["access_token"];
if (time() > ($expires_time + 3600)){
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx43333b9fe558&secret=30518e99f338cb1f8ecac3469204dac5";
        $res = file_get_contents($url);
        $result = json_decode($res, true);
        $access_token = $result["access_token"];
        $expires_time = time();
        file_put_contents('access_token.json', '{"access_token": "'.$access_token.'", "expires_time": '.$expires_time.'}');
       
       echo '{"access_token":"'.$access_token.'","author":"wangsanqu"}';
    }else{
     
      echo '{"access_token":"'.$access_token.'","author":"wangsanqu"}';
     
    }
}
else{
     echo "403禁止调用api";exit();
}



3.在需要调用的地方进行调用


$url = "https://********/token.php";
        $res = file_get_contents($url);
        $result = json_decode($res, true);
        $access_token = $result["access_token"];
        $expires_time = time();


点赞 1
收藏
评论

3 个评论

  • 2019-08-02

    老铁怎么处理的各种过期情况

    2019-08-02
    赞同 1
    回复 1
    • 三去云
      三去云
      2019-08-02
      统一调用
      2019-08-02
      回复
  • _陈selphy
    _陈selphy
    2019-09-18

    有办法实现采集有效的微信appmsg_token 跟wap_sid2这种ck跟参数吗?重金酬谢qq196116707

    2019-09-18
    赞同
    回复
  • 哄哄
    哄哄
    2019-08-01

    是如何处理并发的?

    2019-08-01
    赞同
    回复
登录 后发表内容