Search code examples
phprssfeed

Get last urls from rss feeds with PHP


Imaging i have a RSS feed from a website that contains the lastest news of that website,

Like this:

http://example.com/feed

Now, i want to get the last news url from this feed address.

Like this :

http://example.com/post/555.php
http://example.com/post/554.php
http://example.com/post/553.php
http://example.com/post/552.php
http://example.com/post/551.php
http://example.com/post/550.php

How to get list of urls with a limit of (for example) 25 urls In PHP ?


Solution

  • It can be done by using this code :

    $rss = new DOMDocument();
    $rss->load('http://wordpress.org/news/feed/');
    $feed = array();
    
    foreach ($rss->getElementsByTagName('item') as $node) {
        $item = array (
            'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
        );
        array_push($feed, $item);
    }
    
    $limit = 5;
    
    for($x=0;$x<$limit;$x++) {
        echo $link = $feed[$x]['link'];
    }