I am having some problems creating a cache file for storing a recently grabbed rss feed. Here is my code:
<HTML>
<TITLE>RSS Feed - CNet News.com</TITLE>
<BODY>
<?
$filename = "cache/news.com.cache";
$age = 3600; // seconds
$url = 'http://news.com.com/2547-1_3-0-20.xml';
if ( file_exists ( $filename ) ) {
$mtime = filemtime( $filename );
$fileage = time() - mtime;
if ( fileage > $age ) {
$xml = simplexml_load_file($filename);
$usedfile = "true";
} else {
$xml = simplexml_load_file($url);
$usedfile = "false";
}
} else {
$xml = simplexml_load_file($url);
$usedfile = "false";
}
echo $xml->channel->title, '<br />';
echo '<br />';
foreach ($xml->channel->item as $item) {
echo "<A HREF=\"$item->link\">$item->title</A>", '<br />';
echo $item->description, '<br />';
echo $item->pubDate, '<br />';
echo '<br />';
}
if ($usedfile = "false") {
$fp = @fopen( $cache_file, 'w' );
fwrite( $fp, $xml );
fclose( $fp );
}
?>
</BODY>
</HTML>
I am having problems with the fwrite line. I guess I am doing something wrong. Here is the error which PHP gives me:
Warning: fwrite(): supplied argument is not a valid stream resource in /home/httpd/html/rss-news.com.php on line 36
Warning: fclose(): supplied argument is not a valid stream resource in /home/httpd/html/rss-news.com.php on line 37
Thanks for any help you can offer me.
Jeff