Home arrow PHP arrow Web Services: SimpleXML

Web Services: SimpleXML

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

  1. Web Services: SimpleXML
  2. SimpleXML
  3. Loading XML
  4. Parsing the XML
By: Apress Publishing
Rating: starstarstarstarstar / 1
August 05, 2010

print this article



Limiting the Number of Displayed Headlines

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

Caching Feeds

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:

define('MAGPIE_CACHE_ON', 0);

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:

define('MAGPIE_CACHE_DIR', '/tmp/magpiecache/');

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates


Dev Shed Tutorial Topics: