September 30, 2006
Caching for PHP5 and libcurl
Some day you may find yourself in a situation where repeatedly accessing a remote file with libcurl is too slow and resource-wasteful, but the remote file may be different in an hour so you can’t rely on a local copy to be up to date. When that day comes, this snippet should do the trick.
Note that the $cache_dir must be writable by PHP, and that you must be using PHP5 with the libcurl binding (check your phpinfo() output if you’re not sure).
class YourParser{
var $curl; // cURL handle
var $cache_dir = 'cache_dir';
function YourParser() {
// Constructor
// Set up libcurl
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_FAILONERROR, 1);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
}
function fetch($url, $cache_life = 0) {
// Fetches a page with libcurl. Cache life measured in seconds. Returns the page content as a string.
// Does not yet support POST form fields.
if ($cache_life <= 0) {
// Caching is disabled.
// Download the page
curl_setopt($this->curl, CURLOPT_URL, $url);
return (curl_exec($this->curl));
} else {
// Caching is enabled.
$cached_fn = $this->cache_dir.’/’.md5($url);
$cached = file_get_contents($cached_fn);
if ($cached === false) {
// Could not open cached version of the file.
curl_setopt($this->curl, CURLOPT_URL, $url);
$results = curl_exec($this->curl);
// Write it to cache and return it
if (file_put_contents($cached_fn, $results) === false) {
// Failed writing to cache. Not writable?
}
return ($results);
} else {
// Cached file retrieved successfully
// test age
$age = time() - filemtime($cached_fn);
if ($age < $cache_life) {
// Cache is still alive
return ($cached);
} else {
// Cache is expired
curl_setopt($this->curl, CURLOPT_URL, $url);
$results = curl_exec($this->curl);
// Write it to cache and return it
if (file_put_contents($cached_fn, $results) === false) {
// Failed writing to cache. Not writable?
}
return ($results);
}
}
}
}
}

By email





















