1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

merging multiple RSS feeds into one with PHP

Discussion in 'PHP' started by marcnyc, Jan 18, 2009.

  1. #1
    is this possible?
    I've been looking around and googling but can't find any scripts to do this...
    do you guys know of any?
    Thanks
    SEMrush
     
    marcnyc, Jan 18, 2009 IP
    SEMrush
  2. NinjaWork

    NinjaWork Guest

    Messages:
    132
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi MarcNYC,

    I was just reading this thread about using Yahoo Pipes with multiple feeds:
    http://www.webmasterworld.com/rss_atom/3809232.htm

    I'm sure you could do it in PHP...the trick is to grab all the feeds and put them in a data stucture that you can manipulate the way you want.

    A quick and dirty approach would be using regular expressions. Or you could use a DOM XML parser or some function designed to parse RSS.
     
    NinjaWork, Jan 18, 2009 IP
  3. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #3
    <?php
    
    class Feed_Amalgamator
    {
        public $urls = array();
        public $data = array();
    
        public function addFeeds( array $feeds )
        {
            $this->urls = array_merge( $this->urls, array_values($feeds) );
        }
    
        public function grabRss()
        {
            foreach ( $this->urls as $feed )
            {
                $data = @new SimpleXMLElement( $feed, 0, true );
                if ( !$data )
                    throw new Exception( 'Could not load: ' . $feed );
                foreach ( $data->channel->item as $item )
                {
                    $this->data[] = $item;
                }
            }
        }
    
        public function amalgamate()
        {
            shuffle( $this->data );
            $temp = array();
            foreach ( $this->data as $item )
            {
                if ( !in_array($item->link, $this->links($temp)) )
                {
                    $temp[] = $item;
                }
            }
            $this->data = $temp;
            shuffle( $this->data );
        }
    
        private function links( array $items )
        {
            $links = array();
            foreach ( $items as $item )
            {
                $links[] = $item->link;
            }
            return $links;
        }
    }
    
    /********* Example *********/
    
    $urls = array( 'http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/football/teams/m/man_city/rss.xml', 'http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/football/teams/l/liverpool/rss.xml' );
    
    try
    {
        $feeds = new Feed_Amalgamator;
        $feeds->addFeeds( $urls );
        $feeds->grabRss();
        $feeds->amalgamate();
    }
    catch ( exception $e )
    {
        die( $e->getMessage() );
    }
    
    foreach ( $feeds->data as $item ) :
    extract( (array) $item );
    ?>
    <h2><a href="<?php echo $link; ?>"><?php echo $title; ?></a></h2>
    <p><?php echo $description; ?></p>
    <p><em><?php echo $pubDate; ?></em></p>
    <?php endforeach; ?>
    PHP:
     
    Danltn, Jan 19, 2009 IP
  4. marcnyc

    marcnyc Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks SO MUCH for posting this code man!
    Yesterday late night I actually found (before I read your code) this thing called SimplePie which seems perfect... I haven't been able to do multiple feeds yet so I am messing with that... If i can't get it to do multiple I'll give up and give a try to your code, which seems like it would do the right thing.
    Thanks again for taking the time
     
    marcnyc, Jan 19, 2009 IP
  5. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #5
    I just wrote it for you now, it works for BBC Feeds (and removes link repeats.) but I make no guarantees it'll work for every feed out there, especially malformed ones.

    Dan.
     
    Danltn, Jan 19, 2009 IP
  6. french-webbie

    french-webbie Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    french-webbie, Jan 19, 2009 IP
  7. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #7
    You really didn't read the thread at all did you? :p
     
    Danltn, Jan 19, 2009 IP
  8. marcnyc

    marcnyc Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Wow you wrote this specifically for me? I am touched and feel bad now...
    I am definitely going to give it a try now!

    Thanks so much, even more so than before.



    French-webbie, I had indeed mentioned that I had found SimplePie but have been having trouble with multiple feed... If you have any experience with multi-feed SimplePie I could use your help maybe...
     
    marcnyc, Jan 19, 2009 IP
  9. marcnyc

    marcnyc Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Yes Danltn, French-webbie didn't read every part of the thread but I wouldn't gang up on somebody who pitched in for help, at least he/she took the time and I appreciate that.
    You guys have all been great! Come on now, peace not war! ;-)
     
    marcnyc, Jan 19, 2009 IP
  10. french-webbie

    french-webbie Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Gosh, I should not have so many tabs opened at the same time ;)
     
    french-webbie, Jan 19, 2009 IP
  11. marcnyc

    marcnyc Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    we are all afflicted by that illness I think
    I am for sure
     
    marcnyc, Jan 19, 2009 IP
  12. NinjaWork

    NinjaWork Guest

    Messages:
    132
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    haha...I've done that too :)
     
    NinjaWork, Jan 19, 2009 IP
  13. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #13
    Heh my routine is go to forums, open every unread in a new tab, and either criticise others :)rolleyes:) or add my own replies...

    Some posts, the OP is just so frustrating to work with, I give up and close it.

    Dan.
     
    Danltn, Jan 19, 2009 IP
  14. NinjaWork

    NinjaWork Guest

    Messages:
    132
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    btw Danltn, very nice code :)
     
    NinjaWork, Jan 19, 2009 IP
  15. blogmaster2003

    blogmaster2003 Well-Known Member

    Messages:
    1,652
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    165
    #15
    Will it be difficult to input the feeds from a txt file? I have some 200 rss feeds i need to agregate and i was trying to categorize them, so i have 10 txt files i need to input and i was thinking if i could do that without writing them in the code.
     
    blogmaster2003, Mar 6, 2009 IP
  16. candell

    candell Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Apologies for bringing up an old thread.

    A feed I want to display features a <content:encoded> tag, is there a way to use the script provided by Danltn to also display this?

    Thanks
     
    candell, Aug 24, 2011 IP
  17. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #17
    You might also want to take a look at zRSSFeed zazar.net/developers/zrssfeed/ All it takes is one line on your p a g e [can't imagine why the site doesn't like that word] per feed.
     
    Rukbat, Aug 24, 2011 IP
  18. lanner

    lanner Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #18
    Hello
    Sorry to open this old topic but i need for a website a multiple reader rss , this one works but i need to catch the enclosure , someone can modify this code for the JPG image of enclosure ?
    Thanks :)
     
    lanner, Nov 24, 2013 IP