In this third part of a five-part series on Web Services, we'll wrap up our discussion of MagpieRSS and move on to SimpleXML, an intuitive methodology for processing XML structures. 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).
Some Web site developers are so keen on RSS that they wind up dumping quite a bit of information into their published feeds. However, you might be interested in viewing only the most recent items and ignoring the rest. Because Magpie relies heavily on standard PHP language features such as arrays and objects for managing RSS data, limiting the number of headlines is trivial because you can call upon one of PHPís default array functions for the task. The functionarray_slice()should do the job quite nicely. For example, suppose you want to limit total headlines displayed for a given feed to three. You can usearray_slice()to truncate it prior to iteration, like so:
$rss->items = array_slice($rss->items, 0, 3);
One final topic to discuss regarding Magpie is its caching feature. By default, Magpie caches feeds for 60 minutes, on the premise that the typical feed will likely not be updated more than once per hour. Therefore, even if you constantly attempt to retrieve the same feeds, say once every 5 minutes, any updates will not appear until the cached feed is at least 60 minutes old. However, some feeds are published more than once an hour, or the feed might be used to publish somewhat more pressing information. (RSS feeds donít necessarily have to be used for browsing news headlines; you could use them to publish information about system health, logs, or any other data that could be adapted to its structure. Itís also possible to extend RSS as of version 2.0, but this matter is beyond the scope of this book.) In such cases, you may want to consider modifying the default behavior.
To completely disable caching, disable the constantMAGPIE_CACHE_ON, like so:
To change the default cache time (measured in seconds), you can modify the constantMAGPIE_CACHE_AGE, like so:
Finally, you can opt to display an error instead of a cached feed in the case that the fetch fails by enabling the constantMAGPIE_CACHE_FRESH_ONLY:
You can also change the default cache location (by default, the same location as the executing script) by modifying theMAGPIE_CACHE_DIRconstant: