PHP:获取过去一小时的Feed内容

时间:2015-12-01 16:58:07

标签: php rss

我想获取外部RSS Feed的内容,并仅过去一小时存储条目。

所以,我可以通过以下方式获取RSS源:

$url = 'http://www.animenewsnetwork.com/all/rss.xml';
$feed = new DOMDocument();
$feed->load($url);

$print_r($feed); // display content...

现在,$feed包含所有 RSS Feed的数据。我想存储过去一小时内发布的条目的链接:

$latest_posts = array(
   $URL_1,
   $URL_2,
   $URL_3,
   $URL_4,
   //...
);

我该怎么做?

3 个答案:

答案 0 :(得分:2)

您需要遍历Feed并检查pubDate

//Loop through all the items
foreach ($feed->getElementsByTagName("item") as $item){
   //get the pubDate of the time, and compare it to time (obviously for the 1 hour ago you could do time() - 3600, but for the interest of self-documenting code in this example I've used strtotime()
   if (strtotime($item->getElementsByTagName("pubDate")->item(0)->nodeValue) >= strtotime("-1 hour")){
        //If it is, add it to the array...
        $latest_posts[] = $item->getElementsByTagName("link")->item(0)->nodeValue;
    } else {
        break; //If this post is more than 1 hour old, then so will the rest of them be, so break out of the loop.
    }
}

答案 1 :(得分:1)

简单地将xml转换为数组 Demo

您可以使用此代码进行转换

$feed = 'http://www.animenewsnetwork.com/all/rss.xml';
$feed_to_array = (array) simplexml_load_file($feed);
//OR $feed_to_array = (array) new SimpleXmlElement( file_get_contents($feed) );
// print_r($feed_to_array); // if you want see the array

$feeds_i_need = array();
foreach($feed_to_array['channel']['item'] as $item) {
    if (strtotime($item['pubDate'] >= strtotime("-1 hour")))
         $feeds_i_need[] = $item;
    else
         break; // I did break so it will stop loop for others
}

答案 2 :(得分:1)

这是我的第一篇文章。我希望这对你有帮助。

我无法使用de load函数获取xml。我认为这更简单。

抱歉我的英文。

date_default_timezone_set('America/Buenos_Aires');
$url = 'http://www.animenewsnetwork.com/all/rss.xml';
$content = file_get_contents($url);
$x = new SimpleXmlElement($content);
$now = new DateTime('Tue, 01 Dec 2015 11:57:02 -0500');<--set your current date time.
$last_hour_feeds = array();
foreach($x->channel->item as $entry) {
    $itemPubDate = new DateTime($entry->pubDate);
    $difference = $now->diff($itemPubDate);//php version > 5.3
    /*  
   [y] => 0
   [m] => 0
   [d] => 6
   [h] => 20
   [i] => 17
   [s] => 2
   [weekday] => 0
   [weekday_behavior] => 0
   [first_last_day_of] => 0
   [invert] => 1
   [days] => 6
 */
    if (!$difference->days && !$difference->h && $difference->invert){
        $last_hour_feeds[] = $entry->link; 
    }   
}
print_r($last_hour_feeds);