Rhode Island eGovernment Exchange

REX Parser

Using the Rex Parser with PHP on Linux

Requirements: rex_parser.php, config.inc installed and configured, PHP must be installed and available on your server.

If your site is hosted on the servers at the RI Department of Administration (SORI) the rex_parser.php and config.inc files have already been set up and are available for use.

Please skip the installation instructions and go directly to Step 3, Create or edit the page where you want to display the feed

The include path for the parser is:
"<? include_once("/usr/local/apache/REX/rex_parser.php"); ?>"

Step by step:

  1. Download and copy rex_parser.php and config.inc to a directory on your webserver (details)
  2. Edit config.inc to work with your server's configuration (details)
  3. Create or edit the page where you want to display the feed (details)
  4. Add the simple PHP code to your page to display an RSS feed. (details)
  5. Format the look of your feed (details)
  6. Save the page

Installing and configuring the rex_parser.php and config.inc

Note: You only need to install and configure these files once on your server to use any RSS feeds which are compatible with this solution!

  1. Email RI.gov to obtain a copy of the REX parser
  2. Unzip these files and copy them to your webserver in a subdirectory you choose.
  3. Create a directory to store the RSS cache files. This directory must be writable by your web server software, but should NOT be in a directory that is accessible via a Web browser. The example config.inc file places cache files in "/tmp/rsscache"
  4. Make sure that rex_parser.php is world-readable (chmod a+r rex_parser.php)

Editing the .inc file.

The .inc file has four lines of code:

<?
define('RSS_READER_CACHE_ON', true);
define('RSS_READER_CACHE_DIR', '/tmp/rsscache');
define('RSS_READER_CACHE_AGE', '3600');
define('CURL_PATH', '/usr/local/bin/curl');
?>

Line 1: This line tells the software to cache the results of your rss requests. Setting the parameter to 'true' is less taxing on the server that is hosting the RSS feed. In some cases it is mandatory that you set this parameter to 'true'. It is recommend that you leave this line alone.

Line 2: This line tells the software where to cache the RSS results. You must edit the path on this line to where you created your "cache" directory. An appropriate example cache directory for most UNIX/Linux based servers would be "/tmp/rsscache"

Line 3: It is recommended that you do not change this line.

Line 4: CURL is a standard library required by the software. As it is almost always included as a normal install of apache it will in most cases be in a directory called /usr/local/bin/curl. If you are working with a unique build of Apache, or another web server system you will need to edit this line to the correct path. To determine the path of your CURL file you may telnet to your server and type the command "which CURL" the server will return the correct path.

When you have completed any needed changes, save the file.

Getting the page ready for the feed

You can create the page the same way you create all of your other webpages however the page name must end with the extension ".PHP" not .HTML or .SHTML as you may be used too.

At the very top of the page you need to add the following code:

<? include_once("path_to/rex_parser.php"); ?>

Change the path to the path where you've saved both the rex_parser.php and config.inc file.

If you are hosted on the SORI server the line of code should read:

<? include_once("/usr/local/apache/REX/rex_parser.php"); ?>

Placing RSS headlines on the page

Now, add headlines to this page via RSS using the following line of code as an example:.

<?PHP echo get_rss("http://www.ri.gov/rss/1/news.rss",1,5,1,1); ?>

Understanding how the widget works:

The <?PHP and ?> tags tell the webserver to stop serving HTML code and treat any lines within these tags as PHP programing code. Anything on the page outside of these tags will display the same as they would on any other web page.

The echo command simply tells PHP to display the output of the widget (the feed) on the page.

The get_rss() is a function which is called from the rex_parser.php file.

The get_rss() function requires parameters. You will note that each parameter is separated by a comma.

At this time, the rex parser requires five parameters:

  1. The URL of the feed
  2. The starting position in the list
  3. Number of items to display
  4. An option to display, or not display descriptions in the list
  5. An option to display, or not display the publication date in the list

The first parameter is the URL of the RSS file for the feed you wish to present on your page. For example, changing the URL like this:

<?PHP echo get_rss("http://www.projo.com/rss/st.rss",1,5,1,1); ?>
Offers headlines from the Providence Journal

There are many resources for these URLs. For RI government feeds you can view the list of feeds here or you can research the many RSS feeds available from other sources. You will find some helpful links to some of these sites on our XML resources page.

The second parameter is the starting position in the list. To understand this, think of an RSS feed as a list of links - in most cases the newest link is the first. The starting parameter would usually be set to "1" however, having this parameter available, allows you to start at any position in the list.

The third parameter is a number representing the total number of items in the list you wish to display. If the list has fewer items than the number you set it will only display the number of items available.

The fourth parameter can be set to 0 or 1. If it is set to 1 it will display the description of the list item. If it is set to 0 it will not display this item. This parameter is useful as not all RSS feeds include a description.

The fifth parameter can be set to 0 or 1. If it is set to 1 it will display the publication date of the list item. If it is set to 0 it will not display this item. This parameter is useful as not all RSS feeds include a publication date.

When you are done your page should look something like this:

Formatting the results:

The Rex Parser embeds several CSS tags in its output - this is done so that you can customize the look of each part of the display.

If you wish to create your own style sheet, here is a list of the tags:

<div class="rsspubdate">03-06-2004</div>
<div class="rssheadline">THE HEADLINE</div>
<div class="rssdescription">This is the description of the headline</div>

Here is a sample style sheet: