Embed RSS Feeds in Pages with PHP and MagpieRSS
There are lots of scripts out there to put RSS on your page. A surprising number of them are Javascript based. I understand the reasoning for this. Since so many people don't have access or skills to edit server-side code, it's easy to embed. Unfortunately, lots of people who *could* edit the code on the server are also using that method.
Aside from just plain having control over the formatting and content, the fact that a PHP version of an RSS feed "embedder" gives you a page that Google reads as actual content for indexing and ad placement makes for a big bonus.
This script is the one powering the front page of :phpgeek: that shows the PHP articles from :here:. The next step that I intend to integrate for that site's implementation is to proxy the articles themselves over. In the mean time, it works fairly well.
In order to make the script as flexible as possible and able to be integrated into a variety of loosely coupled systems, this one script can either be included and the main function called from another PHP script or it can be used as a very basic web service and called from a URL.
To include it and call the function would look like this:
include("rss2htmlblock.php");
$php_feedurl = "http://www.wynia.org/wordpress/?feed=rss2&category_name=php";
display_feed($php_feedurl);
That would be replaced by the HTML block.
Calling it by URL looks like this (obviously wrapped to fit):
http://www.wynia.org/onyxcube/rss2htmlblock.php?feedurl=
http://www.wynia.org/wordpress/?feed=rss2&category_name=php
At any rate, here's the script itself, commented to explain.
<?php
//Change the path to your MagpieRSS directory
require_once('/html/lib/magpierss/rss_fetch.inc');
//This is the function wrapper for the functionality.
//$url is an RSS feed URL string
function display_feed($url){
//Use MagpieRSS to grab the content of the feed
$rss = fetch_rss($url);
//If you're curious what it looks like, dump out the contents
//print_r($rss);
//We pull out the channel information.
//This could be used to add a title and link to the feed.
//If you were offering the feed to the public, you
//probably want to do that.
$channel = array_slice($rss->channel,0);
//The items are what we'll display
$items = array_slice($rss->items,0);
//Loop through the items
foreach ($items as $item){
//Grab the useful bits of the item
$id = $item['guid'];
$link = $item['link'];
$subject = $item['title'];
$content = $item['content']['encoded'];
$description = $item['description'];
//spit out the actual HTML.
//The sample shows the description with a link to read the
//real item.
print("<div class='post'>");
print("<div class='post-title'>$subject</div>");
print("<div class='content'>$description <a href='$link'>Read Entire Article</a></div>");
print("</div>");
}
}
//This is the mini web service portion
//Check for a feedurl on the request
if($_GET['feedurl']){
//Show the feed directly
display_feed($_GET['feedurl']);
}
?>
The output is left entirely unstyled, so you'll want to add CSS like the following to make it match your site, but it's functional the way it is. Enjoy.
.post {
width: 450px;
margin-bottom: 20px;
}
.post-title {
font-size: 1.3em;
font-weight: bold;
}
Incidentally, I think this is the basis for a much better system for managing multiple blogs than any I've looked at. I've been frustrated as to exactly what to do with stuff that doesn't really belong on a certain site or stuff that belongs on more than one site. In my vision…
You'd have a central authoring place, where you added the content and did so, not for any specific blog or site. That content would just be a big pile of your authored content. You'd then build feeds for your sites from it. I'd make a :here: feed that would result in the contents of this blog and a :phpgeek: feed that would be its own. This goes along with my belief that RSS is more of a delivery mechanism than anything else and we're going to see a lot more of them being nothing more than customized views and queries.
This would give you a simple, centralized way to build your own blogging network on a variety of topics and the maintenance of the blogging engine would be limited to dropping this script and some caching into place inside a layout template of some sort. It'd be fast, simple, easy to use and easy to maintain. I just need to figure out the big flaw I'm missing that makes it impractical (there always is one).

October 15th, 2005 at 3:16 pm
You missed the homepage for Magpie: http://magpierss.sourceforge.net
And your comments form isn't working in IE.
October 15th, 2005 at 10:18 pm
The autoreplace function actually should have caught that, but didn't. I did have the URL in the other MagpieRSS articles.
I'm not sure what problems you're seeing in IE as I was just able to post a comment completely using IE.
November 3rd, 2005 at 10:11 pm
is there a simple way to display more than 1 feed on a page using the script? currently throws a "can't redeclare display_feed" error.
November 4th, 2005 at 5:17 am
Yeah, change the "include" to "require_once" and it should be fine.
January 11th, 2006 at 6:07 pm
[...] link [...]
August 27th, 2006 at 6:17 pm
hello - I am looking for a way to reproduce pieces of blog postings from a (LiveJournal) blog onto my client's homepage.
Would this work well for that application? Can I hire you to provide support?
caroline
408-448-4615
August 28th, 2006 at 6:42 pm
That shouldn't be a problem. I can provide support, but only through my current consulting firm, due to my contract with them. Do note that Meritide charges about $1000/day for my time.
1-800-MERITIDE
September 14th, 2006 at 1:52 pm
So, the example page for PHP Geek isn't working, has a bunch of errors where the feed should be. That doesn't give me much confidence in the script. Just thought I'd let you know.
August 21st, 2007 at 10:45 am
Hi!
My blog is at http://www.passerine.net/ and my photoblog is at http://www.passerine.net/photography/
I want to embed the photo's from my photoblog's RSS feed into the sidebar of my main blog.
I would be greatly obliged if I could be helped with this…
P.S. I'm no programmer!
Thanks You.
February 9th, 2009 at 1:02 pm
I used this to display the RSS feed of our churches photo albums off of Picasa's web albums. Worked great. Thanks!