データベースを必要とせずにフィードをファイル システムにキャッシュし、そこから一定時間プルする方法を次に示します。これにより、アプリが大幅に高速化されます。おそらく、それはいくつかの興味深いものです。
<?php
//Have a list of feeds
$feeds = array(
'http://rss.cnn.com/rss/cnn_topstories.rss',
'http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=breakingnews',
'http://www.nytimes.com/services/xml/rss/nyt/pop_top.xml',
'http://news.yahoo.com/rss',
);
$cache_for = 3600; //in seconds
$feed_results = array();
/**
* Loop through each feed and check if its age is older then $cache_for
* Grab the feed and store in ./feeds_data
* On next refresh feed is pulled from cache until $cache_for expires
*/
foreach($feeds as $feed){
if(cache(sha1($feed), 'check', null, './feeds_data', $cache_for) == false){
$result = curl_get($feed);
$feed_result[$feed] = cache(sha1($feed), 'put', $result, './feeds_data', $cache_for);
}else{
$feed_result[$feed] = cache(sha1($feed), 'get', null, './feeds_data', $cache_for);
}
}
//Loop through each feed result and render
foreach($feed_result as $result){
render_feed_newz_caption($result);
}
//The curl function, curl is considerably faster then fopen that simplexml_load_file uses
function curl_get($url){
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0 Firefox/5.0');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_ENCODING,'gzip,deflate');
curl_setopt($ch, CURLOPT_AUTOREFERER,true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
function cache($key, $do, $result=null, $storepath, $cacheTime=86400){
switch($do){
case "check":
if(file_exists($storepath.'/'.sha1($key).'.php')){
if((time() - $cacheTime < filemtime($storepath.'/'.sha1($key).'.php'))){
return true;
}
return false;
}else{
return false;
}
break;
case "put":
//Compress
$compressed = gzdeflate($result, 9);
$compressed = gzdeflate($compressed, 9);
file_put_contents($storepath.'/'.sha1($key).'.php', base64_encode($compressed));
return $result;
break;
case "get":
$cache = base64_decode(file_get_contents($storepath.'/'.sha1($key).".php"));
//De-compress
$compressed = gzinflate($cache);
$compressed = gzinflate($compressed);
return $compressed;
break;
default:
return false;
break;
}
}
//Function to wrap your parse
function render_feed_newz_caption($feed){
//load XML string!
$xml = simplexml_load_string($feed);
libxml_use_internal_errors(true);
if(isset($xml->channel->item))
foreach($xml->channel->item as $YODEL){
$title = $YODEL->title;
$description = $YODEL->description;
$link = $YODEL->link;
$pubDate = $YODEL->pubDate; //should be able to use Rutgers pubDate to sort newest.... get a better Drudge feed to do the same
$image = $YODEL->image;
//echo"<div class ='masonry_item' style='background: #FFFFFF;'><a href='". $link ."'>" . "RUT: " . $pubDate . "<br />" . $title . "</a> <!--" . $description . "--></div> <br>";
echo "
<div class='box'>
<div class ='newz_caption' style='background: #FFFFFF;'>
<h3>" . $title . "</h3>
<h5>CNN: " . $pubDate . "</h5>
<p> " . $description . " </p>
<p>
<a class='btn btn-primary' href='" . $link . "'> Source </a>
<a class='btn' href='#'>Thumbs Up </a>
</p>
</div>
</div>
<br>";
}
}
?>