EasyWechat.class.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * 微信公众平台PHP-SDK, 简单缓存实例
  4. * @author binsee@163.com
  5. * @link https://github.com/binsee/wechat-php-sdk
  6. * @version 0.1
  7. * usage:
  8. * $options = array(
  9. * 'token'=>'tokenaccesskey', //填写你设定的key
  10. * 'encodingaeskey'=>'encodingaeskey', //填写加密用的EncodingAESKey
  11. * 'appid'=>'wxdk1234567890', //填写高级调用功能的app id
  12. * 'cachedir'=>'./cache/', //填写缓存目录,默认为当前运行目录的子目录cache下
  13. * 'logfile'=>'run.log' //填写日志输出文件,可选项。如果没有提供logcallback回调,且设置了输出文件则将日志输出至此文件,如果省略则不输出
  14. * );
  15. * $weObj = new EasyWechat($options);
  16. * $weObj->valid();
  17. * ...
  18. *
  19. */
  20. class EasyWechat extends Wechat
  21. {
  22. private $cachedir = '';
  23. private $logfile = '';
  24. public function __construct($options)
  25. {
  26. $this->cachedir = isset($options['cachedir']) ? dirname($options['cachedir'].'/.cache') . '/' : 'cache/';
  27. $this->logfile = isset($options['logfile']) ? $options['logfile'] : '';
  28. if ($this->cachedir) $this->checkDir($this->cachedir);
  29. parent::__construct($options);
  30. }
  31. /**
  32. * log overwrite
  33. * @param string|array $log
  34. */
  35. protected function log($log){
  36. if (is_array($log)) $log = print_r($log,true);
  37. if ($this->debug) {
  38. if (function_exists($this->logcallback)) {
  39. return call_user_func($this->logcallback,$log);
  40. }elseif ($this->logfile) {
  41. return file_put_contents($this->logfile, $log."\n", FILE_APPEND) > 0 ? true : false;
  42. }
  43. }
  44. return false;
  45. }
  46. /**
  47. * 重载设置缓存
  48. * @param string $cachename
  49. * @param mixed $value
  50. * @param int $expired 缓存秒数,如果为0则为长期缓存
  51. * @return boolean
  52. */
  53. protected function setCache($cachename,$value,$expired=0){
  54. $file = $this->cachedir . $cachename . '.cache';
  55. $data = array(
  56. 'value' => $value,
  57. 'expired' => $expired ? time() + $expired : 0
  58. );
  59. return file_put_contents( $file, serialize($data) ) ? true : false;
  60. }
  61. /**
  62. * 重载获取缓存
  63. * @param string $cachename
  64. * @return mixed
  65. */
  66. protected function getCache($cachename){
  67. $file = $this->cachedir . $cachename . '.cache';
  68. if (!is_file($file)) {
  69. return false;
  70. }
  71. $data = unserialize(file_get_contents( $file ));
  72. if (!is_array($data) || !isset($data['value']) || (!empty($data['value']) && $data['expired']<time())) {
  73. @unlink($file);
  74. return false;
  75. }
  76. return $data['value'];
  77. }
  78. /**
  79. * 重载清除缓存
  80. * @param string $cachename
  81. * @return boolean
  82. */
  83. protected function removeCache($cachename){
  84. $file = $this->cachedir . $cachename . '.cache';
  85. if ( is_file($file) ) {
  86. @unlink($file);
  87. }
  88. return true;
  89. }
  90. private function checkDir($dir, $mode=0777) {
  91. if (!$dir) return false;
  92. if(!is_dir($dir)) {
  93. if (!file_exists($dir) && @mkdir($dir, $mode, true))
  94. return true;
  95. return false;
  96. }
  97. return true;
  98. }
  99. }