Forum Moderators: coopster
<?
$expiretime=12; // cache expire time in minutes
$cachename="csv.txt"; //name of the cachefile
//create the cachefile if it doesn't exist
if (!file_exists($cachename)) {
$create = fopen($cachename, 'w');
chmod ("$cachename", 0777); //set chmod 777
fclose($create);
}
// Is the file older than $expiretime, or the file is new/empty?
$FileAge = time() - filectime($cachename);// Calculate file age in seconds
if ($FileAge > ($expiretime * 60) || 0 == filesize($cachename))
{
// Now refresh the cachefile with newer content
$handle = fopen("http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=INANAPOL3&format=1", "r+");
$content = stream_get_contents($handle);
fclose($handle);
$handle = fopen($cachename, 'wb');
fwrite($handle,$content);
}
// seconds to minutes - from http://forums.#*$!.com
$minutes = floor($FileAge/60);
$secondsleft = $FileAge%60;
if($minutes<10)
$minutes = "0" . $minutes;
if($secondsleft<10)
$secondsleft = "0" . $secondsleft;
// Disdplay most recent temperature
$display = file($cachename);
$count = count($display);
for($i=$count - 2; $i<$count-1; $i++){
//echo $display[$i]."<br />";
$temperature = explode(',', $display[$i], 14);
echo "<b>".$temperature[1]."</b>°C (Napoli Posillipo) | ".$minutes."m".$secondsleft."s ago<br />";
}
?>
$datawu = file('http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=INANAPOL3&format=1');
$count2 = count($datawu);
for($i=0; $i<$count2-1; $i++){
$lastRow = $datawu[$i];
}
$handle = fopen($cachename, 'wb');
fwrite($handle,$lastRow);
$display = file($cachename);
$count = count($display);
for($i=0; $i<$count; $i++){
$temperature = explode(',', $display[$i], 14);
echo "<b>".$temperature[1]."</b>°C (Napoli Posillipo) | ".$minutes."m".$secondsleft."s ago<br />";
}
... the very last line of the file is an empty line...
$display = file($cachename);
$count = count($display);
for($i=0; $i<$count; $i++){
$temperature = explode(',', $display[$i], 14);
echo "<b>".$temperature[1]."</b>°C (Napoli Posillipo) | ".$minutes."m".$secondsleft."s ago<br />";
$record = file_get_contents($cachename);
$temperature = explode(',', $record, 14);
echo "<b>".$temperature[1]."</b>°C (Napoli Posillipo) | ".$minutes."m".$secondsleft."s ago<br />";
if(empty ($temperature1[1])){
$temperature1[1] = "N/A";
$temp_avg = "N/A";
}
if(empty ($temperature2[1])){
$temperature2[1] = "N/A";
$temp_avg = "N/A";
}