Example #1
0
 public function testConfiguration()
 {
     $c = new \noaa\weather\Configuration();
     $this->assertEquals('F', $c->getTemperatureScale());
     $c->setTemperatureScale('C');
     $this->assertEquals('C', $c->getTemperatureScale());
     $this->assertEquals('miles', $c->getDistanceUnit());
     $c->setDistanceUnit('kilometers');
     $this->assertEquals('kilometers', $c->getDistanceUnit());
     $this->assertEquals(new \noaa\weather\cache\NoCache(), $c->getCache());
     $c->setCache(new \noaa\weather\cache\ArrayCache());
     $this->assertEquals(new \noaa\weather\cache\ArrayCache(), $c->getCache());
 }
Example #2
0
<?php

// retrieve the station ID from the command line if supplied
if ($argc > 1) {
    $stationId = $argv[1];
} else {
    echo "Usage: php " . $argv[0] . " STATION_ID\n";
    echo "Example: php " . $argv[0] . " KBAF\n";
    echo "Find your closest station ID here: http://www.weather.gov/xml/current_obs/\n";
    exit(1);
}
// Instantiate a Forecaster object using a file cache
// XML data from the NOAA API will be cached for 1 HOUR in the specified file location
require_once dirname(__FILE__) . '/../noaa/Forecaster.php';
$config = new \noaa\weather\Configuration();
$config->setCache(new \noaa\weather\cache\FileCache(dirname(__FILE__) . '/cache'));
$forecaster = new \noaa\Forecaster($config);
// fetch a CurrentWeather instance for a specific station ID
// find station IDs here: http://www.weather.gov/xml/current_obs/
try {
    $current = $forecaster->getCurrentWeather($stationId);
} catch (\Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
    exit(1);
}
// display
echo $current->getLocation() . "\n";
echo $current->getObservationTime() . "\n";
echo "Temperature: " . $current->getTemperatureF() . " °F\n";
echo "Dew Point: " . $current->getDewPointF() . " °F\n";
echo "Relative Humidity: " . $current->getRelativeHumidity() . " %\n";