//获取token
public function getToken()
{
$appid = $this->config->item('appid');
$secret = $this->config->item('secret');
$url = "https://api.weixin.qq.com/cgi-bin/token";
$data = "grant_type=client_credential"."&appid=".$appid."&secret=".$secret;
$result = $this->curl_post($url,$data);
return $result;
}
//把token写入文件 加个时间戳
public function cacheToken()
{
//判断文件是否存在 存在取读取token 在判断是否到期
if (file_exists('token.json')){$ken = file_get_contents('token.json');
$arr_token = json_decode($ken,true);
if (is_array($arr_token) && $arr_token['expire_time'] > time()){
$token['access_token'] = $arr_token['access_token'];
}else{
$token = $this->getToken();
$token['expire_time'] = time() + 7000;
file_put_contents('token.json',json_encode($token));
}
}else{
$token = $this->getToken();
$token['expire_time'] = time() + 7000;
file_put_contents('token.json',json_encode($token));
}
return $token['access_token'];
}