In this second part of a five-part series on Web Services, you will learn how to install and use MagpieRSS, a popular RSS parser. This article is excerpted from chapter 20 of the book Beginning PHP and Oracle: From Novice to Professional, written by W. Jason Gilmore and Bob Bryla (Apress; ISBN: 1590597702).
Based on your knowledge of Magpie’s parsing behavior, rendering the feed components should be trivial. Listing 20-2 demonstrates how easy it is to render a retrieved feed within a standard browser.
// Format the feed for the browser $feedTitle = $rss->channel['title']; echo "Latest News from <strong>$feedTitle</strong>"; foreach ($rss->items as $item) { $link = $item['link']; $title = $item['title']; // Not all items necessarily have a description, so test for one. $description = isset($item['description']) ? $item['description'] : ""; echo "<p><a href="$link">$title</a><br />$description</p>"; }
?>
Note that Magpie does all of the hard work of parsing the RSS document, placing the data into easily referenced arrays. Figure 20-2 shows the fruits of this script.
Figure 20-2. Rendering an RSS feed within the browser
As you can see in Figure 20-2, each feed item is formatted with the title linking to the complete entry. So, for example, following the Killer Firefox Tip #294 link will take the user to http://opensource.apress.com/article/190/.