rss:combine_multiple_rss_feeds
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
rss:combine_multiple_rss_feeds [2016/10/09 13:28] – created peter | rss:combine_multiple_rss_feeds [2020/07/15 09:30] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 88: | Line 88: | ||
$xml = NULL; | $xml = NULL; | ||
} | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ===== Another way ===== | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | class Feed_Amalgamator | ||
+ | { | ||
+ | public $urls = array(); | ||
+ | public $data = array(); | ||
+ | |||
+ | public function addFeeds( array $feeds ) | ||
+ | { | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | public function grabRss() | ||
+ | { | ||
+ | foreach ( $this-> | ||
+ | { | ||
+ | $data = @new SimpleXMLElement( $feed, 0, true ); | ||
+ | if ( !$data ) | ||
+ | throw new Exception( 'Could not load: ' . $feed ); | ||
+ | foreach ( $data-> | ||
+ | { | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public function amalgamate() | ||
+ | { | ||
+ | shuffle( $this-> | ||
+ | $temp = array(); | ||
+ | foreach ( $this-> | ||
+ | { | ||
+ | if ( !in_array($item-> | ||
+ | { | ||
+ | $temp[] = $item; | ||
+ | } | ||
+ | } | ||
+ | $this-> | ||
+ | shuffle( $this-> | ||
+ | } | ||
+ | |||
+ | private function links( array $items ) | ||
+ | { | ||
+ | $links = array(); | ||
+ | foreach ( $items as $item ) | ||
+ | { | ||
+ | $links[] = $item-> | ||
+ | } | ||
+ | return $links; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /********* Example *********/ | ||
+ | |||
+ | $urls = array( ' | ||
+ | |||
+ | try | ||
+ | { | ||
+ | $feeds = new Feed_Amalgamator; | ||
+ | $feeds-> | ||
+ | $feeds-> | ||
+ | $feeds-> | ||
+ | } | ||
+ | catch ( exception $e ) | ||
+ | { | ||
+ | die( $e-> | ||
+ | } | ||
+ | |||
+ | foreach ( $feeds-> | ||
+ | extract( (array) $item ); | ||
+ | ?> | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | <?php endforeach; ?> | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Another Way ===== | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | |||
+ | // Set the feed URLs here | ||
+ | $feeds = array( | ||
+ | //' | ||
+ | //' | ||
+ | |||
+ | ' | ||
+ | ' | ||
+ | // etc. | ||
+ | ); | ||
+ | |||
+ | |||
+ | // Get all feed entries | ||
+ | $entries = array(); | ||
+ | foreach ($feeds as $feed) { | ||
+ | $xml = simplexml_load_file($feed); | ||
+ | $entries = array_merge($entries, | ||
+ | } | ||
+ | |||
+ | // Sort feed entries by pubDate (ascending) | ||
+ | usort($entries, | ||
+ | //return strtotime($x-> | ||
+ | return strtotime($y-> | ||
+ | }); | ||
+ | |||
+ | print_r($entries); | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Another way ===== | ||
+ | |||
+ | With 4 lines, I import a rss to an array. | ||
+ | |||
+ | <code php> | ||
+ | $feed = implode(file(' | ||
+ | $xml = simplexml_load_string($feed); | ||
+ | $json = json_encode($xml); | ||
+ | $array = json_decode($json, | ||
+ | </ | ||
+ | |||
+ | Try also changing 1st line for one of these... | ||
+ | |||
+ | <code php> | ||
+ | $feed = file_get_contents(' | ||
+ | $feed = json_decode(json_encode(simplexml_load_file(' | ||
+ | </ | ||
+ | |||
+ | |||
+ | For a more complex solution | ||
+ | |||
+ | <code php> | ||
+ | $feed = new DOMDocument(); | ||
+ | | ||
+ | $json = array(); | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | $i = 0; | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | $guid = $item-> | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | echo json_encode($json); | ||
</ | </ |
rss/combine_multiple_rss_feeds.1476019689.txt.gz · Last modified: 2020/07/15 09:30 (external edit)