Henrik Petterson
Henrik Petterson

Reputation: 7094

PHP: Get feed content of the past hour

I want to get the content of an external RSS feed and store entries from the last hour only.

So, I can get the RSS feed by:

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

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

Now, $feed contains all the data of the RSS feed. I want to store links to entries posted in the past hour only:

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

How can I do this?

Upvotes: 0

Views: 393

Answers (3)

user2070744
user2070744

Reputation: 11

This is my first post. I hope this help you.

I couldn't fetch the xml with de load function. I think this is more simple.

Sorry for my english.

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

Upvotes: 1

Fahed Alkaabi
Fahed Alkaabi

Reputation: 269

Simple convert xml to array Demo

you can use this code to convert it

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

Upvotes: 1

Liam Wiltshire
Liam Wiltshire

Reputation: 1264

You will need to loop through the feed and check the 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.
    }
}

Upvotes: 2

Related Questions