1 comment »

Integrating external feeds: Part 1

This post creates a basic feed reader using SimplePie and PHP

A wise man one said "You have been given two hands. One to give and the other to receive." In the spirit of that quote we are going to write a multiple part article on itegrating remote RSS feeds into your existing website.



The examples in this series are meant to be a guide for integrating feeds and not necessarily an exhaustive imlementation so standard *disclaimers apply. That having been said, let's begin.

 

The Tools

In this first article we will show you how to implement a basic RSS feed using SimplePie. We have chosen SimplePie because its implementation of feed reading is reasonably complete with support for RSS and Atom as well as enclosures for the podcast inclined. SimplePie requires PHP 4.3+ or PHP 5.0.3+ which is a pretty common configuration for most servers and hosting environments. It also needs the XML extension and curl or an unlocked fopen. If you have these, you may now pass Go and download the scripts so we can begin.

In addition to SimplePie's ability to read feeds, it is also responsible for the caching of feeds and this is a tremendous value. Some feed reading examples use Javascript and by virtue of its use the feeds that are displayed suffer common maladies like no or poor caching resulting in slow page loads for visitors and no SEO value for the actual feed. While these are important topics, they are not appropriate for this article and have been addressed in other posts.

Because the code is so brief in this example, we will merely address the key points which inclusion of SimplePie and creating the objects that read the feeds.

Rather than reference the scripts through relative paths, this script expects and requires that SimplePie is installed in a subdirectory below the example script and named 'SimplePie'

SimplePie uses classes to encapsulate the feed reading behavior so elementary understanding of objects and OOP would be helpful but these next tidbits will example what is happening.

 

The million mile view

The following line of code creates a new SimplePie object and is resposible for doing the basic housekeeping that makes feed reading possible.

$feed = new SimplePie();

This line tells the feed reading object which URL we will be processing. The URL was passed into the feed reader function and for the sake of this example is pointing at our own RSS feed.

$feed->feed_url($url);

This line causes SimplePie to read the feed and store the RSS items for later access. At this point SimplePie has decided if the cache is present and/or expired and chooses to either read the feed from the website or simply pull a copy from its cache. SimplePie allows you to set the cache lifetime but we have opted for the defaults to keep this example simple.

$feed->init();

The next lines of code in the full listing below merely walk the items that were pulled and displays them. We have used basic tags for display so the output can be customized through CSS.

This next block represents the functional example which forms the basis example.

<?php
/* Feed reader article #1 by John McKnight @ RSS Pieces.
* Notes:
* This code is written as a function to ease integration into the following articles.
* It is also intended to be embedded into an existing page so it will not generate headers or a valid HTML document
* but instead creates a rudimentary piece of HTML to load into an existing page.
* There are also provisions for an options array to be passed in which will be expanded
* so that it can be configured easily.
*/

error_reporting(E_ALL);
ini_set('display_errors', 1);

// The feed to read.
$feedURL = 'http://www.rsspieces.com/RSS2';

include_once(dirname(__FILE__) . '/SimplePie/simplepie.inc');

feedReader($feedURL);

function feedReader($url, $options = array()) {
$feed = new SimplePie();
$feed->feed_url($url);
$feed->init();
echo '<div class="RSSResults">';
if ($feed->data):
$items = $feed->get_items();
echo '<div class="RSSSummary">Displaying ' . $feed->get_item_quantity() . ' most recent entries.</div>';
foreach($items as $item):
echo '<div class="RSSItem">';
echo '<h4><a href="' . $item->get_permalink() . '">' . $item->get_title() . '</a> ' . $item->get_date('j M Y') . '</h4>';
echo $item->get_description();
echo '</div>';
endforeach;
echo '</div>';
endif;
}

?>

 

I'll leave you with this code to test, digest and play with for now. In part two we will implement the options parameter and roll the code into a WordPress plugin so you can see it in action.

*While we strive to write complete and functional examples it is possible that a bug or two slips through the cracks. Where possible and known they will be addressed but this should certainly get you started.

Related Posts
Integrating an RSS Feed Part 2
WHAT TO expect
MYREALTYBLOG launches
TRAFFIC DRIVING content
Generate 100+ More Quality Real Estate Leads per Month by Feeding Your Blog RSS Feeds from the MLS


http://www.rsspieces.com/00017B
digg me Reddit newsvine del.icio.us Technorati
Posted on October 19, 2006 16:38:24 by John.McKnight
Comment from: Posicionamiento valencia [Visitor] Email · http://www.posicionamientovalencia.com
****-

A good article about simple pie rss reader.

PermalinkPermalink May 08, 2008 12:04:43
Comment on this article

Your email address will not be displayed.

Your URL will be displayed.
Poor Excellent

Standard HTML is allowed in posts
Line breaks become <br />

Remember me

Allow users to contact you through a message form.
Captcha image.

Please enter the characters from the image above. (case insensitive)

This post has no feedback awaiting moderation...


real estate blogs

Like what you have been reading on this site?
Subscribe to our feed below.




Buy a Real Estate Blog

[Valid RSS 2.0] [Valid Atom 1.0] Valid XHTML 1.0 Transitional
All three RSS feeds validate - RSS, RSS 1.0, RSS 2.0
A note about validation
Call today to start a real estate blog. Toll free: 866-403-9626