All pastes #186598 Raw Edit

Caching with libcurl

public php v1 · immutable
#186598 ·published 2006-09-30 05:31 UTC
rendered paste body
    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.                $cache_dir = 'libs/eapia_cache';        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 = $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);                }            }            // return the results        }    }