21,886
社区成员
发帖
与我相关
我的任务
分享
<?php
include_once __DIR__ . '/../lib/access_token.php';
/**
* 文件缓存机制的凭据中控程序.支持并发,支持多appid.
*/
class FileCacheTokenControl {
protected $m_appid = null;
protected $m_appsecret = null;
protected $m_path = null;
private function get_php_file($filename) {
return file_exists($filename) ? trim(substr(file_get_contents($filename), 15)) : "";
}
private function set_php_file($filename, $content) {
if(!file_exists(dirname($filename)))
mkdir(dirname($filename), 0777, true);
file_put_contents($filename, "<?php exit();?>" . $content);
/*
* $fp = fopen($filename, "w");
* write($fp, "<?php exit();?>" . $content);
* fclose($fp);
*/
}
/**
* @param string $appid 公众号应用ID
* @param string $appsecret 公众号应用密钥
*/
public function __construct($appid, $appsecret) {
$this->m_appid = $appid;
$this->m_appsecret = $appsecret;
$this->m_path = __DIR__ . "/cache/" . md5($this->m_appid); //支持多appid
}
/**
* 获取access_token票据.使用缓存机制
*/
public function get_access_token() {
$result = json_decode($this->get_php_file($this->m_path . "/access_token.php"));
if(!$result || $result->expire_time < time() || $result->appid != $this->m_appid || $result->appsecret != $this->m_appsecret) {
$result = json_decode(access_token($this->m_appid, $this->m_appsecret));
if(!isset($result) || !isset($result->access_token))
return "";
$data = new \stdClass();
$data->expire_time = time() + 7000;
$data->access_token = $result->access_token;
$data->appid = $this->m_appid;
$data->appsecret = $this->m_appsecret;
$this->set_php_file($this->m_path . "/access_token.php", json_encode($data, JSON_UNESCAPED_UNICODE));
}
return $result->access_token;
}
/**
* 获取jsapi_ticket票据.使用缓存机制
*/
public function get_jsapi_ticket() {
$result = json_decode($this->get_php_file($this->m_path . "/jsapi_ticket.php"));
if(!$result || $result->expire_time < time() || $result->appid != $this->m_appid || $result->appsecret != $this->m_appsecret) {
$result = json_decode(jsapi_ticket($this->get_access_token()));
if(!isset($result) || !isset($result->ticket))
return "";
$result->jsapi_ticket = $result->ticket; //为了名移风格统一,稍微改个名.^_^
$data = new \stdClass();
$data->expire_time = time() + 7000;
$data->jsapi_ticket = $result->jsapi_ticket;
$data->appid = $this->m_appid;
$data->appsecret = $this->m_appsecret;
$this->set_php_file($this->m_path . "/jsapi_ticket.php", json_encode($data, JSON_UNESCAPED_UNICODE));
}
return $result->jsapi_ticket;
}
/**
* 获取card_ticket票据.使用缓存机制
*/
public function get_card_ticket() {
$result = json_decode($this->get_php_file($this->m_path . "/card_ticket.php"));
if(!$result || $result->expire_time < time() || $result->appid != $this->m_appid || $result->appsecret != $this->m_appsecret) {
$result = json_decode(card_ticket($this->get_access_token()));
if(!isset($result) || !isset($result->ticket))
return "";
$result->card_ticket = $result->ticket; //为了名移风格统一,稍微改个名.^_^
$data = new \stdClass();
$data->expire_time = time() + 7000;
$data->card_ticket = $result->card_ticket;
$data->appid = $this->m_appid;
$data->appsecret = $this->m_appsecret;
$this->set_php_file($this->m_path . "/card_ticket.php", json_encode($data, JSON_UNESCAPED_UNICODE));
}
return $result->card_ticket;
}
}
?>
<?php
include_once __DIR__ . '/utils.php';
/**
* 获取通用access_token凭据
* @param string $appid 公众号应用ID
* @param string $appsecret 公众号应用密钥
* @return json字符串.成功时如{"access_token": "ACCESS_TOKEN", "expires_in":7200}; 失败时如{"errcode": 40013, "errmsg": "invalid appid"}
*/
function access_token($appid, $appsecret) {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
$result = curl_http_get($url, null, 30);
return $result;
}
/**
* 获取jsapi_ticket凭据
* @param string $access_token 通用access_token凭据
* @return json字符串.成功时如: {"errcode": 0, "errmsg": "ok", "ticket": "TICKET", "expires_in":7200}; 失败时如: {"errcode": 40013, "errmsg": "invalid appid"}
*/
function jsapi_ticket($access_token) {
//如果是企业号用以下URL获取jsapi_ticket
//$url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$access_token";
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$access_token";
$result = curl_http_get($url, null, 30);
return $result;
}
/**
* 获取card_ticket凭据
* @param string $access_token 通用access_token凭据
* @return json字符串.成功时如: {"errcode": 0, "errmsg": "ok", "ticket": "TICKET", "expires_in":7200}; 失败时如: {"errcode": 40013, "errmsg": "invalid appid"}
*/
function card_ticket($access_token) {
//如果是企业号用以下URL获取jsapi_ticket
//$url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$access_token";
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=wx_card&access_token=$access_token";
$result = curl_http_get($url, null, 30);
return $result;
}
?>
/**
* @tutorial 支持http,https
* @param string $url 要访问的站点
* @param array,object $data get只能是key/value的参数集合
* @param int $timeout 超时时间,单位秒
* @return 站点数据或失败信息
*/
function curl_http_get($url, $data = null, $timeout = 30) {
while(strlen($url) > 0 && (strrpos($url, "&") == strlen($url) - 1 || strrpos($url, "?") == strlen($url) - 1))
$url = substr($url, 0, strlen($url) - 1);
if(isset($data) && (is_array($data) || is_object($data)))
$url .= (strpos($url, "?") === false ? "?" : "&") . http_build_query($data);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //将curl会话获取的信息以字符串返回,而不是直接输出
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //禁止curl验证对等证书(peer's certificate)
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); //不检查服务器SSL证书中是否存在一个公用名(common name)
//curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); //允许curl验证对等证书(peer's certificate)
//curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); //严格检查服务器SSL证书中是否存在一个公用名(common name)
curl_setopt($curl, CURLOPT_POST, false); //发送 GET请求.类型为:application/x-www-form-urlencoded
//curl_setopt($curl, CURLOPT_POSTFIELDS, $data); //发送数据.可以是字符串或键值对数组.如果是数组,Content-Type头会被设置成multipart/form-data.使用@前缀语法时,必须是数组.
$result = curl_exec($curl);
if($errno = curl_errno($curl))
$result = "$errno:" . curl_error($curl);
curl_close($curl);
return $result;
}
/**
* @tutorial 支持http,https
* @param string $url 要访问的站点
* @param mixed $data post可以是任意类型的数据,例如string,array,object,json等
* @param int $timeout 超时时间,单位秒
* @return 站点数据或失败信息
*/
function curl_http_post($url, $data, $timeout = 30) {
while(strlen($url) > 0 && (strrpos($url, "&") == strlen($url) - 1 || strrpos($url, "?") == strlen($url) - 1))
$url = substr($url, 0, strlen($url) - 1);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //将curl会话获取的信息以字符串返回,而不是直接输出
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //禁止curl验证对等证书(peer's certificate)
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); //不检查服务器SSL证书中是否存在一个公用名(common name)
//curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); //允许curl验证对等证书(peer's certificate)
//curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); //严格检查服务器SSL证书中是否存在一个公用名(common name)
curl_setopt($curl, CURLOPT_POST, true); //发送 POST请求.类型为:application/x-www-form-urlencoded
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); //发送数据.可以是字符串或键值对数组.如果是数组,Content-Type头会被设置成multipart/form-data.使用@前缀语法时,必须是数组.
$result = curl_exec($curl);
if($errno = curl_errno($curl))
$result = "$errno:" . curl_error($curl);
curl_close($curl);
return $result;
}
public function __construct() {
$this -> init();
}
public function init() {
if(!get_php_file('access_token')){
$this -> reloadCache();//重新加载缓存
}
}
public function reloadCache() {
$access_token = 'access_token';
set_php_file('access_token', $access_token);
}
public function set_php_file() {
}
public function get_php_file() {
}