Categories:

A PHP script to get the contents of a remote file

Since Ajax is restricted to calling local files on the server, we need a PHP script to at least grab the contents of the remote RSS file and feed it to our JavaScript on the other side. Furthermore, the script must cache the RSS file locally on the server and only make periodic requests to the actual RSS feed for updates if we know what's good for us. Here it is:

<?
/*
======================================================================
Get, cache, and output contents of a RSS XML file
Author: George at JavaScriptKit.com/ DynamicDrive.com
Created: Feb 1st, 2006. Updated: Feb 1st, 2006
======================================================================
*/

header('Content-type: text/xml');

// -------------------------------------------------------------------
// Enter list of possible RSS feeds to fetch inside array:
// -------------------------------------------------------------------

$rsslist=array(
"CNN" => "http://rss.cnn.com/rss/cnn_topstories.rss",
"BBC" => "http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml",
"news.com" => "http://news.com.com/2547-1_3-0-5.xml",
"slashdot" => "http://rss.slashdot.org/Slashdot/slashdot",
"dynamicdrive" => "http://www.dynamicdrive.com/export.php?type=new"
);

$cachefolder="cache"; //path to cache directory. No trailing "/". Set dir permission to read/write!

// -------------------------------------------------------------------
// Determine which RSS file to actually fetch
// Based on the value of the "id" parameter of the URL string mapping to the RSS array's key
// -------------------------------------------------------------------

$rssid=$_GET['id'];
$rssurl=isset($rsslist[$rssid])? $rsslist[$rssid] : die("Error: Can't find requested RSS in list.");
$localfile=$cachefolder. "/" . urlencode($rssurl); //Name cache file based on RSS URL

// -------------------------------------------------------------------
// Get the minutes to cache the local RSS file based on "cachetime" parameter of URL string
// -------------------------------------------------------------------

$cacheminutes=(int) $_GET["cachetime"]; //typecast "cachetime" parameter as integer (0 or greater)

// -------------------------------------------------------------------
// fetchfeed() gets the contents of an external RSS feed,
// and saves its contents to the "cached" file on the server
// -------------------------------------------------------------------

function fetchfeed(){
global $rssurl, $localfile;
$contents=file_get_contents($rssurl); //fetch RSS feed
$fp=fopen($localfile, "w");
fwrite($fp, $contents); //write contents of feed to cache file
fclose($fp);
}

// -------------------------------------------------------------------
// outputrsscontent() outputs the contents of a RSS feed using the cached local RSS file
// It checks if a cached version of the RSS feed is available, and if not, creates one first.
// -------------------------------------------------------------------

function outputrsscontent(){
global $rssurl, $localfile, $cacheminutes;
if (!file_exists($localfile)){ //if cache file doesn't exist
touch($localfile); //create it
chmod($localfile, 0666);
fetchfeed(); //then populate cache file with contents of RSS feed
}
else if (((time()-filemtime($localfile))/60)>$cacheminutes) //if age of cache file great than cache minutes setting
fetchfeed();
readfile($localfile); //return the contents of the cache file
}

outputrsscontent();
?>

Click here to see a sample output by the script based on the parameters passed into it.

Don't be intimated by the length of the script, a good chunk of it is comments and spacing. :) All this PHP script does is grab the contents of a remote RSS file and return its content unparsed back to the browser as a XML file (specified using header()).. The script will cache the contents of the file after the initial request to it by creating a duplicate file on the server, and update the later based on the user specified cache time. Be sure that the cache directory specified (ie: "cache") has read/write permission set, via FTP for example. Both the exact remote RSS file to grab and the cache duration is controlled by passing in the appropriate parameter to the script via GET (URL string). This design is to facilitate what comes next- using Ajax coupled with JavaScript to pull this PHP's strings and do what they want, on demand!

Ok, time for the real magic to occur- using Ajax to get the XML file, and JavaScript to pound it into something legible and dynamic.