Embed RSS Feeds in Pages with PHP and MagpieRSS

Originally published: 09/2005 by J Wynia

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).

Comments

Sarah on 10/15/2005
You missed the homepage for Magpie: http://magpierss.sourceforge.net And your comments form isn't working in IE.
J Wynia on 10/15/2005
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.
Kurt on 11/3/2005
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.
J Wynia on 11/4/2005
Yeah, change the "include" to "require_once" and it should be fine.
welchnet.us » more magpie on 1/11/2006
[...] link [...]
caroline on 8/27/2006
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
J Wynia on 8/28/2006
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
Jesse on 9/14/2006
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.
Sumedh on 8/21/2007
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.
Mike P. on 2/9/2009
I used this to display the RSS feed of our churches photo albums off of Picasa's web albums. Worked great. Thanks!
blog comments powered by Disqus
Or, browse the archives.
© 2003- 2014 J Wynia. Very Few Rights Reserved. This article is licensed under the terms of the Creative Commons Attribution License. Quoted content or content included from others is not subject to that license and defaults to normal copyright.