This is a very simplified snippet to
read RSS feeds from another site and display them on yours.
Why would this be useful? Well, in the case of the company I work for, we have a blog that we want to show titles and content for.
This should be an interesting step up for someone who’d like to make a serious snippet out of this.
Example call:
[!RSSFeed? &url=`http://nimja.com/nimja.rss` &words=`30`!]
<?php
// RSSFeed reader by Nimja.com
// Save as "RSSFeed"
//URL to read, can be set in snippet call.
$url = (!empty($url)) ? $url : "http://nimja.com/nimja.rss";
//Words in the 'preview', can be set in snippet call.
$words = (!empty($words)) ? $words : 30;
//Get feed contents (the easy way)
$feed = file_get_contents($url);
//Open a dom object and get all the 'item's
$dom = domxml_open_mem($feed) or die('error');
$root = $dom->document_element();
$items = $root->get_elements_by_tagname('item');
//Loop over the items.
foreach ($items as $item) {
$title = $item->get_elements_by_tagname('title');
$desc = $item->get_elements_by_tagname('description');
$link = $item->get_elements_by_tagname('link');
$date = $item->get_elements_by_tagname('pubDate');
$time = strtotime($date[0]->get_content());
$text = strip_tags($desc[0]->get_content());
$words_array = array_slice(split(' ', $text), 0, $words);
$description = implode(' ', $words_array).'...';
echo '<h2>'.$title[0]->get_content().'<span>'.date('d M Y',$time).'</span></h2>';
echo $description.'<br />';
echo '<a href="'.$link[0]->get_content().'">Read More</a><br /><br />';
}
?>
ps. For the less lazy, it should be fairly easy to create an ATOM reader the same way. I didn’t look at the differences myself much.