I have two php pages, the first of which contains a "config section." In the config section of the first page, I have defined Celsius by inserting the following:
define('DEFAULT_UNITS', "c");
It seems as of the problem is the weather class assumes the default is Fahrenheit and will not switch to Celsius because it has no idea what is stored in DEFAULT_UNITS. Can someone troubleshoot? I've been staring at this so long that I'm probably looking right through the answer.
//////// PAGE 1 (index.php) ////////
<?php
include('weather.class.php');
/* Config Section */
$zip = "24018"; // Input your zip code
define('DEFAULT_UNITS', "c"); // f=Fahrenheit, c=Celsius
define('IMAGES', 'icons/sm/'); // Input your image folder location
/* End Config Section */
if($zip != '')
{
$weather = new Weather();
$weather = $weather->getWeather($zip, $_GET['units']);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Weather</title>
</head>
<body>
<?php
{
echo "\t\t\tCurrent Temperature<br/>".$weather['temp']."\n";
}
?>
</body>
</html>
//////// PAGE 2 (weather.class.php) ////////
<?php
define('WEATHER_FILE', 'weather'); // prefix for caching, <zip>.xml
class weather {
var $data;
var $saved;
var $metric;
function getWeather($zip, $units=DEFAULT_UNITS) {
// setup the units
$this->metric = (strtoupper($units) == 'C');
if($this->metric) $units = array(temp=>'C', distance=>'km', measure=>'mb', speed=>'kph');
else $units = array(temp=>'F', distance=>'mi', measure=>'in', speed=>'mph');
$pdir = array('steady', 'rising', 'falling');
$wdir = array('N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW', 'N');
// get the feed contents from file unless older than 1 hour
$file = WEATHER_FILE.".{$zip}.xml";
if(!file_exists($file) || filemtime($file) < time() - 3600) {
$this->data = @file_get_contents("http://xml.weather.yahoo.com/forecastrss?p={$zip}&u=".$units['temp']);
$fp = @fopen($file, 'w');
@fwrite($fp, $this->data);
@fclose($fd);
}
else $this->data = @file_get_contents($file);
if(strlen($this->data) <= 0) return;
// get the units of the saved file
$this->saved = explode('"', $this->tag('yweather:units'));
$this->saved = (strtoupper($this->saved[1]) == 'C');
$return = array();
// get the location
$attr = explode('"', $this->tag('yweather:location'));
$return['location'] = $attr[1].', '.$attr[3].', '.$attr[5];
// get the wind data
$attr = explode('"', $this->tag('yweather:wind'));
$return['windchill'] = $this->convert($attr[1], 'temp').'°'.$units['temp'];
$return['wind'] = $this->convert($attr[5], 'speed').' '.$units['speed'].' '.$wdir[round($attr[3]/45)];
// get the atmosphere data
$attr = explode('"', $this->tag('yweather:atmosphere'));
$return['humidity'] = $attr[1].'%';
$return['visibility'] = ($this->convert($attr[3], 'distance') / 100).' '.$units['distance'];
$return['preasure'] = $this->convert($attr[5], 'measure').' '.$units['measure'].' '.$pdir[$attr[7]];
$attr = explode('"', $this->tag('yweather:astronomy'));
$return['sunrise'] = $attr[1];
$return['sunset'] = $attr[3];
// get the temperature data
$attr = explode('"', $this->tag('yweather:condition'));
$return['text'] = $attr[1];
$return['temp'] = $this->convert($attr[5], 'temp').'°'.$units['temp'];
$return['image'] = IMAGES.$this->translate($attr[3], 'temp').'.png';
// get the two forecasts
$return['forecast'] = array();
for($i = 0; $i < 2; $i++) {
$attr = explode('"', $this->tag('yweather:forecast',$i));
if(count($attr) > 1) {
$day = array();
$day['when'] = $attr[1];
$day['low'] = $this->convert($attr[5], 'temp').'°'.$units['temp'];
$day['high'] = $this->convert($attr[7], 'temp').'°'.$units['temp'];
$day['text'] = $attr[9];
$day['image'] = IMAGES.$this->translate($attr[11]).'.png';
array_push($return['forecast'], $day);
}
}
return $return;
}
function tag($tag, $skip=0) {
$start = -1;
for($i = 0; $i <= $skip; $i++)
$start = strpos($this->data, "<{$tag}", $start + 1);
if($start === false) return false;
$start += strlen($tag) + 1;
$end = strpos($this->data, "</{$tag}>", $start);
if($end === false)
$end = strpos($this->data, '/>', $start);
return trim(substr($this->data, $start, $end - $start));
}
function convert($value, $type) {
switch($type) {
case 'temp': // Celsius or Farenheit
if($this->saved == $this->metric) return $value;
if($this->saved) return number_format($value * 1.8 + 32, 0);
return number_format(($value - 32) / 1.8, 0);
case 'speed': // kilometers per hour or miles per hour
case 'distance': // kilometers or miles
if($this->saved == $this->metric) return $value;
if($this->saved) return number_format($value * 0.621371192, 0);
return number_format($value * 1.609344, 0);
case 'measure': // millibars or inches
if($this->saved == $this->metric) return $value;
if($this->saved) return number_format($value * 0.0295301, 2);
return number_format($value * 33.8637526, 0);
}
}
}
?>