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

PHP:获取过去一小时的提要内容,php,rss,Php,Rss,我只想获取外部RSS源的内容,并存储上一小时的条目 因此,我可以通过以下方式获取RSS提要: $url = 'http://www.animenewsnetwork.com/all/rss.xml'; $feed = new DOMDocument(); $feed->load($url); $print_r($feed); // display content... 现在,$feed包含RSS提要的所有数据。我只想存储过去一小时内发布的条目的链接: $latest_posts = a

我只想获取外部RSS源的内容,并存储上一小时的条目

因此,我可以通过以下方式获取RSS提要:

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

$print_r($feed); // display content...
现在,
$feed
包含RSS提要的所有数据。我只想存储过去一小时内发布的条目的链接:

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

我该怎么做

您需要循环浏览提要并检查发布日期

//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.
    }
}

您需要循环浏览提要并检查发布日期

//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.
    }
}

将xml转换为数组的简单方法

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

$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
}

将xml转换为数组的简单方法

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

$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
}

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

我无法使用卸载函数获取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);
日期默认时区设置(“美国/布宜诺斯艾利斯”);
$url='1http://www.animenewsnetwork.com/all/rss.xml';
$content=file\u get\u contents($url);
$x=新的SimpleXmlElement($content);
$now=新日期时间('2015年12月1日星期二11:57:02-0500');频道->项目作为$entry){
$itemPubDate=新日期时间($entry->pubDate);
$difference=$now->diff($itemPubDate);//php版本>5.3
/*  
[y] =>0
[m] =>0
[d] =>6
[h] =>20
[i] =>17
[s] =>2
[工作日]=>0
[工作日行为]=>0
[第一天最后一天]=>0
[反转]=>1
[天]=>6天
*/
如果(!$difference->days&&!$difference->h&&$difference->invert){
$last\u hour\u feeds[]=$entry->link;
}   
}
打印($last\u hour\u feed);

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

我无法使用卸载函数获取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);
日期默认时区设置(“美国/布宜诺斯艾利斯”);
$url='1http://www.animenewsnetwork.com/all/rss.xml';
$content=file\u get\u contents($url);
$x=新的SimpleXmlElement($content);
$now=新日期时间('2015年12月1日星期二11:57:02-0500');频道->项目作为$entry){
$itemPubDate=新日期时间($entry->pubDate);
$difference=$now->diff($itemPubDate);//php版本>5.3
/*  
[y] =>0
[m] =>0
[d] =>6
[h] =>20
[i] =>17
[s] =>2
[工作日]=>0
[工作日行为]=>0
[第一天最后一天]=>0
[反转]=>1
[天]=>6天
*/
如果(!$difference->days&&!$difference->h&&$difference->invert){
$last\u hour\u feeds[]=$entry->link;
}   
}
打印($last\u hour\u feed);


一旦该问题符合条件,我将给予50分奖励。除非订阅源url提供过滤选项,否则您必须接受您正在下载所有订阅源url,然后你可以在你自己的代码中过滤掉它们。一旦这个问题符合条件,我会给它50分。除非提要url提供过滤选项,否则你必须接受你正在下载所有提要的url,然后你可以在你自己的代码中过滤掉它们。我收到一个解析错误:语法错误,意外['…在线if(实时)($item…但似乎找不到错误,可以吗?您使用的是什么版本的PHP?我使用了一个5.4功能,允许您访问方法返回的特定索引,但是如果您使用的是5.3,这将不起作用-有了siad,5.3不再受支持,因此如果是这种情况,请升级!!将pubDate替换为lastBuildDate,如图所示在外部url@FahedAlkaabi-你为什么要这样做??lastBuildDate适用于整个提要,但OP希望在最后一个小时内发布的特定项目,跳过较旧的项目-lastBuildDate不适用于每个项目级别。很抱歉,我是个白痴,我运行的是5.3--我现在正在PHP5.5上测试它并重新测试在同一行上接收到以下错误:致命错误:无法将DOMNodeList类型的对象用作arrayI,我收到一个分析错误:语法错误,在if(strtotime)行上意外出现“[”($item…但似乎找不到错误,可以吗?您使用的是什么版本的PHP?我使用了一个5.4功能,允许您访问方法返回的特定索引,但是如果您使用的是5.3,这将不起作用-有了siad,5.3不再受支持,因此如果是这种情况,请升级!!将pubDate替换为lastBuildDate,如图所示在外部url@FahedAlkaabi-你为什么要这样做??lastBuildDate适用于整个提要,但OP希望在最后一个小时内发布的特定项目,跳过较旧的项目-lastBuildDate不适用于每个项目级别。很抱歉,我是个白痴,我运行的是5.3--我现在正在PHP5.5上测试它并重新测试在同一行上接收到以下错误:致命错误:无法将DOMNodeList类型的对象用作arrayThank,尽管此方法是否比发布的另一种方法(在性能方面)更好?确保项目按pubDate排序,如果不按pubDate排序,请删除break;@henrikpeterson-DOMDocument和simpleXML下面使用相同的解析器,因此性能差异很小-请参阅将simpleXML对象转换为数组可能会稍微慢一点,但如果您知道RSS提要项目,Fahed在这方面做得很好是按日期顺序的,一旦超过1小时,您就可以中断循环,以避免检查其余项目。@HenrikPetterson-这两种方法在性能上都很好,但DOMDocument确实读取所有内容并循环很少时间以获得结果,simpleXML也很少循环,但您可以像我在中所做的那样控制它break@HenrikPetterson-我做了e fix if(strotime($item['pubDate']>=strotime(“-1小时”))感谢您的回答,尽管这种方法比发布的另一种方法(在性能方面)好吗?确保项目按pubDate排序,如果不按pubDate排序,请删除break;@henrikpeterson-DOMDocument和simpleXML下面使用相同的解析器,因此性能差异很小-请参阅将simpleXML对象转换为数组可能会稍微慢一点,但Fahed是一个不错的选择