CURL缓存远程文件类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
 * 用于获取远程文件并缓存
 * CurlCache v1.0
 * Created by cr.
 * Date: 2015/12/03
 */
 
//namespace vendor\core;
 
 
class CurlCache
{
    /**
     * 请求地址
     * @var string
     */
    protected $url;
 
    /**
     * 缓存生存期
     * @var int
     */
    protected $expires;
 
    /**
     * 请求超时
     * @var int
     */
    protected $timeOut;
 
    /**
     * 是否开启缓存
     * @var int
     */
    protected $cacheEna;
 
    /**
     * 是否显示debug信息
     * @var bool
     */
    protected $debug;
 
    /**
     * 其他不用配置的内部属性
     */
    protected $path;
 
    protected $filename;
 
    protected $curlData;
 
    protected $curlHeader;
 
    protected $curlError;
 
    protected $cacheData;
 
    public $data;
    /**
     * CurlCache constructor.
     * @param int $expires
     * @param int $timeOut
     * @param bool|true $cacheEna
     * @param bool|false $debug
     */
    public function __construct($expires=3600, $timeOut=30, $cacheEna=true, $debug=false)
    {
        $this->expires=$expires;
        $this->timeOut=$timeOut;
        $this->cacheEna=$cacheEna;
        $this->debug=$debug;
    }
 
    /**
     * 检测并创建目录
     * @return bool
     */
    public function mkdirs(){
        if(is_dir($this->path)){
            return true;
        }else{
            return mkdir($this->path, 777, true);
        }
    }
 
    /**
     * 设置缓存文件相关信息
     * @param $url
     */
    public function setCacheFileInfo($url){
        $this->url=$url;
        $this->path=dirname(__FILE__).'/cache/';
        $this->filename=$this->url?md5($this->url):'null';
    }
 
    /**
     * 主方法,取得数据
     * @param $url
     * @return bool|string
     */
    public function get($url){
        $this->setCacheFileInfo($url);
        if($this->cacheEna) $this->data=unserialize($this->getCache($url));
        if(!$this->data) $this->data=$this->curlGet($url);
        if($this->data && $this->cacheEna) $this->setCache($url,serialize($this->data));
        return $this->data->contents;
    }
 
    /**
     * curl GET 请求数据
     * @param $url
     * @return string
     */
    public function curlGet($url){
        if($this->debug && $url=='') echo('请求URL为空,请检查');
        $return=new stdClass();
        $ch = curl_init ();
        curl_setopt($ch, CURLOPT_URL,$url );
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeOut);
        curl_setopt($ch, CURLOPT_HEADER, 0 );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        $this->curlData=curl_exec($ch);
        $this->curlHeader=curl_getinfo($ch);
        $this->curlError=curl_error($ch);
        $return->header=$this->curlHeader;
        $return->contents=$this->curlData;
        curl_close($ch);
        if((!$this->curlData || $this->curlHeader['http_code'] != '200') && $this->debug ) echo '请求失败,错误信息:'.$this->curlError;
        else return $return;
    }
 
    /**
     * 取得缓存数据
     * @param $url
     * @return bool|string
     */
    public function getCache($url){
        $this->setCacheFileInfo($url);
        if(file_exists($this->path.$this->filename)){
            if(time() - filemtime($this->path.$this->filename) < $this->expires) $this->cacheData=file_get_contents($this->path.$this->filename);
            else if($this->debug) echo '缓存文件已过期';
            if($this->cacheData) return $this->cacheData;else if($this->debug) echo '缓存文件内容为空';
        }else{
            //if($this->debug) echo '缓存文件不存在';
            return false;
        }
    }
 
    /**
     * 写入缓存数据
     * @param $url
     * @param $data
     */
    public function setCache($url,$data){
        $this->setCacheFileInfo($url);
        if(!$this->mkdirs() && $this->debug) echo('创建缓存目录失败:'.$this->path);
        if($data){
            if(!file_put_contents($this->path.$this->filename,$data) && $this->debug){
                echo '写入缓存失败 File:'.$this->path.$this->filename;
            }
        }else{
            if($this->debug) echo '数据为空,不写入缓存';
        }
    }
 
 
 
}

编程技巧