-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsimpleCachedCurl.inc.php
85 lines (77 loc) · 2.81 KB
/
simpleCachedCurl.inc.php
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
<?php
/*
simpleCachedCurl V1.1
Dirk Ginader
http://ginader.com
Copyright (c) 2013 Dirk Ginader
Dual licensed under the MIT and GPL licenses:
http://www.opensource.org/licenses/mit-license.php
http://www.gnu.org/licenses/gpl.html
code: http://github.com/ginader
easy to use cURL wrapper with added file cache
usage: created a folder named "cache" in the same folder as this file and chmod it 777
call this function with 3 parameters:
$url (string) the URL of the data that you would like to load
$expires (integer) the amound of seconds the cache should stay valid
$curlopt (array, optional) set multiple options for a cURL transfer http://php.net/manual/en/function.curl-setopt-array.php
$debug (boolean, optional) write debug information for troubleshooting
returns either the raw cURL data or false if request fails and no cache is available
*/
function simpleCachedCurl($url,$expires,$curlopt = array(),$debug=false){
if($debug){
echo "simpleCachedCurl debug:<br>";
}
$hash = md5($url);
$filename = dirname(__FILE__).'/cache/' . $hash . '.cache';
$changed = file_exists($filename) ? filemtime($filename) : 0;
$now = time();
$diff = $now - $changed;
if ( !$changed || ($diff > $expires) ) {
if($debug){
echo "no cache or expired --> make new request<br>";
}
$ch = curl_init();
$default_curlopt = array(
CURLOPT_TIMEOUT => 2,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1
);
$curlopt = array(CURLOPT_URL => $url) + $curlopt + $default_curlopt;
curl_setopt_array($ch, $curlopt);
$rawData = curl_exec($ch);
curl_close($ch);
if(!$rawData){
if($debug){
echo "cURL request failed<br>";
}
if($changed){
if($debug){
echo "at least we have an expired cache --> better than nothing --> read it<br>";
}
$cache = unserialize(file_get_contents($filename));
return $cache;
}else{
if($debug){
echo "request failed and we have no cache at all --> FAIL<br>";
}
return false;
}
}
if($debug){
echo "we got a return --> save it to cache<br>";
}
$cache = fopen($filename, 'wb');
$write = fwrite($cache, serialize($rawData));
if($debug && !$write){
echo "writing to $filename failed. Make the folder '".dirname(__FILE__).'/cache/'."' is writeable (chmod 777)<br>";
}
fclose($cache);
return $rawData;
}
if($debug){
echo "yay we hit the cache --> read it<br>";
}
$cache = unserialize(file_get_contents($filename));
return $cache;
}
?>