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);

}

}

}

}

}

Leave a reply

Inspiration

6pli Tumblr Aptana IDE Markus Homm Mint Humanized Rawkus Records // All Things Hip Hop // www.rawkus.com The New York Times WeShouldDoItAll Justinsomnia Deluxe Digital Media Democracy Internet Tv Take More Photos fluxiom - capture, manage, access and deliver content across your enterprise Olivier Danchin Jason Santa Maria Tubetorial Ajaxian Raincity Studios 88 Miles - Simple time tracking Welcome to Zopa (UK) - The first lending and borrowing exchange Inspirational design for a web2.0 homepage