The University of Texas at Austin- What Starts Here Changes the World
Services Navigation
UT Home -> Web Publishing -> RSS Channels

Including RSS Content In Your Pages

Publishers can also include RSS content in their own Web pages. This can eliminate duplication and will also let you capitalize on other University content in your Web page as well. For example, in addition to your own spotlights or news, you might want to include press releases from the Office of Public Affairs, spotlights from the main UT Home page, or external RSS feeds. You can find lists of RSS feeds at several sites:

There are several methods for including RSS content in your Web site; this page outlines two of those methods:

  • Using server side includes (Web Central publishers only)
  • Using a PHP script

The server side includes are easier, but do not provide as much flexibility as PHP.

Using Server Side Includes (Web Central only)

This method illustrates how you can include RSS files that are pre-processed for We Central.

Step 1: Enable Your Directory for Server-Side Includes

Server-side includes allow you to include files in your Web pages when they are requested. For example, you might have a common footer file that you want to appear at the bottom of each page in your site. You can include this file via a server-side include. Edits made to the included file will appear in all of the pages in your site. In the case of channels, you will include the channel in your HTML document, and when the page is requested, the server will insert the contents of the RSS channel file into your page in the location that you specify.

  1. Using a text editor, such as Notepad or BBEdit, create a file named .htaccess. The file should contain the line shown below:
    
    AddHandler server-parsed .html
    
    
  2. This line means that all files ending in .html in your directory and every directory in your site will be parsed by the server looking for server-side includes. Be careful. If you have a very large site this will mean that the Web server will analyze every file requested to see if it contains a server-side include. This can affect the performance of the server.

  3. If you are creating new files that will have server-side includes you should name them with an extension of .shtml. Files ending in .shtml are automatically parsed by the Web server, even without a .htaccess file.

  4. If you might want to use server-side includes in just a few files that already exist (i.e. you don't want to change their name to .shtml), you can specify these files in the .htaccess file. Instead of the:
    AddHandler server-parsed .html
    
    described earlier, use the following code in your .htaccess file:
    <Files "finances.html">
    AddHandler server-parsed .html
    </Files>
    <Files "services.html">
    AddHandler server-parsed .html
    </Files>
    <Files "academics.html">
    AddHandler server-parsed .html
    </Files>
    <Files "online.html">
    AddHandler server-parsed .html
    </Files>
    
  5. You should substitute your files for the ones named above.

  6. Save the .htaccess file and upload it to your publishing directory.

Step 2: Include the file in your Web page

  1. To actually include the contents of a file in your Web page, use the include directive in your HTML:
    <!--#include virtual="/news/channels/communication.html"-->
    
  2. In the example above, whenever the html file is requested, the file called communication.html will be inserted into the file where the include statement appears.

For more information about creating and using RSS files, see http://www.webreference.com/perl/tutorial/8/

Using PHP

RSS is simply a form of XML so any programming languages that can read and parse XML, can retrieve and display an RSS feed. Below is the PHP code that we use on several Web Central pages (see http://www.utexas.edu/news/ as an example) to read and parse PHP.

This PHP script only has a few lines that would need to be changed (like the URL of the RSS file you want to include), but it is not a basic PHP script. If you are not experienced with PHP, you should probably learn a little more about PHP before venturing further.

 
<?php

// Code adapted from Kevin Yank's article on PHP and RSS 1.0 from 
// Sitepoint.  See URL http://www.webmasterbase.com/article/560

// Primary local modifications were to create an array of all stories 
// rather than printing them out one as a time as they are encountered 
// I also made some changes in formatting and created slightly prettier 
// error report

// Script will not display any information from the channel tag 
// It only displays headlines from the item tags

// global variable initialization
$insideitem = false;  // set to true when the parser is inside an item tag
$tag = "";   // name of tag you are in

$title = "";  // title of item
$description = "";  // description of item
$link = "";  // url for item
$numitems = 0;  // number of items

/******************************************************************************* 
    Function:       parseRssFile($xml_parser, $fp, $errorurl, $errortitle) 
    Description:    parses an rss file and does some checking for & characters 
    Arguments:      $xml_parser pointer to XML parser
    $fp file pointer
    $errorurl path to error url as string
    $errortitle an error title as string
    Returns:        nothing
*******************************************************************************/
function parseRssFile($xml_parser, $fp, $errorurl, $errortitle) {
while ($data = fread($fp, 4096)) 
{
       $data = ereg_replace("&", "&amp;", $data);
       
          if (!xml_parse($xml_parser, $data, feof($fp))) 
	     {
              // print error string and clear variables
              print "<a href=\"$errorurl\">$errortitle</a>";
              $title = "";
              $link = "";
              $description = "";
             }
}
	
 // close the file handle
 fclose($fp);
}


//  Change this to the URL of your RSS file 
// ############################################
$rssurl = "http://www.utexas.edu/news/channels/dailytexan.rss";
// ############################################


// if there is a parse error display the following generic 
// title and link rather than a cryptic numeric XML error


//  change title and URL to suit your site 
//  ###########################################
$errortitle = "Today's Daily Texan";
$errorurl = "http://www.dailytexanonline.com/";
//  ###########################################

// startElement is function called when the parser encounters a new element 
// startElement function basically ignores starting elements until 
// it finds an ITEM element, once inside an 

function startElement($parser, $name, $attrs) {
global $insideitem, $numitems, $tag, $title, $description, $link;
    if ($insideitem) {
        $tag = $name;
    } elseif ($name == "ITEM") {
        // we are now inside an item tag so set $insideitem to true
        // and increment $numitems
        $insideitem = true;
        $numitems++;
    }
}

// endElement is function called when the parser encounters an ending element 
// endElement just needs to reset $insideitem variable when it sees 
// </item> because the parser is no longer inside an item tag

function endElement($parser, $name) {
global $insideitem, $numitems, $tag, $title, $description, $link;

if ($name == "ITEM") {

            $insideitem = false;
	    }
}

// characterData is where the work is done 
// when we are inside an ITEM element build an array of headlines 
// with links and descriptions

function characterData($parser, $data) {
global $insideitem, $numitems, $tag, $title, $description, $link;

        // if we are inside an item tag build up the arrays of headline
        // information

        if ($insideitem) {
	switch ($tag) {
	case "TITLE":
	$title[$numitems] .= $data;
	break;
	case "DESCRIPTION":
	$description[$numitems] .= $data;
	break;
	case "LINK":
	$link[$numitems] .= $data;
	break;
}
}
}

// Here is where the work is done and the preceding functions 
// are called

// Create parser and register functions

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");

// open rss file
$fp = fopen("$rssurl","r")
or die("Error reading RSS data.");

// do the parsing
// note that if there is an error it will display the $errortitle set 
// above with a link or $errorurl

parseRssFile($xml_parser, $fp, $errorurl, $errortitle); xml_parser_free($xml_parser);


// If parsing was successful we have three arrays 
// $headlines contains the headline text 
// $link contains the URLs for the headlines 
// $description contain the descriptions for each headline

// The following for loop will print elements from the array 
// Several modifications can be made to the loop 
// The example below will only print the first 6  headlines from the
// RSS file (6 elements from the array)

// if you want to print all elements from the array change 
// $j<=6 to $j<=count($title)

// Also the for loop below does not print descriptions 
// If your channel also contains descriptions you might want to 
// print them as well by trying the following print statement in the loop 
// instead of the one that is there:

// print "<p><a href=\"$link[$j]\">$title[$j]</a><br>$description[$j]</p>\n";

// unroll array printing out links to headlines

for ($j=1; $j<=6; $j++) {
   print "<p><a href=\"$link[$j]\">$title[$j]</a></p>\n";
}


// end script
?>

This script is not the most generalized or robust example, but it serves the needs of Web Central publishers fairly well. A simple google search will reveal numerous example scripts in PHP and other languages that will parse RSS files.



  Updated 2006 February 12
  Comments to www@www.utexas.edu