Build Your Own News Aggregator with Twitter Bootstrap and SimplePie

twitter-bootstrap-simple-pie

Wouldn’t it be cool to use two great technologies to create your very own news aggregating website? Well surely it would, and that is exactly what we’re going to do. Let’s grab ourselves a copy of the latest version of Twitter Bootstrap and SimplePie and get cookin!

The tools you will need

http://simplepie.org/

SimplePie is a PHP Library that is fast and easy to use for parsing syndication rss feeds. We’ll use this as the engine that will grab data for our news aggregator. SimplePie will handle the heavy lifting for us on the backend.

http://getbootstrap.com/

Up front we’ll use Twitter Bootstrap 3 to give us a quick and easy way to apply a nice and professional looking front end to our site.

http://bootswatch.com/

This step is optional, but if you would prefer a different style to your design, while still using the same markup and syntax, you could choose one of the many beautiful designs from Bootswatch.

We’re going to keep this as simple as we possibly can. In fact, this site will only have one index.php page consisting of 328 lines of mixed HTML and PHP, and a handful of supporting files to be included via PHP for SimplePie and Twitter Bootstrap. This is just a quick and fun example of using these two very cool tools to build something fun.

<!doctype html>
<html>
    <head>
        <link rel="stylesheet" href="bootstrap.min.css">
        <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
        <script src="bootstrap.min.js"></script>
    </head>
    <?php
    // turn off errors
    error_reporting(0); 
    
    // feeds for our example, choose your favorites
    $one = 'http://techcrunch.com/feed/';
    $two = 'http://www.theverge.com/rss/index.xml';
    $three = 'http://allthingsd.com/feed/';
    $four = 'http://thenextweb.com/feed/'; 
    
    // Include SimplePie
    include_once('autoloader.php');
    include_once('idn/idna_convert.class.php');
    
    // Create a new instance of the SimplePie object
    $feed = new SimplePie();
    
    // Set feed url
    $feed->set_feed_url($one);
    
    // Trigger force-feed
    $feed->force_feed(true);
    
    // Initialize the whole SimplePie object.  Read the feed, process it, parse it, cache it, and
    // all that other good stuff.  The feed's information will not be available to SimplePie before
    // this is called.
    $success = $feed->init();
    
    // We'll make sure that the right content type and character encoding gets set automatically.
    // This function will grab the proper character encoding, as well as set the content type to text/html.
    $feed->handle_content_type();
    ?>
    
    <body>
        <div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
            <div class="container">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button>
                    <p class="navbar-brand">SimpleBoot News</p>
                </div>
                <div class="navbar-collapse collapse"> </div>
                <!--/.navbar-collapse --> 
            </div>
        </div>
        
        <!-- Main jumbotron for a primary marketing message or call to action -->
        <?php $item = $feed->get_item(0); ?>
        <div class="jumbotron">
            <div class="container">
                <h1>
                    <?php if ($item->get_permalink()) echo $item->get_title(); ?>
                </h1>
                <small><?php echo $item->get_date('j M Y, g:i a'); ?></small>
                <p><?php echo '<img src="'.$feed->get_favicon().'">'; ?> <?php $d = strip_tags($item->get_description()); echo substr($d, 0, 300);?></p>
                <p><a <?php echo 'href="' . $item->get_permalink() . '"'; ?> class="btn btn-primary btn-lg" role="button"><span class="glyphicon glyphicon-link"></span> <span class="badge">Learn more »</span></a></p>
            </div>
        </div>
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <div class="bs-example">
                        <div id="accordion" class="panel-group">
                            <div class="panel panel-default">
                                <div class="panel-heading">
                                    <?php $item = $feed->get_item(1); ?>
                                    <h4 class="panel-title"> <a href="#collapseOne" data-parent="#accordion" data-toggle="collapse" class="collapsed"><strong>
                                        <?php if ($item->get_permalink()) echo $item->get_title(); ?>
                                        </strong> </a><small><?php echo $item->get_date('j M Y, g:i a'); ?></small></h4>
                                </div>
                                <div class="panel-collapse collapse" id="collapseOne" style="height: 0px;">
                                    <div class="panel-body"> <?php echo '<img src="'.$feed->get_favicon().'">'; ?> <?php $d = strip_tags($item->get_description()); echo substr($d, 0, 300);?>  <a <?php echo 'href="' . $item->get_permalink() . '"'; ?>><span class="glyphicon glyphicon-link"></span></a></div>
                                </div>
                            </div>
                            <div class="panel panel-default">
                                <div class="panel-heading">
                                    <?php $item = $feed->get_item(2); ?>
                                    <h4 class="panel-title"> <a href="#collapseTwo" data-parent="#accordion" data-toggle="collapse" class="collapsed"><strong>
                                        <?php if ($item->get_permalink()) echo $item->get_title(); ?>
                                        </strong> </a><small><?php echo $item->get_date('j M Y, g:i a'); ?></small> </h4>
                                </div>
                                <div class="panel-collapse collapse" id="collapseTwo" style="height: 0px;">
                                    <div class="panel-body"> <?php echo '<img src="'.$feed->get_favicon().'">'; ?> <?php $d = strip_tags($item->get_description()); echo substr($d, 0, 300);?>  <a <?php echo 'href="' . $item->get_permalink() . '"'; ?>><span class="glyphicon glyphicon-link"></span></a></div>
                                </div>
                            </div>
                            <div class="panel panel-default">
                                <div class="panel-heading">
                                    <?php $item = $feed->get_item(3); ?>
                                    <h4 class="panel-title"> <a href="#collapseThree" data-parent="#accordion" data-toggle="collapse" class="collapsed"><strong>
                                        <?php if ($item->get_permalink()) echo $item->get_title(); ?>
                                        </strong> </a><small><?php echo $item->get_date('j M Y, g:i a'); ?></small> </h4>
                                </div>
                                <div class="panel-collapse collapse" id="collapseThree" style="height: 0px;">
                                    <div class="panel-body"> <?php echo '<img src="'.$feed->get_favicon().'">'; ?> <?php $d = strip_tags($item->get_description()); echo substr($d, 0, 300);?>  <a <?php echo 'href="' . $item->get_permalink() . '"'; ?>><span class="glyphicon glyphicon-link"></span></a></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <p> </p>
        <div class="container"> 
            <!-- Example row of columns -->
            <div class="row">
                <div class="col-md-4">
                <?php
                // Create a new instance of the SimplePie object
                $feed = new SimplePie();
                // Set feed url
                $feed->set_feed_url($two);
                // Trigger force-feed
                $feed->force_feed(true);
                // Initialize the whole SimplePie object.  Read the feed, process it, parse it, cache it, and
                // all that other good stuff.  The feed's information will not be available to SimplePie before
                // this is called.
                $success = $feed->init();
                // We'll make sure that the right content type and character encoding gets set automatically.
                // This function will grab the proper character encoding, as well as set the content type to text/html.
                $feed->handle_content_type();
                ?>
                    <div class="panel panel-success">
                        <?php $item = $feed->get_item(0); ?>
                        <div class="panel-heading">
                            <h3 class="panel-title"><strong>
                                <?php if ($item->get_permalink()) echo $item->get_title(); ?>
                                </strong></h3>
                        </div>
                        <div class="panel-body"><?php echo '<img src="'.$feed->get_favicon().'">'; ?> <?php $d = strip_tags($item->get_description()); echo substr($d, 0, 300);?>
                            <p><a <?php echo 'href="' . $item->get_permalink() . '"'; ?> class="btn btn-default pull-right" href="#" role="button"><span class="glyphicon glyphicon-link"></span> Read More »</a></p>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="bs-example1">
                                    <div id="accordion1" class="panel-group">
                                        <div class="panel panel-default">
                                            <div class="panel-heading">
                                                <?php $item = $feed->get_item(1); ?>
                                                <h4 class="panel-title"> <a href="#collapseOne1" data-parent="#accordion1" data-toggle="collapse" class="collapsed"><strong>
                                                    <?php if ($item->get_permalink()) echo $item->get_title(); ?>
                                                    </strong> </a><small><?php echo $item->get_date('j M Y, g:i a'); ?></small> </h4>
                                            </div>
                                            <div class="panel-default" id="collapseOne1" style="height: 0px;">
                                                <div class="panel-body"> <?php echo '<img src="'.$feed->get_favicon().'">'; ?> <?php $d = strip_tags($item->get_description()); echo substr($d, 0, 300);?>  <a <?php echo 'href="' . $item->get_permalink() . '"'; ?>><span class="glyphicon glyphicon-link"></span></a></div>
                                            </div>
                                        </div>
                                        <div class="panel panel-default">
                                            <div class="panel-heading">
                                                <?php $item = $feed->get_item(2); ?>
                                                <h4 class="panel-title"> <a href="#collapseTwo1" data-parent="#accordion1" data-toggle="collapse" class="collapsed"><strong>
                                                    <?php if ($item->get_permalink()) echo $item->get_title(); ?>
                                                    </strong> </a><small><?php echo $item->get_date('j M Y, g:i a'); ?></small> </h4>
                                            </div>
                                            <div class="panel-collapse collapse" id="collapseTwo1" style="height: 0px;">
                                                <div class="panel-body"> <?php echo '<img src="'.$feed->get_favicon().'">'; ?> <?php $d = strip_tags($item->get_description()); echo substr($d, 0, 300);?>  <a <?php echo 'href="' . $item->get_permalink() . '"'; ?>><span class="glyphicon glyphicon-link"></span></a></div>
                                            </div>
                                        </div>
                                        <div class="panel panel-default">
                                            <div class="panel-heading">
                                                <?php $item = $feed->get_item(3); ?>
                                                <h4 class="panel-title"> <a href="#collapseThree1" data-parent="#accordion1" data-toggle="collapse" class="collapsed"><strong>
                                                    <?php if ($item->get_permalink()) echo $item->get_title(); ?>
                                                    </strong> </a><small><?php echo $item->get_date('j M Y, g:i a'); ?></small> </h4>
                                            </div>
                                            <div class="panel-collapse collapse" id="collapseThree1" style="height: 0px;">
                                                <div class="panel-body"> <?php echo '<img src="'.$feed->get_favicon().'">'; ?> <?php $d = strip_tags($item->get_description()); echo substr($d, 0, 300);?>  <a <?php echo 'href="' . $item->get_permalink() . '"'; ?>><span class="glyphicon glyphicon-link"></span></a></div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                <?php
                // Create a new instance of the SimplePie object
                $feed = new SimplePie();
                // Set feed url
                $feed->set_feed_url($three);
                // Trigger force-feed
                $feed->force_feed(true);
                // Initialize the whole SimplePie object.  Read the feed, process it, parse it, cache it, and
                // all that other good stuff.  The feed's information will not be available to SimplePie before
                // this is called.
                $success = $feed->init();
                // We'll make sure that the right content type and character encoding gets set automatically.
                // This function will grab the proper character encoding, as well as set the content type to text/html.
                $feed->handle_content_type();
                ?>
                    <div class="panel panel-info">
                        <?php $item = $feed->get_item(0); ?>
                        <div class="panel-heading">
                            <h3 class="panel-title"><strong>
                                <?php if ($item->get_permalink()) echo $item->get_title(); ?>
                                </strong></h3>
                        </div>
                        <div class="panel-body"><?php echo '<img src="'.$feed->get_favicon().'">'; ?> <?php $d = strip_tags($item->get_description()); echo substr($d, 0, 300);?>
                            <p><a <?php echo 'href="' . $item->get_permalink() . '"'; ?> class="btn btn-default pull-right" href="#" role="button"><span class="glyphicon glyphicon-link"></span> Read More »</a></p>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="bs-example2">
                                    <div id="accordion2" class="panel-group">
                                        <div class="panel panel-default">
                                            <div class="panel-heading">
                                                <?php $item = $feed->get_item(1); ?>
                                                <h4 class="panel-title"> <a href="#collapseOne2" data-parent="#accordion2" data-toggle="collapse" class="collapsed"><strong>
                                                    <?php if ($item->get_permalink()) echo $item->get_title(); ?>
                                                    </strong> </a><small><?php echo $item->get_date('j M Y, g:i a'); ?></small> </h4>
                                            </div>
                                            <div class="panel-collapse collapse" id="collapseOne2" style="height: 0px;">
                                                <div class="panel-body"> <?php echo '<img src="'.$feed->get_favicon().'">'; ?> <?php $d = strip_tags($item->get_description()); echo substr($d, 0, 300);?>  <a <?php echo 'href="' . $item->get_permalink() . '"'; ?>><span class="glyphicon glyphicon-link"></span></a></div>
                                            </div>
                                        </div>
                                        <div class="panel panel-default">
                                            <div class="panel-heading">
                                                <?php $item = $feed->get_item(2); ?>
                                                <h4 class="panel-title"> <a href="#collapseTwo2" data-parent="#accordion2" data-toggle="collapse" class="collapsed"><strong>
                                                    <?php if ($item->get_permalink()) echo $item->get_title(); ?>
                                                    </strong> </a><small><?php echo $item->get_date('j M Y, g:i a'); ?></small> </h4>
                                            </div>
                                            <div class="panel-collapse collapse" id="collapseTwo2" style="height: 0px;">
                                                <div class="panel-body"> <?php echo '<img src="'.$feed->get_favicon().'">'; ?> <?php $d = strip_tags($item->get_description()); echo substr($d, 0, 300);?>  <a <?php echo 'href="' . $item->get_permalink() . '"'; ?>><span class="glyphicon glyphicon-link"></span></a></div>
                                            </div>
                                        </div>
                                        <div class="panel panel-default">
                                            <div class="panel-heading">
                                                <?php $item = $feed->get_item(3); ?>
                                                <h4 class="panel-title"> <a href="#collapseThree2" data-parent="#accordion2" data-toggle="collapse" class="collapsed"><strong>
                                                    <?php if ($item->get_permalink()) echo $item->get_title(); ?>
                                                    </strong> </a><small><?php echo $item->get_date('j M Y, g:i a'); ?></small> </h4>
                                            </div>
                                            <div class="panel-collapse collapse" id="collapseThree2" style="height: 0px;">
                                                <div class="panel-body"> <?php echo '<img src="'.$feed->get_favicon().'">'; ?> <?php $d = strip_tags($item->get_description()); echo substr($d, 0, 300);?>  <a <?php echo 'href="' . $item->get_permalink() . '"'; ?>><span class="glyphicon glyphicon-link"></span></a></div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                <?php
                // Create a new instance of the SimplePie object
                $feed = new SimplePie();
                // Set feed url
                $feed->set_feed_url($four);
                // Trigger force-feed
                $feed->force_feed(true);
                // Initialize the whole SimplePie object.  Read the feed, process it, parse it, cache it, and
                // all that other good stuff.  The feed's information will not be available to SimplePie before
                // this is called.
                $success = $feed->init();
                // We'll make sure that the right content type and character encoding gets set automatically.
                // This function will grab the proper character encoding, as well as set the content type to text/html.
                $feed->handle_content_type();
                ?>
                    <div class="panel panel-warning">
                        <?php $item = $feed->get_item(0); ?>
                        <div class="panel-heading">
                            <h3 class="panel-title"><strong>
                                <?php if ($item->get_permalink()) echo $item->get_title(); ?>
                                </strong></h3>
                        </div>
                        <div class="panel-body"><?php echo '<img src="'.$feed->get_favicon().'">'; ?> <?php $d = strip_tags($item->get_description()); echo substr($d, 0, 300);?>
                            <p><a <?php echo 'href="' . $item->get_permalink() . '"'; ?> class="btn btn-default pull-right" href="#" role="button"><span class="glyphicon glyphicon-link"></span> Read More »</a></p>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="bs-example3">
                                    <div id="accordion3" class="panel-group">
                                        <div class="panel panel-default">
                                            <div class="panel-heading">
                                                <?php $item = $feed->get_item(1); ?>
                                                <h4 class="panel-title"> <a href="#collapseOne3" data-parent="#accordion3" data-toggle="collapse" class="collapsed"><strong>
                                                    <?php if ($item->get_permalink()) echo $item->get_title(); ?>
                                                    </strong> </a><small><?php echo $item->get_date('j M Y, g:i a'); ?></small> </h4>
                                            </div>
                                            <div class="panel-collapse collapse" id="collapseOne3" style="height: 0px;">
                                                <div class="panel-body"> <?php echo '<img src="'.$feed->get_favicon().'">'; ?> <?php $d = strip_tags($item->get_description()); echo substr($d, 0, 300);?>  <a <?php echo 'href="' . $item->get_permalink() . '"'; ?>><span class="glyphicon glyphicon-link"></span></a></div>
                                            </div>
                                        </div>
                                        <div class="panel panel-default">
                                            <div class="panel-heading">
                                                <?php $item = $feed->get_item(2); ?>
                                                <h4 class="panel-title"> <a href="#collapseTwo3" data-parent="#accordion3" data-toggle="collapse" class="collapsed"><strong>
                                                    <?php if ($item->get_permalink()) echo $item->get_title(); ?>
                                                    </strong> </a><small><?php echo $item->get_date('j M Y, g:i a'); ?></small> </h4>
                                            </div>
                                            <div class="panel-collapse collapse" id="collapseTwo3" style="height: 0px;">
                                                <div class="panel-body"> <?php echo '<img src="'.$feed->get_favicon().'">'; ?> <?php $d = strip_tags($item->get_description()); echo substr($d, 0, 300);?>  <a <?php echo 'href="' . $item->get_permalink() . '"'; ?>><span class="glyphicon glyphicon-link"></span></a></div>
                                            </div>
                                        </div>
                                        <div class="panel panel-default">
                                            <div class="panel-heading">
                                                <?php $item = $feed->get_item(3); ?>
                                                <h4 class="panel-title"> <a href="#collapseThree3" data-parent="#accordion3" data-toggle="collapse" class="collapsed"><strong>
                                                    <?php if ($item->get_permalink()) echo $item->get_title(); ?>
                                                    </strong> </a><small><?php echo $item->get_date('j M Y, g:i a'); ?></small> </h4>
                                            </div>
                                            <div class="panel-collapse collapse" id="collapseThree3" style="height: 0px;">
                                                <div class="panel-body"> <?php echo '<img src="'.$feed->get_favicon().'">'; ?> <?php $d = strip_tags($item->get_description()); echo substr($d, 0, 300);?>  <a <?php echo 'href="' . $item->get_permalink() . '"'; ?>><span class="glyphicon glyphicon-link"></span></a></div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="container">       
            <footer>
                <p>© A Vegibit Production 2013</p>
            </footer>
        </div>
    </div>
