微信access_token多业务统一调用解决方案
发布时间:2024-03-07

    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


     ($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();