Advertisement
Muskie

Merging Feeds into a single RSS Feed

Apr 27th, 2012
560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.89 KB | None | 0 0
  1. <?php
  2. // Include the SimplePie library, I also use one method from the newsblocks demo to format item descriptions nicely
  3. require_once('./php/simplepie.inc');
  4. require_once('./php/newsblocks.inc.php');
  5.  
  6. // Try setting header here
  7. header('Content-type:text/xml; charset=utf-8');
  8.  
  9. // This text file contains a list of valid feed URIs one per line.
  10. $feeds = file('allstarFeeds.txt');
  11.  
  12. // This array will hold the items we'll be grabbing.
  13. $first_items = array();
  14.  
  15. // The resulting feed validates use http://feedvalidator.org/ if in doubt
  16.  
  17. // Let's go through the array, feed by feed, and store the items we want.
  18. foreach ($feeds as $url)
  19. {
  20.     $feed = new SimplePie();
  21.     $feed->set_feed_url($url);
  22.     // Tempted to force feed and see if that returns more data from some of the feeds...
  23.     // Problem in default blogspot feeds isn't going away, just removed them from list of feeds to merge
  24.     $feed->init();
  25.  
  26.     // How many items per feed should we try to grab?
  27.     $items_per_feed = 1;
  28.  
  29.     // As long as we're not trying to grab more items than the feed has, go through them one by one and add them to the array.
  30.     for ($x = 0; $x < $feed->get_item_quantity($items_per_feed); $x++)
  31.     {
  32.         $first_items[] = $feed->get_item($x);
  33.     }
  34.  
  35.     // We're done with this feed, so let's release some memory.
  36.     unset($feed);
  37. }
  38.  
  39. // We need to sort the items by date with a user-defined sorting function.  Since usort() won't accept "SimplePie::sort_items", we need to wrap it in a new function.
  40. function sort_items($a, $b)
  41. {
  42.     return SimplePie::sort_items($a, $b);
  43. }
  44.  
  45. // Now we can sort $first_items with our custom sorting function.
  46. usort($first_items, "sort_items");
  47.  
  48.  
  49. // Begin the RSS Feed
  50. echo '<?xml version="1.0" encoding="UTF-8" ?>';
  51. ?>
  52.  
  53. <rss version="0.91"> <!-- No need to use RSS version 2.0 -->
  54. <channel>
  55. <title>Merged Feeds</title>
  56. <link>http://news.muschamp.ca</link>
  57. <description>
  58. Merged feeds into a single feed, with one item from each feed, sorted by publish date.
  59. </description>
  60. <language>en-us</language>
  61. <pubDate><?php echo date("D, d M Y H:i:s T"); ?></pubDate>
  62.  
  63.     <?php
  64.     foreach($first_items as $item)
  65.     {
  66.         $feed = $item->get_feed();
  67.         $feed->xml_dump = true;
  68.        
  69.         // Added newline characters to make it easier to debug
  70.         print('<item>' . "\n");
  71.                 print('<title>' . $feed->get_title() . ' : ' . preg_replace("/&#?[a-z0-9]{2,8};/i", "", $item->get_title()) . '</title>' . "\n");
  72.         print('<link>' . $item->get_permalink() . '</link>' . "\n");
  73.         // 16 corresponds to ENT_XML1
  74.         // The next line functions fine in my experience but perhaps there is a better way to ensure the feed validates
  75.         print('<description>' . htmlspecialchars(newsblocks::cleanup($item->get_description(), 400), 16, "UTF-8") . '</description>'. "\n");
  76.         print('<pubDate>' . $item->get_date("D, d M Y H:i:s T") . '</pubDate>' . "\n");
  77.         print('</item>'. "\n");
  78.     }
  79.     ?>
  80.    
  81. </channel>
  82. </rss>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement