【问题标题】:php how do I grab the date of oldest item in rss feedphp 如何获取 rss 提要中最旧项目的日期
【发布时间】:2011-02-06 10:28:22
【问题描述】:

使用 simplexml 解析提要,但我想获取提要中最旧项目的日期。有谁知道该怎么做?谢谢

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

【问题讨论】:

  • 条目是否已排序,即是否要抓取提要中的最后一个 元素?
  • 是最后一个条目->已发布

标签: php rss twitter


【解决方案1】:
  1. 首先从 RSS 提要中获取所有项目并将它们存储在数组中,例如 this

  2. Now sort array by date

  3. 根据您的排序顺序获取第一个/最后一个结果。

【讨论】:

    【解决方案2】:

    如果你只是想得到最后一个<entry>element(在document-order中)你可以使用SimpleXMLElement::xpath()和last()函数
    http://www.w3.org/TR/xpath/说:

    child::para[position()=last()] 选择上下文节点的最后一个 para 子节点

    例如

    $url = "http://search.twitter.com/search.atom?lang=en&q=foobar&rpp=100&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()]可以简写为[last()]

    如果最旧的条目不是文档顺序中的最后一个条目,则需要其他内容。

    【讨论】:

    • 那里有一个杂散的“)”,但是一旦我删除它,代码就可以正常工作。谢谢
    猜你喜欢
    • 2010-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-27
    • 1970-01-01
    • 2022-01-08
    • 2010-09-18
    • 1970-01-01
    相关资源
    最近更新 更多