码迷,mamicode.com
首页 > 微信 > 详细

微信公众号获取acess_token并存储(php)

时间:2017-04-24 23:04:24      阅读:525      评论:0      收藏:0      [点我收藏+]

标签:php   function   class   func   weixin   pps   logs   app   content   

 

<?php
    define("appid", "你的appid");
    define("appsecret", "你的appsecret");

    //获取access_token,每7000s重新获取
    function getAccessToken() {
        $tokenFile = "./access_token.txt";
        $data = json_decode(file_get_contents($tokenFile));

        if ($data->expire_time < time() || !$data->expire_time) {
            $appid = appid;
            $appsecret = appsecret;
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
            $res = json_decode(file_get_contents($url));
            $access_token = $res->access_token;
            if($access_token) {
                $data_new[‘expire_time‘] = time() + 7000;
                $data_new[‘access_token‘] = $access_token;
                file_put_contents($tokenFile, json_encode($data_new));
            }
        }else{
            $access_token = $data->access_token;
        }
        return $access_token;
    }

    // var_dump(getAccessToken());
?>

亲测可用,也许会遇到读写权限问题。

微信公众号获取acess_token并存储(php)

标签:php   function   class   func   weixin   pps   logs   app   content   

原文地址:http://www.cnblogs.com/zczhangcui/p/6759042.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!