As promised here is a quick run down of how to exploit the multitude of RSS feeds out there. With a fairly simple chunk of php you can embed any RSS feed into your page giving you instant dynamic news content.
First things first all credit for this method goes to the authors of the 0'Reilly PHP cookbook as that is where I first encountered the method. If you are fairly new to php and looking for an instant how-to guide on how to do the most common php tasks then I would certainly recommend that book.
Anyway cutting to the chase below is the code for pulling in the RSS feed with a quick explanation and a download link at the end for the terminally lazy.
<?php
require 'XML/RSS.php';
$feed = 'http://news.google.co.uk/news?hl=en&ned=uk&q=
snakes+on+a+plane&ie=UTF-8&output=rss';
$rss =& new XML_RSS($feed);
$rss->parse();
print "<ul>\n";
foreach ($rss->getItems() as $item) {
print '<li>' .$item['description'] . "<li>\n";
}
print "</ul>\n";
?>
Straight forward enough right? No? Well ok then maybe a bit of explanation is in order.
require 'XML/RSS.php';
The first thing you will notice is that we are using require to pull in a file called RSS.php this is the PEAR XML_RSS class which is doing all of the work for us. You will need to make sure you have PEAR installed on your server for any of this to work.
$feed = 'http://news.google.co.uk/news?hl=en&ned=uk&q=
snakes+on+a+plane&ie=UTF-8&output=rss';
Next we set the variable $feed to whatever feed we want to display on the site. I have chosen the topical news about snakes on planes always a good one to stay on top of.
$rss =& new XML_RSS($feed);
$rss->parse();
$rss is a new XML_RSS object with the feed set to whatever we defined $feed as. The feed is then parsed by XML_RSS::parse()
print "<ul>\n";
foreach ($rss->getItems() as $item) {
print '<li>' .$item['description'] . "<li>\n";
}
print "</ul>\n";
?>
The rest of the code just prints out each item in the parsed feed as part of an unordered list. Depending on what information you want you can change the print statement around for instance:
print "<ul>\n";
foreach ($rss->getItems() as $item) {
print '<li><a href="' . $item['link'] . '">' . $item['title'] . "</a><li>\n";
}
print "</ul>\n";
?>
That would display linked titles above each item whereas the initial example was set to display only the description, if you are getting your feed from Google News they include the title and link in the description so it all comes out a bit neater.
Thats about it, told you it was simple. In fact so simple that you are surely either kicking youself that you haven't done it before or wondering why I am telling people about something that your grandma has been doing for years. In which case why are you still reading, dumb ass.
If you can't be bothered to even copy and paste the example you can download the source file or check out the example
Hope this was of some use anyway.