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