Short guide to include RSS on your website
Including RSS feeds in to your own pages is pretty simple with PHP and some open source libraries. The follwing lines show how to create HTML from RSS feeds using the popular PHP tools magpierss (fetches and parses the RSS feed) and Smarty (template engine).
It may look like overkill to use such powerful tools for such a simple task. But once installed, both give a variety of functions and reduce the self-written code to a few lines.
Here we go:
A. Preparing the Webhost/Server
A current version of PHP is necessary. The following tutorial assumes that all files are placed in the folder /rssdemo on your web host.
1. Install magpierss RSS tool
I am not sure if I am allowed to add URLs here, so just google for "magpierss" and take the first result. Follow the DOWNLOAD link and download the recent version of magpierss (0.72 as I write this).
The file comes as .tar.gz format, so if you are on a Windows PC, you will need additional software to unzip this. I think WinZip does the job, you can also use the free tool "7-zip". The first "Extract" will make a .tar-file out of the .tar.gz, the second extraction should create a folder named magpierss-0.72.
2. Install Smarty template engine
Google for "smarty" and follow the link to php.net. Download the the recent version of Smarty (2.6.12 as I write this).
The file also comes as .tar.gz format, after extraction there should be a folder named Smarty-2.6.12.
3. Rename folders and move them to /rssdemo
Whereever you extracted both folders, now it is time to move them. First delete the version numbers out of the folder names. Change magpierss-0.72 to magpierss and Smarty-2.6.12 to Smarty. Move both folders into the /rssdemo folder on your local test server or upload them to your web server.
4. Smarty needs some special folders.
Please create the following folders in the /rssdemo folder:
- cache
- configs
- templates
- templates_c
The last one, templates_c, needs to have write permission for Smarty. If your webhost is on Unix, set the permissions as needed (777 in the worst case, depends on server config).
B. A few lines of PHP for parsing and displaying the feed.
Now, with all the installation work done, we only need a few more lines of code for our task. Place the follwing code in a file named feed.php in the /rssdemo folder.
------------------
<?
/* The folder where this file is located. Change to whatever you need */
$mydir = "/rssdemo";
/* Include magpierss and Smarty library */
require_once($_SERVER["DOCUMENT_ROOT"].$mydir."/Smarty/libs/Smarty.class.php");
require_once($_SERVER["DOCUMENT_ROOT"].$mydir."/magpierss/rss_fetch.inc");
/* Create a template object for further use */
$tpl = new Smarty();
/* Set folders for Smarty object. This folders have to exist on your web server (Check A.3) */
$tpl->template_dir = $_SERVER["DOCUMENT_ROOT"].$mydir.'/templates/';
$tpl->compile_dir = $_SERVER["DOCUMENT_ROOT"].$mydir.'/templates_c/';
$tpl->config_dir = $_SERVER["DOCUMENT_ROOT"].$mydir.'/configs/';
$tpl->cache_dir = $_SERVER["DOCUMENT_ROOT"].$mydir.'/cache/';
/* The URL of the feed we want to include */
$url = "http://rss.news.yahoo.com/rss/world";
/* magpierss does all the work! */
$rss = fetch_rss($url);
/* Uncomment the following line to see the object and array data returned. Good to see which other information has been processed by magpierss */
// echo "<pre>"; print_r($rss); echo "</pre>";
/* If the RSS could be parsed, add it to the template */
if ($rss) {
/* Sends the feed title to the template engine */
$tpl->assign("feedtitle",$rss->channel["title"]);
/* Sends the RSS items as an array */
$tpl->assign("items",$rss->items);
}
else {
/* RSS problem? */
$tpl->assign("feedtitle","Problem with: $url");
}
/* Fill the template file itemlist.html with the information and return it */
$feedhtml = $tpl->fetch($_SERVER["DOCUMENT_ROOT"].$mydir."/templates/itemlist.html");
/* Do with $feedhtml whatever you want */
echo $feedhtml;
?>
------------------
C. We need a template!
Smarty is a very powerful template engine which makes creating tables and lists a snap. For this demo we need a simple template that prints the name of the feed in the title and lists the items.
Copy the following lines into a file named "itemlist.html" and save it in the /rssdemo/templates folder.
------------------
{* very simple template for a table with RSS items *}
<table>
{* Show the feed title as a header in the first row of the table *}
<tr><th style="font-size: 24px; border: 1px solid black;">{$feedtitle}</th></tr>
{* loop through all items and add date, link, title and description in the follwing section *}
{section name=x loop=$items}
<tr><td class="rssitem" style="border: 1px solid red;">
<strong>{$items[x].pubdate}: <a href="{$items[x].link}" target="_blank">{$items[x].title}</a></strong><br>
{$items[x].description}
</td></tr>
{/section}
</table>
{* That's it. Table can be formatted using some CSS classes *}
------------------
D. Putting it all together
Now browse to the URL /rssdemo/feed.php on your web server. The feed should be displayed with some red lines around each item.
E. Serious usage
Usually you want to include the feed output somewhere into your page. The simplest way is the name your pages .php instead of .html and add the follwing line where the feed html should be displayed.
<? include "rssdemo/feed.php"?>
e.g. like this
<table>
<tr>
<td width="80%">Left column with some information</td>
<td>Right column contains the feed<br /> <? include "rssdemo/feed.php"?></td>
</tr>
</table>
If you can't or don't want to rename your existing files, you could force your webserver to parse even .html-files for PHP code. If you use Apache, you could add the "AddType application/x-httpd-php .php .html" to your .htaccess. But this is another story.
I am sure I forgot some important things, so please feel free to ask. I installed both libraries and scripted and tested the PHP and template code as I wrote this on my local development server, so everything should work as expected.