Web Services: MagpieRSS

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

MagpieRSS 

MagpieRSS (Magpie for short) is a powerful RSS parser written in PHP by Kellan Elliott-McCrea. It’s freely available for download via http://magpierss.sourceforge.net/ and is distributed under the GPL license. Magpie offers developers an amazingly practical and easy means for retrieving and rendering RSS feeds, as you’ll soon see. In addition, Magpie offers users a number of cool features, including the following:

Simplicity: Magpie gets the job done with a minimum of effort by the developer. For example, typing a few lines of code is all it takes to begin retrieving, parsing, and converting RSS feeds into an easily readable format.

Nonvalidating: If the feed is well-formed, Magpie will successfully parse it. This means that it supports all tag sets found within the various RSS versions, as well as your own custom tags.

Bandwidth-friendly: By default, Magpie caches feed contents for 60 minutes, cutting down on use of unnecessary bandwidth. You’re free to modify the default to fit caching preferences on a per-feed basis. If retrieval is requested after the cache has expired, Magpie will retrieve the feed only if it has been changed (by checking the Last-Modified and ETag headers provided by the Web server). In addition, Magpie recognizes HTTP’s Gzip content-negotiation ability when supported.

Installing Magpie

Like most PHP classes, Magpie is as simple to install as placing the relevant files within a directory that can later be referenced from a PHP script. The instructions for doing so follow:

  1. Download Magpie from http://magpierss.sourceforge.net/ .
  2. Extract the package contents to a location convenient for inclusion from a PHP script. For instance, consider placing third-party classes within an aptly named directory located within the PHP_INSTALL_DIR/includes/ directory. Note that you can forgo the hassle of typing out the complete path to the Magpie directory by adding its location to the include_path directive found in the php.ini file.
  3. Include the Magpie class ( magpie.php ) within your script:

    require(‘magpie/magpie.php’);

That’s it. You’re ready to begin using Magpie.

{mospagebreak title=How Magpie Parses a Feed}

Magpie parses a feed by placing it into an object consisting of four fields: channel , image , items , and textinput . In turn, channel is an array of associative arrays, while the remaining three are associative arrays. The following script retrieves the blog.xml feed, outputting it using the print_r() statement:

<?php
   
require("magpie/magpie.php");
   
$url = http://localhost/book/20/blog.xml;
    $rss = fetch_rss($url);
   
print_r($rss);
?>

This returns the following output (formatted for readability):

Magpie_Feed Object (
    [items] => Array (
       
[0] => Array (
            [title] => Killer Firefox Tip #294
            [title_detail] => Array (
                [type] => text
               
[value] => Killer Firefox Tip #294
            )
            [link] => http://opensource.apress.com/article/190/
            [links] => Array (
               
[0] => Array (
                    [rel] => alternate [href] =>        
http://opensource.apress.com/article/190/

               
)
            )
           
[author] => W. Jason Gilmore
            [description] => Like most of you, I spend bunches of time
       
downloading large files from the Web,
       typically podcasts and PDF documents…
        )
        [1] => Array (
            [title] => Beginning Ubuntu Linux wins Linux Journal Award!
            [title_detail] => Array (
                [type] => text
               
[value] => Beginning Ubuntu Linux wins Linux Journal Award!
            )
            [link] => http://opensource.apress.com/article/189/
            [links] => Array (
                [0] => Array (
                    [rel] => alternate [
                    href] => http://opensource.apress.com/article/189/
               
)
            )
            [author] => Keir Thomas
            [description] => Woo hoo! My book, Beginning Ubuntu Linux, has
                                 
won an award in the Linux Journal Editor’s Choice
                                  2006 awards! More precisely…
        )

        [2] => Array (
            [title] => Forms Validation with CakePHP
            [title_detail] => Array (
                [type] => text
                [value] => Forms Validation with CakePHP
            )
            [link] => http://opensource.apress.com/article/188/
            [links] => Array (
                [0] => Array (
                    [rel] => alternate
                    [href] => http://opensource.apress.com/article/188/
                )
            )
           
[author] => W. Jason Gilmore
            [description] => Neglecting to validate user input is akin to foregoing
                             any defensive gameplan for containing the NFL’s
                             leading rusher. Chances are sooner or later…
       
)
    )
    [feed] => Array (
       
[title] => Inside Open Source
        [title_detail] => Array (
            [type] => text
            [value] => Inside Open Source
        )
        [link] => http://opensource.apress.com/
        [links] => Array (
           
[0] => Array (
                [rel] => alternate
                [href] => http:// opensource.apress.com/
           
)
        
)
    )
    [feed_type] =>
    [feed_version] =>
    [_namespaces] => Array ( )
    [from_cache] =>
    [_headers] => Array (
        [date] => Sun, 12 Nov 2006 21:11:12 GMT
        [server] => Apache/2.0.58 (Win32) PHP/5.1.4
        [last-modified] => Sun, 12 Nov 2006 21:10:41 GMT
        [etag] => "ad43-4f5-37c15b77"
        [accept-ranges] => bytes
        [content-length] => 1269
        [connection] => close
        [content-type] => application/xml
   
)
    [_etag] => "ad43-4f5-37c15b77"
    [_last_modified] => Sun, 12 Nov 2006 21:10:41 GMT
    [output_encoding] => utf-8
    [channel] => Array (
        [title] => Inside Open Source
        [title_detail] => Array (
            [type] => text
            [value] => Inside Open Source
        )
        [link] => http://opensource.apress.com/
        [links] => Array (
            [0] => Array (
                [rel] => alternate
                [href] => http://opensource.apress.com/
            )
        )
    )
)

An object named Magpie_Feed is returned, containing several attributes. This means you can access the feed content and other attributes using standard object-oriented syntax. The following examples demonstrate how the data is peeled from this object and presented in various fashions.

{mospagebreak title=Retrieving and Rendering an RSS Feed}

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

{mospagebreak title=Aggregating Feeds}

Of course, chances are you’re going to want to aggregate multiple feeds and devise some means for viewing them simultaneously. To do so, you can simply modify Listing 20-2, passing in an array of feeds. A bit of CSS may also be added to shrink the space required for output. Listing 20-3 shows the rendered version.

Listing 20-3. Aggregating Multiple Feeds with Magpie

<style><!–
p { font: 11px arial,sans-serif; margin-top: 2px;}
//–>
</style>

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

// Compile array of feeds
$feeds = array(
"http://localhost/book/20/blog.xml", "http://news.com.com/2547-1_3-0-5.xml", "http://rss.slashdot.org/Slashdot/slashdot");

// Iterate through each feed
foreach ($feeds as $feed) {

   // Retrieve the feed
   $rss = fetch_rss($feed);

   // Format the feed for the browser
   $feedTitle = $rss->channel['title'];
   echo "
"<p><strong>$feedTitle</strong><br />";

   foreach ($rss->items as $item) {
      $link = $item['link'];
      $title = $item['title'];
      $description = isset($item['description']) ? $item['description'].
                    
"<br />" : "";
     
echo "<a href="$link">$title</a><br />$description";
   
}
  
echo "</p>";

}

?>

Figure 20-3 depicts the output based on these three feeds.


Figure 20-3.  Aggregating feeds

Although the use of a static array for containing feeds certainly works, it might be more practical to maintain them within a database table, or at the very least a text file. It really all depends upon the number of feeds you’ll be using and how often you intend on managing the feeds themselves.

Please check back next week for the continuation of this article.

[gp-comments width="770" linklove="off" ]

antalya escort bayan antalya escort bayan Antalya escort diyarbakir escort