Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Include the SimplePie library, I also use one method from the newsblocks demo to format item descriptions nicely
- require_once('./php/simplepie.inc');
- require_once('./php/newsblocks.inc.php');
- // Try setting header here
- header('Content-type:text/xml; charset=utf-8');
- // This text file contains a list of valid feed URIs one per line.
- $feeds = file('allstarFeeds.txt');
- // This array will hold the items we'll be grabbing.
- $first_items = array();
- // The resulting feed validates use http://feedvalidator.org/ if in doubt
- // Let's go through the array, feed by feed, and store the items we want.
- foreach ($feeds as $url)
- {
- $feed = new SimplePie();
- $feed->set_feed_url($url);
- // Tempted to force feed and see if that returns more data from some of the feeds...
- // Problem in default blogspot feeds isn't going away, just removed them from list of feeds to merge
- $feed->init();
- // How many items per feed should we try to grab?
- $items_per_feed = 1;
- // 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.
- for ($x = 0; $x < $feed->get_item_quantity($items_per_feed); $x++)
- {
- $first_items[] = $feed->get_item($x);
- }
- // We're done with this feed, so let's release some memory.
- unset($feed);
- }
- // 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.
- function sort_items($a, $b)
- {
- return SimplePie::sort_items($a, $b);
- }
- // Now we can sort $first_items with our custom sorting function.
- usort($first_items, "sort_items");
- // Begin the RSS Feed
- echo '<?xml version="1.0" encoding="UTF-8" ?>';
- ?>
- <rss version="0.91"> <!-- No need to use RSS version 2.0 -->
- <channel>
- <title>Merged Feeds</title>
- <link>http://news.muschamp.ca</link>
- <description>
- Merged feeds into a single feed, with one item from each feed, sorted by publish date.
- </description>
- <language>en-us</language>
- <pubDate><?php echo date("D, d M Y H:i:s T"); ?></pubDate>
- <?php
- foreach($first_items as $item)
- {
- $feed = $item->get_feed();
- $feed->xml_dump = true;
- // Added newline characters to make it easier to debug
- print('<item>' . "\n");
- print('<title>' . $feed->get_title() . ' : ' . preg_replace("/&#?[a-z0-9]{2,8};/i", "", $item->get_title()) . '</title>' . "\n");
- print('<link>' . $item->get_permalink() . '</link>' . "\n");
- // 16 corresponds to ENT_XML1
- // The next line functions fine in my experience but perhaps there is a better way to ensure the feed validates
- print('<description>' . htmlspecialchars(newsblocks::cleanup($item->get_description(), 400), 16, "UTF-8") . '</description>'. "\n");
- print('<pubDate>' . $item->get_date("D, d M Y H:i:s T") . '</pubDate>' . "\n");
- print('</item>'. "\n");
- }
- ?>
- </channel>
- </rss>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement