Forum Moderators: coopster

Message Too Old, No Replies

Creating a Cache File

         

jeffgman

2:29 pm on Aug 31, 2004 (gmt 0)

10+ Year Member



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

jatar_k

4:35 pm on Aug 31, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what is in the var $cache_file?
I don't see it being set anywhere.

jeffgman

5:40 pm on Aug 31, 2004 (gmt 0)

10+ Year Member



Stupid me. That is what happens when you start to copy some code from somewhere else, but forget to put in the variable names that you have assigned. That did it. Thanks.

One more question. Shouldn't I be able to do a less on that cache file and see the xml?