Skip to content

Instantly share code, notes, and snippets.

@finalwebsites
Created April 9, 2016 06:44
Show Gist options
  • Save finalwebsites/d30ba455d849944c7698c9912777c16a to your computer and use it in GitHub Desktop.
Save finalwebsites/d30ba455d849944c7698c9912777c16a to your computer and use it in GitHub Desktop.
RSS parser script
<?php
$cache_time = 3600*24; // 24 hours
$cache_file = $_SERVER['DOCUMENT_ROOT'].'/cache/test.rss';
$timedif = @(time() - filemtime($cache_file));
if (file_exists($cache_file) && $timedif < $cache_time) {
$string = file_get_contents($cache_file);
} else {
$string = file_get_contents('http://www.web-development-blog.com/feed/');
if ($f = @fopen($cache_file, 'w')) {
fwrite ($f, $string, strlen($string));
fclose($f);
}
}
$xml = simplexml_load_string($string);
// place the code below somewhere in your html
echo '
<div id="rssbox">
<ul>';
$count = 0;
$max = 3;
// the next object is an example for a feed from wordpress
foreach ($xml->channel->item as $val) {
if ($count < $max) {
echo '
<li>
<strong>'.$val->title.'</strong><br />
'.$val->description.' | <a href="'.$val->link.'">More &gt;</a>
</li>';
}
$count++;
}
echo '
</ul>
</div>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment