Display your feed on your website
What you will need for this tutorial :
- Your webspace needs to be PHP enabled to run the scripts needed for this tutorial.
- MagpieRSS, a collection of PHP scripts ready made for the job.
- A .php page to display the xml data.
Step 1 - download magpieRSS, upload to webspace
MagpieRSS is available from Sourceforge and the latest version (at the time of writing) is 0.71.1 (Full list of available versions at http://sourceforge.net/project/showfiles.php?group_id=55691 ) Download magpierss 0.71.1 to your hard drive and use Winzip or similar to unzip the .tar.gz package, then again unzip the resulting .tar package. Create a new folder called Magpie in your website setup, copy all files and folders from the unzipped 'magpierss-0.71.1' folder into your new Magpie folder and then upload to your webspace.
step 2 - add some php to your web page
In order to display your feed on a web page, we need to add some PHP code to it, which then uses your feed and the Magpie files to display the feed data.
Open up the webpage (or create a new one) in which you want the rss data to be displayed. This page must have the .php extension. At the point in the web page where you want the rss information, paste this PHP code into place.
Note : This code has been updated, checked and is now fully XHTML Strict and 1.1 compliant. The code was adapted from elixirsystems.com.
<?php require_once '/home/yoursite/public_html/magpies/rss_fetch.inc'; $rss_file = '/home/yoursite/public_html/path_to_your_feed/yourfeed.xml'; $rss_string = read_file($rss_file); $rss = new MagpieRSS( $rss_string ); echo 'Site: ', $rss->channel["title"], '<br /><br />'; if ( $rss and !$rss->ERROR) { foreach ($rss->items as $item ) { echo '<p>'; echo '<a href="' . $item[link] . '">' . $item[title] . '</a><br/>'; echo 'Publish Date: ' . $item[pubdate] . '<br /><br />'; echo $item[ description ] ; echo '</p>'; } } else { echo "RSS Error: " . $rss->ERROR . "<br /><br />" ; } function read_file($filename) { $fh = fopen($filename, 'r') or die($php_errormsg); $rss_string = fread($fh, filesize($filename) ); fclose($fh); return $rss_string; } ?>
The important lines in this code which you WILL need to change in order to work are :-
require_once '/home/yoursite/public_html/magpie/rss_fetch.inc';
$rss_file = '/home/yoursite/public_html/path_to_your_feed/yourfeed.xml';
The first line points to the rss_fetch.inc file which is one of the
magpie files we uploaded earlier.
The second file points to the feed file on your site and so you will
need to change the path and the filename to suit. The rest of the path
is determined by your webspace and webspace provider, some ISPs will
have configured your webspace so that any scripts run will need the
fully qualified server path in order to work (rather than just a relative
to site root which could be /magpie/rss_fetch.inc instead).
Upload your changed php web page to your webspace.
confirm that your feed is being displayed
Once uploaded, your newly adapted web page should work straight away, and your feed should now be showing on your web page. If not, there could be a number of reasons, the main ones being :-
1. Web Space not PHP enabled
2. The path to rss_fetch.inc and/or yourfeed.xml are not specified correctly.
3. The web page used does not have the .php extension.
The best way to solve this is to post a message in our forums boards. There are a few resource links at the end of this tutorial where you can go to find out more information and maybe get some other hints.
Want to view a working example ? Just to prove that what I have written above does indeed work as is, have a look at minitutorials.com, the forums box contains the latest posts from the forums.
want to display a feed from another site?
If you don't yet have your own feed or you just want to use someone elses to display their relevent content on your site then these are the code changes you will need to make.
Most of the code is as above, with the exception of these lines :
$rss_file = '/home/yoursite/public_html/path_to_your_feed/yourfeed.xml';
$rss_string = read_file($rss_file);
$rss = new MagpieRSS( $rss_string );
delete the three lines above from the code and replace with :
$url = 'http://www.minitutorials.com/rss/feeds/minifeed.xml' ;
$rss = fetch_rss($url);
Obviously, unless you really want the miniTutorials feed displayed on your site, replace the url with the full url of the feed file that you do want displayed on your site.
Summary
Now that you have your feed showing on your website, remember that all you need to do is update your feed.xml (or feed.rss) file and upload, the changes will show immediately on your page And at the same time the same information will go out to the desktops of whoever has subscribed to your actual feed! This makes for a great a convenient way to update your visitors on any changes or additions to your website.
Want to know more?
Related Tutorials
RSS Install - How to install an RSS Reader to retrive RSS Feeds to your Desktop.
RSS Deliver - Create your own RSS Feeds for delivery to RSS Readers or use with RSS Display.
Related Links
Here are a few good links to more information. A couple of these sites helped me enormously when I wanted to know how to display our feed and in creating this tutorial.
http://www.elixirsystems.com/seo_tips/rss_feeds_website_consuming.php
http://www.mnot.net/rss/tutorial/
http://www.devx.com/xml/Article/10790/1954?pf=true
http://blogs.law.harvard.edu/tech/encodingDescriptions
http://www-106.ibm.com/developerworks/library/w-rss.html?dwzone=web#resources
Check out the cookbook provided with the MagpieRSS download, on your hard drive then look for the \magpie\htdocs\cookbook.html file stored in your download location, or find it where you uploaded your file to.