</body>
</html>

Supporting Files and Folders

The above index.php file is meant to go in your root directory. The supporting files you need in the root directory are autoloader.php, bootstrap.min.css, and bootstrap.min.js. I told you this would be simple 🙂

There are two supporting folders; cache, and library. The cache folder contains the cached entries of the data you collect with SimplePie and the library folder contains the SimplePie.php class and the remaining SimplePie class files in a subfolder called SimplePie. Once all of this is in place, you are ready to launch your site! The picture at the beginning of this post gives an idea of what it will look like.

The PHP that makes it work

Lastly we’ll provide the actual methods used in the SimplePie object to get the data of each post you display. This will help to make sense of what is going on in the provided index.php file above.

// Create a new instance of the SimplePie object
$feed = new SimplePie();   
// Set feed url
$feed->set_feed_url($one);   
// Trigger force-feed
$feed->force_feed(true);  
// Initialize the whole SimplePie object.  Read the feed, process it, parse it, cache it, and
// all that other good stuff.  The feed's information will not be available to SimplePie before
// this is called.
$success = $feed->init();  
// We'll make sure that the right content type and character encoding gets set automatically.
// This function will grab the proper character encoding, as well as set the content type to text/html.
$feed->handle_content_type();
// grab the first item in the feed, use 1 or 2, etc for the other posts
$item = $feed->get_item(0);
// the link back to the source
$item->get_permalink();
// the title of the article
$item->get_title(); 
// the date formatted nicely
$item->get_date('j M Y, g:i a'); 
// the favicon of the source
$feed->get_favicon();
// sanitize the post content and put it in a variable
$d = strip_tags($item->get_description()); 
// echo out the first 300 characters as a sumamary
echo substr($d, 0, 300);

Thank you for reading Build Your Own News Aggregator with Twitter Bootstrap and SimplePie – If you found this post helpful, Please do share using the buttons below!