Simple Page Caching with URL Retrival with PHP
Posted by hawk under Dirty ScriptsLet Me Hear Your Thoughts >>
So tonight you better stop and rebuild all your ruins, because peace and trust can win the day despite all your losing.
-Led Zeppelin
When attempting to use my quotes grabber for a real-time application it turned out to take to long to request pages, parse them, then return a result for a high volume site.
A simple solution for a page that doesn’t change much in 24 hours (you can of course adjust for less) is the following PHP Caching system uses just a day file and checks against it.
$v)
$ch = curl_init($v);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_HEADER ,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1);
$url_str = curl_exec($ch);
preg_match_all('~
]*>(.*?)
~is',$url_str,$matches);
foreach($matches[0] as $k=>$v){
$str .= $v;
}
file_put_contents($datename,$str);
}else{
$str = file_get_contents($datename);
}
?>
Fairly simple but sometimes simple things evade the smartest people.











