From: Todd Van Hoosear
Sent: Tuesday, September 28, 2004 4:23 PM
To: chris
Cc: Hope Kercher; Todd Van Hoosear
Subject: PHP RSS Code: How I coded www.bostonspartans.org

If you look at http://www.bostonspartans.org/, the section under "What's New" (really just the 2+ bullets) is generated by sucking in an RSS feed using PHP.
 
The PHP code (the file is called index.php) that generates that part of the page follows:
 
<H3>What's New!</H3>
 
<?php
 
require_once 'rss_fetch.inc';
 
$url = 'http://bostonspartans.blogspot.com/atom.xml';
$rss = fetch_rss($url);
 
echo "<UL>\n";
foreach ($rss->items as $item ) {
$title = $item[title];
$link   = $item[link];
 
echo "<li><a href=$link>$title</a></li>\n";
}
echo "</UL>\n";
?>
 
As you can hopefully see, it requires another PHP file called 'rss_fetch.inc'. This needs to be saved in the same directory as the index.php file. That file is available online at http://www.bostonspartans.org/rss_fetch.inc, so you should be able to see the code.
 
As you can see from the code above, the source file for the RSS feed is an XML file that's sitting at http://bostonspartans.blogspot.com/atom.xml. It's formatted using the Atom specification, which is available online at http://www.ietf.org/internet-drafts/draft-ietf-atompub-format-02.txt. The important thing to know about the specification is how Atom labels each part of the news feed (section 5). The other thing you need to know is how the Atom file is generated.
 
I'll talk about how the Atom file is generated first. I created a blog using Blogger and formatted it to look exactly like my site (by editing the blog template's CSS code to the best of my ability). You can see the blog at http://bostonspartans.blogspot.com/. This takes the place of my site's "News" page, which used to be updated manually by editing the HTML. This makes it much easier for people other than me to make changes to the site.
 
Now anytime somebody submits (and publishes) a new article, the news page (i.e., the blog) is automatically updated. Additionally, the Atom file is automatically updated. (If you click on the Atom file like I gave you above, you can see what it looks like, kind of, but for some reason it disables the "View Source" command, which would be most helpful. So, I made the code available online elsewhere, at http://lalaland.cl.msu.edu/~vanhoose/msu/atom.xml. You should be able to view source on this to see how the Atom file is really organized using XML. It's actually pretty simple.
 
 
 
To figure out how to parse this stuff using the PHP code I found, I wrote a simple PHP page that prints ALL of the elements. That way I know what they're called (at least by the rss_fetch.inc file). The code for the page follows:
 
<?php
/**
* Simple RSS feed news parser
*
*/
 
require_once 'rss_fetch.inc';
 
$url = 'http://bostonspartans.blogspot.com/atom.xml';
$rss = fetch_rss($url);
 
echo "Site: ", $rss->channel['title'], "<br>\n";
echo "<UL>\n";
foreach ($rss->items as $item ) {
 
/**
* COMMENT OUT THE INPUTS YOU DON'T NEED
*/
 
        $title = $item[title];
        $link   = $item[link];
        $author = $item[author];
        $contributor = $item[contributor];
        $id = $item[id];
        $modified = $item[modified];
        $issued = $item[issued];
        $created = $item[created];
        $description = $item[description];
        $content = $item[atom_content];
 
/**
*ACTIONS ON EACH ENTRY
*/
        echo "<li>TITLE=<a href=$url>$title</a></li>\n";
        echo "<UL>\n";
        echo "<li>LINK=$link\n";
        echo "<li>AUTHOR=$author\n";
        echo "<li>CONTRIBUTOR=$contributor\n";
        echo "<LI>ID=$id\n";
        echo "<LI>MODIFIED=$modified\n";
        echo "<LI>ISSUED=$issued\n";
        echo "<LI>CREATED=$created\n";
        echo "<LI>DESCRIPTION=$description\n";
        echo "<LI>CONTENT=$content\n";
        echo "</UL></UL>\n";
}
 
?>
 
You can see the results of this test online at http://lalaland.cl.msu.edu/~vanhoose/msu/test.php. Ignore the fact that I play with formatting a little and actually include a graphic in the TITLE element, which you normally wouldn't. So now you know, at least in PHP using this code, what each of the data elements are and how to display them.
 
So if our goal is to create a snippet of (in this case) PHP code that displays the contents of the Topaz Partners blog, here's what we'd need to do.
 
1. Recode the home page to PHP. Assuming 1) your web server supports PHP, and 2) there isn't much ASP code currently on the home page that would be difficult to port to PHP (two VERY BIG assumptions), it's easy: simply rename the index.asp file to index.php (and change any existing ASP code to PHP--best of luck with that initiative).
 
2. Add the code that makes the sidebar. Assuming I cought the right snippet of code on the website (the part that make the content to the left), it should look like this:
 
 
<table width="180" border="0" cellspacing="0" cellpadding="0">
  <tr>
                    <!--CHANGE THE GRAPHIC BELOW TO ONE THAT READS 'Latest Blog Entries' OR WHATEVER>
    <td colspan="3"><img src="parts/images/main-sidebar_01.gif" alt="" width="278" height="37" border="0"></td>
  </tr>
  <tr>
    <td rowspan="2" valign="bottom" background="parts/images/main-sidebar_02-background.gif">
    <img src="parts/images/main-sidebar_02.gif" alt="" width="29" height="116" border="0"></td>
    <td rowspan="2" valign="top" bgcolor="#f8f8f8"><img src="parts/images/main-sidebar_03.gif"
    alt="" width="230" height="5" border="0">
    <table width="225" border="0" cellspacing="2" cellpadding="0">

<!--TODD'S NEW PHP CODE FOLLOWS>
<?php
 
  require_once 'rss_fetch.inc';
 
  $url = 'http://topazpartners.blogspot.com/atom.xml';
  $rss = fetch_rss($url);
 
  foreach ($rss->items as $item ) {
    $title = $item[title];
    $link   = $item[link];
    $created = $item[created];
 
    echo "<tr><td><font size=1 color=\"#666666\" face=\"Arial,Helvetica,Geneva,Swiss,SunSans-Regular\"><i>\n";
    echo "<a href=\"$link\">$title</a></i></font></td></tr>\n";
    echo "<tr><td><font size=1 color=\"#cc6600\" face=\"Arial,Helvetica,Geneva,Swiss,SunSans-Regular\">\n";
    echo "<b>$created</b></font></td></tr>\n";
 
  }
 
?>
<!--TODD'S NEW PHP CODE ENDS>
 
    </table>
    </td>
    <td rowspan="2" valign="bottom" background="parts/images/main-sidebar_04-background.gif">
    <img src="parts/images/main-sidebar_04.gif" alt="" width="19" height="116" border="0"></td>
  </tr>
  <tr>
  </tr>
  <tr>
    <td colspan="3"><img src="parts/images/main-sidebar_06.gif" alt="" width="278" height="20"
    border="0"></td>
  </tr>
</table>
 
I tested the PHP part of the code online and got this: http://lalaland.cl.msu.edu/~vanhoose/msu/topaz.php. Not bad for a 10-minute hack. For more information on the RSS code I use, go here: http://magpierss.sourceforge.net/.
 
Best of luck implementing this in PHP or translating this to ASP. I *think* the ASP stuff Hope found will do a similar job, but I just don't know enough about ASP to be sure--and it doesn't show any output, so it's hard to figure out what the code does exactly.
 
Let me know if you have any questions!
 
- Todd