Simplest way to generate a cache of a homepage?

software development

#1

Hello,

I read the wiki entry regarding the disability of remote-access functions of PHP and the recommandation of CURL.

I don’t understand how CURL is more secure than fopen. It is just a different way of accessing the content of a file, isnt’ it?

I have a question. I would like to generate a cache of the front page of my web site so that it is displayed faster and to reduce strain on the servers. Here’s the code I am using:

[CODE]

<?php $ch = curl_init(); $timeout = 5; // set to zero for no timeout curl_setopt ($ch, CURLOPT_URL, 'http://www.example.com/dynamic_index.php'); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $srcurl = curl_exec($ch); curl_close($ch); $tempfilename = 'tempindex.html'; $targetfilename = 'index.html'; ?> Generating <?php echo $targetfilename; ?>

Generating <?php echo $targetfilename; ?>...

<?php @unlink($tempfilename); $htmldata = $srcurl; $tempfile = fopen($tempfilename, 'w'); if (!$tempfile) { exit("

Unable to open temporary file ($tempfilename) for " . "writing. Static page update aborted!

"); } fwrite($tempfile, $htmldata); fclose($tempfile); $ok = copy($tempfilename, $targetfilename); unlink($tempfilename); ?>

Static page successfully updated!

[/CODE]

Do you think there would be a more simple, less cluttered way to do so? It looks quite bloated to my taste :slight_smile:
Thanks in advance!