Keep a Copy of the Feed Structure


Drop the feed into either a SimpleXML or MiniXML construct, and use print_r() to view the contents. From here you should be able to spec out your code in detail. Keep print_r() handy while coding because it is a great tool to re-examine the branch of a feed you are working with when you are having problems. I often like to keep a hard copy of a print_r() dump handy while playing with an XML document.

Doing a quick dump of the Yahoo! feed results in the following (some items have been shortened for space — the description tag for one, to fit on one line and the indenting has been modified):

 SimpleXMLElement Object (  [channel] => SimpleXMLElement Object  (   [title] => Yahoo! News: Technology - Software   [copyright] => Copyright (c) 2004 Yahoo! Inc. All rights reserved.     [description] => Technology - Software   [language] => en-us   [lastBuildDate] => Fri, 26 Nov 2004 18:50:07 GMT   [ttl] => 5   [image] => SimpleXMLElement Object   (    [title] => Yahoo! News    [width] => 142    [height] => 18       [url] => http://us.i1.yimg.com/us.yimg.com/i/us/nws/th/main_142.gif   )   [item] => Array   (    [0] => SimpleXMLElement Object    (     [title] => Recording Industry, File-Share Face Off (AP)     [link] => http://us.rd.yahoo.com/dailynews/rss/software/* http://story.news.yahoo.com/news?tmpl=story2&u=/ap/20041126/       ap_on_bi_ge/kazaa_trial     [guid] => ap/20041126/kazaa_trial     [pubDate] => Fri, 26 Nov 2004 18:50:07 GMT     [description] => AP - The next chapter in the global legal battle between...    )    [1] => SimpleXMLElement Object    (     [title] => Britons Offered 'Real' Windows XP (AP)         [guid] => ap/20041126/britain_microsoft_piracy     [pubDate] => Fri, 26 Nov 2004 16:25:12 GMT     [description] => AP - Owners of pirated copies of Microsoft Corp.'s Windows...    )    ...    [49] => SimpleXMLElement Object    (     [title] => GPL 3 to Take on IP, Patents (Ziff Davis)         [guid] => zd/20041122/139714     [pubDate] => Mon, 22 Nov 2004 06:21:23 GMT     [description] => Ziff Davis - With a relatively hostile environment that has...    )   )  ) ) 

Note 

Note that running print_r() on a SimpleXML object will not reveal attributes, which are used in several places throughout feeds. If something seems to be missing, go back and look at the original source.

Looking at the feed in this manner, it is obvious not only where loops belong (a foreach around the item tag would work perfectly), but what the syntax should be to access any element in particular ($xml->item[0]->pubDate to get the publication date of the most recently posted item).

As an additional piece of wisdom to make your life easier, I suggest you grab the feed once (I like to use file_get_contents() then file_put_contents() myself, to avoid any encoding "fun" taking it on and off my Windows box) and save it on your test server. There is simply no need to pester the source of the feed constantly while testing everything. In this instance, a copy of the feed was saved with the file_ functions and as yahoo.xml.




Professional Web APIs with PHP. eBay, Google, PayPal, Amazon, FedEx, Plus Web Feeds
Professional Web APIs with PHP. eBay, Google, PayPal, Amazon, FedEx, Plus Web Feeds
ISBN: 764589547
EAN: N/A
Year: 2006
Pages: 130

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net