DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Create Simple News Aggregator With SimplePie

04.17.2011
| 3392 views |
  • submit to reddit
        Parse multiple feeds from different sources into separate pages using SimplePie class. A fast and efficient way to serve fresh content to your site as to provides the most up-to-date information to the readers.

<?php
/**************************************************************************
To improve the script functionality, you might 
need to be familiar with using SimplePie.

Requirements: http://simplepie.org/wiki/setup/requirements

Documentation: http://simplepie.org/wiki/

Copyright (c) 2010, recentfootballnews.com
All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions are met: 

 * Redistributions of source code must retain the above copyright notice, 
   this list of conditions and the following disclaimer. 
 * Redistributions in binary form must reproduce the above copyright 
   notice, this list of conditions and the following disclaimer in the 
   documentation and/or other materials provided with the distribution. 
 * Neither the name of Recent Football News nor the names of its 
   contributors may be used to endorse or promote products derived from 
   this software without specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
***************************************************************************/
require 'simplepie.inc';
$feed = new SimplePie();
$feed->set_feed_url(array('http://feeds.feedburner.com/recentfootballnews',
			  'http://www.fifa.com/rss/index.xml')); // Add your favorites RSS feeds
$success = $feed->init();
$feed->handle_content_type();
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}
$limit = 10; // Configure how many items to display per page
$start = (($page * $limit) );
$end = (($page * $limit) + $limit);
?><html>
<head>
<title>Recent Football News</title>
</head>
<body>
<?php if($success): ?>
<?php
$new = array();
$new2 = array();
foreach($feed->get_items() as $item)
{
$yesterday = time() - (24*60*60);
if($item->get_date('U') > $yesterday)
{
$new[] = $item;
}
}
$max = ceil(count($new) / $limit);
foreach($new AS $key => $val)
{
if($key >= $start && $key < $end)
$new2[] = $new[$key];
}
foreach($new2 as $item): ?>
<div>
<h2><?php echo $item->get_title(); ?></h2>
<p><small><?php echo $item->get_date('r'); ?></small></p>
<p><?php echo $item->get_description(); ?></p>
<p><a href='<?php echo $item->get_permalink(); ?>' target='_blank'>Read more →</a></p>
</div>
<?php endforeach; ?>
<?php endif; ?>
<div>
<?php
$max = $max -1;
if($page > 1)
{
$back = ($page - 1);
echo "<a href='?page=$back'>Back</a>";
}
for($i = 1; $i <= $max; $i++){
if(($page) == $i){
echo "<span class='current'>$i</span>";
} else {
echo "<a href='?page=$i'>$i</a>";
}
}
if($page < $max){
$next = ($page + 1);
echo "<a href='?page=$next'>Next</a>";
}
?> 
</div>
</body>
</html>