Home arrow PHP arrow Page 3 - Web Services: MagpieRSS

Retrieving and Rendering an RSS Feed - PHP

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).

TABLE OF CONTENTS:
  1. Web Services: MagpieRSS
  2. How Magpie Parses a Feed
  3. Retrieving and Rendering an RSS Feed
  4. Aggregating Feeds
By: Apress Publishing
Rating: starstarstarstarstar / 1
July 29, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.

Listing 20-2. Rendering an RSS Feed with Magpie

<?php
   
require("magpie/magpie.php");

 

    // RSS feed location?
    $url = http://localhost/book/20/blog.xml;
    // Retrieve the feed
    $rss = fetch_rss($url);

    // 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/.



 
 
>>> More PHP Articles          >>> More By Apress Publishing
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap

Dev Shed Tutorial Topics: