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).
Magpie parses a feed by placing it into an object consisting of four fields:channel,image,items, andtextinput. In turn,channel is an array of associative arrays, while the remaining three are associative arrays. The following script retrieves theblog.xmlfeed, outputting it using theprint_r()statement:
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 namedMagpie_Feedis 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.