CURL cache remote files
CURL cache remote files
Expires = $ expires; $ this-> timeOut = $ timeOut; $ this-> cacheEna = $ cacheEna; $ this-> debug = $ debug ;} /*** check and create the directory * @ return bool */public function mkdirs () {if (is_dir ($ this-> path) {return true ;} else {return mkdir ($ this-> path, 777, true );}} /*** set cache file information * @ param $ url */public function setCacheFileInfo ($ url) {$ this-> url = $ url; $ this-> path = dirname (_ FILE __). '/cache/'; $ this-> filename = $ this-> url? Md5 ($ this-> url): 'null';}/*** main method, get Data * @ param $ url * @ return bool | string */public function get ($ url) {$ this-> setCacheFileInfo ($ url ); if ($ this-> cacheEna) $ this-> data = unserialize ($ this-> getCache ($ url); if (! $ This-> data) $ this-> data = $ this-> curlGet ($ url); if ($ this-> data & $ this-> cacheEna) $ this-> setCache ($ url, serialize ($ this-> data); return $ this-> data-> contents ;} /*** curl GET request data * @ param $ url * @ return string */public function curlGet ($ url) {if ($ this-> debug & $ url = '') echo ('request URL is empty; check '); $ return = new stdClass (); $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT _ TIMEOUT, $ this-> timeOut); curl_setopt ($ ch, CURLOPT_HEADER, 0); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1 ); $ this-> curlData = curl_exec ($ ch); $ this-> curlHeader = curl_getinfo ($ ch); $ this-> curlError = curl_error ($ ch ); $ return-> header = $ this-> curlHeader; $ return-> contents = $ this-> curlData; curl_close ($ ch); if ((! $ This-> curlData | $ this-> curlHeader ['http _ Code']! = '000000') & $ this-> debug) echo 'request failed, error message :'. $ this-> curlError; else return $ return;}/*** get cache data * @ param $ url * @ return bool | string */public function getCache ($ url) {$ this-> setCacheFileInfo ($ url); if (file_exists ($ this-> path. $ this-> filename) {if (time ()-filemtime ($ this-> path. $ this-> filename) <$ this-> expires) $ this-> cacheData = file_get_contents ($ this-> path. $ this-> filename); else if ($ this-> debug) Echo 'cache file expired '; if ($ this-> cacheData) return $ this-> cacheData; else if ($ this-> debug) echo 'cached file content is Null';} else {// if ($ this-> debug) echo 'cached file does not exist'; return false ;}} /*** write cache data ** @ param $ url * @ param $ data */public function setCache ($ url, $ data) {$ this-> setCacheFileInfo ($ url ); if (! $ This-> mkdirs () & $ this-> debug) echo ('failed to create cache Directory :'. $ this-> path); if ($ data) {if (! File_put_contents ($ this-> path. $ this-> filename, $ data) & $ this-> debug) {echo 'write cache failed File :'. $ this-> path. $ this-> filename;} else {if ($ this-> debug) echo 'data is empty and no cache is written ';}}}
2.CurlCacheExample. php
get('http://www.baidu.com');header('Content-type:'.$curlCache->data->header['content_type']);echo $curlCache->data->contents;
The above is the content of remote files cached by CURL. For more information, see PHP Chinese website (www.php1.cn )!