微信Access_Token全局存储

浏览:3495 发布日期:2016/11/09 分类:技术分享 关键字: 微信 Access_Token 缓存

namespace Org\Util;
class Access_Token {

function accessToken() {
$tokenFile = "Public/confing/wexin.txt";//缓存文件名;
$myfile = fopen($tokenFile, "r");
$data = json_decode(file_get_contents($tokenFile));
$access_token = "";
if ($data->expire_time <= time() or !$data->expire_time) {
$appid = C("APPID");
$appsecret =C("APP_SECRET");
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
$res = $this::getJson($url);
$access_token = $res['access_token'];
if($access_token) {
$data['expire_time'] = time() + 7200;
$data['access_token'] = $access_token;
$fp = fopen($tokenFile, "w");
fwrite($fp, json_encode($data));
fclose($fp);

}
} else {
$access_token = $data->access_token;
}
return $access_token;
}

function getJson($url,$obj){$ch=curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);$output = curl_exec($ch);curl_close($ch);return json_decode($output, true);}



}
最佳答案
评论( 相关
后面还有条评论,点击查看>>