My 'Quote Of The Day' Function
This is a short function I wrote in PHP to parse an RSS feed from the Quotations Page website. It then displays one of the quotes from the file randomly each time the function is called.
To prevent hammering the RSS server it makes a local cached copy of the RSS file which is then used instead of the remote file. This also has the advantage of being much quicker than transmitting data over the internet. The function automatically checks to see if the cache file is over 24 hours old and, if it is, it replaces it with a fresh copy from the quotation server.
Please note that this function is not a universal RSS parser; rather it is specifically written for this one task in order to be as fast and efficient as possible. If you wish to use it to parse other RSS feeds you will need to modify it. If you are just looking for a more general RSS parser then I'd recommend using CaRP instead.
Here is the source code for the function :
<?
/* Function to return a random quote from an RSS stream. Uses a cached version changed every 24 hours */
function qotd($rssfile="http://www.quotationspage.com/data/qotd.rss")
{
$cachedir ="rsscache";
$cachefile="$cachedir/".basename($rssfile);
if (file_exists($cachefile))
{
$modified = ((time() - filemtime($cachefile))/3600); // Time (in hours) when cache file was last modified
}
else { $modified=0; }
if (($modified > 24 || $modified==0) && $fp=@fopen($rssfile,"r")) // Get RSS file from server if available
{
stream_set_timeout($fp, 1); // Set timeout to one second
$buffer="";
while (!feof ($fp)) $buffer .= fgets($fp, 1024); // Read file into buffer
fclose($fp);
if (!$fp=fopen($cachefile,"wb")) return FALSE; // Write new file to cache
fwrite($fp,$buffer);
}
if (!$fp=fopen($cachefile,"r")) return FALSE;
$buffer="";
while (!feof ($fp)) $buffer .= fgets($fp, 1024); // Read cachefile into buffer
$item=preg_split("/\<item>/s",$buffer); // Split file into array of items
$i=rand(0,(count($item)-2))+1; // Choose a random quote from array
preg_match("/\<title>(.*)\<\/title>/s", $item[$i], $match); // Author
$author=$match[1];
preg_match("/\<description>(.*)\<\/description>/s", $item[$i], $match); // Quotation
$quote=$match[1];
preg_match("/\<link>(.*)\<\/link>/s", $item[$i], $match); // Link URL
$link=$match[1];
return ("<p>$quote - <a href=\"$link\" title=\"Find more quotes from $author\">$author</a></p>\n");
}
?>
How To Use
The function is very simple to use and you should be able to work out how it works from the comments. Just download the script and then you can
call the function in your page with the following command :
qotd();
Alternatively you can pass in a string to the function which should be the full internet address of the RSS file you wish to use :
qotd("http://www.quotationspage.com/data/qotd.rss");
(Please bare in mind, though, it has only been tested on the Quotations Page feed).
Note: you need to make sure that the directory that holds the cache file ($cachedir) and the cachefile itself are writeable by your webserver.
Usage License
This code is free and you can use it as you like. However, if you do use it I'd appreciate a link back to this page :) If you happen to modify or make any improvements to this function then I'd be happy to hear from you and see the code. You can easily contact me.


