User Tools

Site Tools


rss:combine_multiple_rss_feeds

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rss:combine_multiple_rss_feeds [2016/10/09 14:31] peterrss:combine_multiple_rss_feeds [2020/07/15 09:30] (current) – external edit 127.0.0.1
Line 169: Line 169:
 <p><em><?php echo $pubDate; ?></em></p> <p><em><?php echo $pubDate; ?></em></p>
 <?php endforeach; ?> <?php endforeach; ?>
 +</code>
 +
 +
 +===== Another Way =====
 +
 +<code php>
 +<?php
 +
 +// Set the feed URLs here
 +$feeds = array(
 +    //'http://www.example.org/feed1.rss',
 +    //'http://www.example.org/feed2.rss',
 +
 +    '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',
 +    // etc.
 +);
 +
 +
 +// Get all feed entries
 +$entries = array();
 +foreach ($feeds as $feed) {
 +    $xml = simplexml_load_file($feed);
 +    $entries = array_merge($entries, $xml->xpath('/rss//item'));
 +}
 +
 +// Sort feed entries by pubDate (ascending)
 +usort($entries, function ($x, $y) {
 +    //return strtotime($x->pubDate) - strtotime($y->pubDate);
 +    return strtotime($y->pubDate) - strtotime($x->pubDate);
 +});
 +
 +print_r($entries);
 +?>
 +</code>
 +
 +
 +===== Another way =====
 +
 +With 4 lines, I import a rss to an array.
 +
 +<code php>
 +$feed = implode(file('http://yourdomains.com/feed.rss'));
 +$xml = simplexml_load_string($feed);
 +$json = json_encode($xml);
 +$array = json_decode($json,TRUE);
 +</code>
 +
 +Try also changing 1st line for one of these...
 +
 +<code php>
 +$feed = file_get_contents('http://yourdomains.com/feed.rss');
 +$feed = json_decode(json_encode(simplexml_load_file('news.google.com‌​/?output=rss')), true); 
 +</code>
 +
 +
 +For a more complex solution
 +
 +<code php>
 +$feed = new DOMDocument();
 + $feed->load('file.rss');
 + $json = array();
 + $json['title'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
 + $json['description'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
 + $json['link'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('link')->item(0)->firstChild->nodeValue;
 + $items = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('item');
 +
 + $json['item'] = array();
 + $i = 0;
 +
 + foreach($items as $key => $item) {
 + $title = $item->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
 + $description = $item->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
 + $pubDate = $item->getElementsByTagName('pubDate')->item(0)->firstChild->nodeValue;
 + $guid = $item->getElementsByTagName('guid')->item(0)->firstChild->nodeValue;
 +
 + $json['item'][$key]['title'] = $title;
 + $json['item'][$key]['description'] = $description;
 + $json['item'][$key]['pubdate'] = $pubDate;
 + $json['item'][$key]['guid'] = $guid; 
 + }
 +
 +echo json_encode($json);
 </code> </code>
rss/combine_multiple_rss_feeds.1476023488.txt.gz · Last modified: 2020/07/15 09:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki