views:

45

answers:

2

using simplexml to parse a feed, but I want to grab the date of the oldest item in the feed. Does anyone know how to do it? Thanks

$rss = simplexml_load_file("http://search.twitter.com/search.atom?lang=en&q=foobar&rpp=100&page=1");

+1  A: 
  1. First get all items from RSS feed and store them in array something like this.

  2. Now sort array by date.

  3. Get the first/last result depending on your sorting order.

NAVEED
A: 

If you simply want to get the last <entry>element (in document-order) you can use SimpleXMLElement::xpath() and the last() function
http://www.w3.org/TR/xpath/ says:

child::para[position()=last()] selects the last para child of the context node

e.g.

$url = "http://search.twitter.com/search.atom?lang=en&amp;q=foobar&amp;rpp=100&amp;page=1"; 
$feed = simplexml_load_file($url);
$feed->registerXPathNamespace('atom', 'http://www.w3.org/2005/Atom');

$entry = $feed->xpath('//atom:entry[position()=last()]');
if ( isset($entry[0]) ) {
  $entry = $entry[0];
}
else {
  die('not found');
}
var_dump($entry);

[position()=last()] can be shortened to [last()]

If the oldest entry is not the last entry in document-order, you'll need something else.

VolkerK
there is a stray ")" in there, but once I removed it the code worked fine. Thanks
Steven
oh yes. Corrected.
VolkerK