The objective here is to display an RSS feed. We have picked one that looks interesting from the BBC website, we will have alook at the science and nature news feed which is http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/sci/tech/rss.xml
Firstly we are going to use 2 Pear packages to achieve this we will need the XML_Parser package and also the XML_RSS package. Underneath you can see 2 dowload links to get these packages.
XML_parser package
XML_RSS package
Now download these to a location on your PC and extract all of the contents in them.
The next step involves creating a folder on your website, this is where we tend to upload all of these packages. In this case lets call it the pear folder.
Create a folder called xmlparser and xmlrss and put the contents of the respective dowloaded packages into the folders.
The first step requires opening the RSS.php file that you should now see in the xmlrss folder, locate the line of code that begins with
require_once 'XML/Parser.php';
change this to the following to allow the script to work
require_once 'pear/xmlparser/Parser.php';
Now for the script to get this all working
<?php
require_once "pear/xmlrss/RSS.php";
//this is the rss news feed you wish to display
$rss =& new XML_RSS("http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/sci/tech/rss.xml");
$rss->parse();
//display a line of text saying where this is from (good manners)
echo "<h1>Headlines from <a href=\"http://www.bbc.co.uk\">BBC Science/Nature</a></h1>\n";
echo "<ul>\n";
//loop through all of the items
foreach ($rss->getItems() as $item)
{
echo "<li><a href=\"" . $item['link'] . "\">" . $item['title'] . "</a></li>\n";
}
echo "</ul>\n";
?>
If you look at the bottom of the page you can see this in action |