This is a how-to that describes a way of setting up an rss feed on your website using PEAR, an extension of php. While there are many such how-to's, this one explains how to avoid changing your firewall settings. This is important because the more changes one makes to his firewall, the more chances for security issues to arise. This is nothing world changing . . . just something you may not have thought of. Note: The pound sign (#) represents your command prompt.Pre-Requisites
PHP PEAR Details
Install XML_Parser package (#pear install XML_Parser)
Install XML_RSS package (This install should be similar to XML_Parser above, but my experience wasn't so consistent. I had to be more explicit about what to install. Normally, the install would automatically *get* the package and install it. I had to make the *get* path explicite eg., #pear install http://pear.php.net/get/XML_RSS)
Create a php file that will parse the rss/xml information. Compare PEAR's sample code with mine. PEAR code:
My code:
require_once 'XML/RSS.php'; $rss =& new XML_RSS("http://rss.slashdot.org/Slashdot/slashdot"); $rss->parse(); echo " \n"; foreach ($rss->getItems() as $item) { echo "
\n"; ?>- " . $item['title'] . "
\n"; } echo "Beside the fact that my code involves a generic php function (which you should note), they don't look too different. However, notice what is being parsed. In the PEAR sample code, XML_RSS goes to the specific URL to get that which you want to be fed (eg., headlines) when the XML_RSS function is called. Thus, if you were to make a php function, where the parameter was that which is to be parsed, you would give it a URL.
function rss($xml) { require_once "XML/RSS.php"; $rss =& new XML_RSS($xml); $rss->parse(); echo " \n"; foreach ($rss->getItems() as $item) { echo "
\n"; } ?>- " . $item['title'] . "
\n"; } echo "In my code, a local file (eg., slashdot.xml) is parsed. That is, the parameter of the function rss is a .xml file found locally. Apache doesn't need to go "out-of-the-box" to get the info.
The point of not using the the default settings is that in order to get it to work, you need to open up "paths" through your firewall to allow apache to access info via url, rather than an in house file. Well, skrew that! The less holes in my firewal, the better! So, I suggest you write a shell script that ftp's the stuff you want. For example, my slashdot.sh . . .
Note that root d'loads the, in this case, slashdot file in a place that may not be easily/intuitively accessible. Thus, I *mv* it from where it is to a place that I like. You can *find* the file, if you lose it (. . . it's somewhere).
#!/bin/sh
#fetch slashdot headlines and then mv them to accessible location
/usr/bin/ftp http://rss.slashdot.org/Slashdot/slashdot
/bin/mv /path/to/slashdot /path/to/slashdot.xml
Now you write a cronjob that runs that script as often as you want your feed updated. I do mine hourly.
0 * * * * /path/to/slashdot.sh All you need to do now is to call the php function from the page where you'd like to display the rss feed. In this case, the above outputs a link to each headlined article at slashdot. Consult, the PEAR documentation to gather other pertinent info. like images.
require_once 'rssFeed.php'; rss(/path/to/slashdot.xml); ?> Check out my news page for a glimpse of what the result of the above tutorial looks like.