Hi, I understand that you can use cron jobs to achieve this, but I can't use cron in my case.
I have a script that will copy weather details off of the Government of Canada site and put it on my site. If the site gets popular, I don't want the script to fetch the info every time the page refreshes. So far, I have this script put together, however all I can say is that it "doesn't work" I only get a blank page with no errors from my host.
The index will see if the script has been ran before 2000 seconds, if not it will execute weather.php, if it has, it will show the buffered results from buffer.log. When weather.php executes, it echos the result to the page and also writes the html to buffer.log
This is in index.php
<?php
$lastRunLog = '/lastrun.log';
$lastRunBuf = '/buffer.log';
if (file_exists($lastRunLog) && file_exists($lastRunBuf)) {
$lastRun = file_get_contents($lastRunLog);
if (time() - $lastRun >= 2000) {
$cron = file_get_contents('weather.php');
file_put_contents($lastRunLog, time());
eval($cron);
}
elseif {
$buffer = file_get_contents($lastRunBuf);
echo "$buffer";
}
}
?>
weather.php
<?php
$url = "http://www.weatheroffice.gc.ca/city/pages/on-69_metric_e.html";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 100);
$text = curl_exec($ch);
$buffer = "buffer.log";
$fh = fopen($buffer, 'w') or die("Can not open buffer");
$exploded = explode('<p class="temperature">',$text);
$result = explode('<sup>',$exploded[1]);
$exploded2 = explode('<img id="currentimg" src="/weathericons/',$text);
$idnum = explode('.gif"',$exploded2[1]);
if ($idnum[0] == ''){
$idnum[0] = '50';
}
$exploded3 = explode('<dt>Condition:</dt>',$text);
$iden = explode('</dd>',$exploded3[1]);
curl_close($ch);
$iden2 = ereg_replace("<dd>", "", $iden[0]);
$iden3 = ereg_replace(" ", "", $iden2);
$num = intval($idnum[0]);
if($num < 10){
$stringdata1 = "<img src=\"images/weather/0$num.png\" style=\"border-style: none\">";
echo "$stringdata1";
fwrite($fh, $stringdata1);
}
if($num > 9 && $num < 50){
$stringdata2 = "<img src=\"images/weather/$num.png\" style=\"border-style: none\">";
echo "$stringdata2";
fwrite($fh, $stringdata2);
}
if($num == 50){
$stringdata3 = "<img src=\"images/weather/31.png\" style=\"border-style: none\">";
echo "$stringdata3";
fwrite($fh, $stringdata3);
}
else{
$stringdata4 = " ";
echo "$stringdata4";
fwrite($fh, $stringdata4);
}
$stringdata5 = " <span id=\"number\">$result[0]</span> $iden3";
echo "$stringdata5";
fwrite($fh, $stringdata5);
fclose($fh);
?>
buffer.log and lastrun.log exist and is blank.
Thanks for reading. Hopefully its a stupid mistake, I'm still pretty new to PHP. My apologies for the bad spacing, some formatting issues with Dreamweaver.