Ai
2 Star 0 Fork 0

ijijni/wechat-php-sdk11

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
EasyWechat.class.php 3.31 KB
一键复制 编辑 原始数据 按行查看 历史
<?php
/**
* 微信公众平台PHP-SDK, 简单缓存实例
* @author binsee@163.com
* @link https://github.com/binsee/wechat-php-sdk
* @version 0.1
* usage:
* $options = array(
* 'token'=>'tokenaccesskey', //填写你设定的key
* 'encodingaeskey'=>'encodingaeskey', //填写加密用的EncodingAESKey
* 'appid'=>'wxdk1234567890', //填写高级调用功能的app id
* 'cachedir'=>'./cache/', //填写缓存目录,默认为当前运行目录的子目录cache下
* 'logfile'=>'run.log' //填写日志输出文件,可选项。如果没有提供logcallback回调,且设置了输出文件则将日志输出至此文件,如果省略则不输出
* );
* $weObj = new EasyWechat($options);
* $weObj->valid();
* ...
*
*/
class EasyWechat extends Wechat
{
private $cachedir = '';
private $logfile = '';
public function __construct($options)
{
$this->cachedir = isset($options['cachedir']) ? dirname($options['cachedir'].'/.cache') . '/' : 'cache/';
$this->logfile = isset($options['logfile']) ? $options['logfile'] : '';
if ($this->cachedir) $this->checkDir($this->cachedir);
parent::__construct($options);
}
/**
* log overwrite
* @param string|array $log
*/
protected function log($log){
if (is_array($log)) $log = print_r($log,true);
if ($this->debug) {
if (function_exists($this->logcallback)) {
return call_user_func($this->logcallback,$log);
}elseif ($this->logfile) {
return file_put_contents($this->logfile, $log."\n", FILE_APPEND) > 0 ? true : false;
}
}
return false;
}
/**
* 重载设置缓存
* @param string $cachename
* @param mixed $value
* @param int $expired 缓存秒数,如果为0则为长期缓存
* @return boolean
*/
protected function setCache($cachename,$value,$expired=0){
$file = $this->cachedir . $cachename . '.cache';
$data = array(
'value' => $value,
'expired' => $expired ? time() + $expired : 0
);
return file_put_contents( $file, serialize($data) ) ? true : false;
}
/**
* 重载获取缓存
* @param string $cachename
* @return mixed
*/
protected function getCache($cachename){
$file = $this->cachedir . $cachename . '.cache';
if (!is_file($file)) {
return false;
}
$data = unserialize(file_get_contents( $file ));
if (!is_array($data) || !isset($data['value']) || (!empty($data['value']) && $data['expired']<time())) {
@unlink($file);
return false;
}
return $data['value'];
}
/**
* 重载清除缓存
* @param string $cachename
* @return boolean
*/
protected function removeCache($cachename){
$file = $this->cachedir . $cachename . '.cache';
if ( is_file($file) ) {
@unlink($file);
}
return true;
}
private function checkDir($dir, $mode=0777) {
if (!$dir) return false;
if(!is_dir($dir)) {
if (!file_exists($dir) && @mkdir($dir, $mode, true))
return true;
return false;
}
return true;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/ijijni/wechat-php-sdk.git
git@gitee.com:ijijni/wechat-php-sdk.git
ijijni
wechat-php-sdk
wechat-php-sdk11
master

搜索帮助