Go to Content
Columbia College Chicago
Including RSS Feeds
Print this Page Email this Page

Including RSS Feeds

Copy and paste the following code into the "Source" view of your document. Change the values in $feed_locations to the RSS feeds that you wish to include in your pages -- delete the second URL if you only need one feed, or add others as necessary.

Example code for two RSS feeds, displaying 5 items from each feed:

<?php

    // pull in the SimplePie library
    require_once('SimplePie.php');

    // tell the object where to the feeds are
    $feed_locations = array(
        'http://www.colum.edu/Site_Files/scripts/site_rss.php',
        'http://www.flickr.com/photos/tags/columbiacollegechicago'
    );        

    // tell the object how many feed entries you want to get per feed
    $n_individual_feed_items = 5;

    // set the maximum number of items returned. set to false to remove restriction
    $n_total_items = false;

    // initialize the SimplePie object
    $feed = new SimplePie();
    $feed->set_feed_url($feed_locations);
    $feed->set_item_limit($n_individual_feed_items);
    $feed->set_cache_duration(1);
    $feed->set_cache_location("/tmp");
    $feed->init();
    $feed->handle_content_type();
   
    // retrieve the feed items
    $items = $feed->get_items();
   
    $n=1;
   
    // cycle through all the feed items and display them
    foreach ($items as $item) {

        if ($n_total_items && ($n > $n_total_items)) continue;

        $title = $item->get_title();
        $link = $item->get_permalink();
        $posted_date = $item->get_date("l, F j, Y \a\\t h:i a");
        $description = $item->get_description();
        
        // To change the HTML layout of each feed item, edit the following code
        $output .= "<h2>$title</h2>";
        $output .= "<p>$description</p>";
        $output .= "<p>Posted on <a href='$link'>$posted_date</a></p>";
            
        $n++;
        
    }
   
    echo $output;
   
?>