Skip to content

cristianradu/simple-nws

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SimpleNWS

PHP library for accessing NOAA's National Weather Service.

Using the free REST API provided by the National Weather Service should be straightforward enough - except it isn't. The results are overly convoluted - made to be read by machines, not humans. The purpose of this library is to make all that easier by doing all the hard work for you - it takes a latitude and longitude and gives back a nicely formatted object with all the weather data you need.

Requires PHP 5.3 (uses namespaces). Written by Cristian Radu, http://cristianradu.com/. MIT licensed, see the LICENSE file for details.

Example

// set your values for latitude and longitude
$lat  =  40.75;
$long = -73.92;

// instantiate the library
$simpleNWS = new SimpleNWS($lat, $long);

// get the forecast (for today in this example)
$forecast = $simpleNWS->getForecastForToday();

// extract the data that you need
$apparentTemperature = $forecast->getHourlyApparentTemperature();

Alternate use: if you prefer, you can pass the latitude and longitude parameters to the forecast function instead of the library constructor:

$simpleNWS = new SimpleNWS();

$forecast = $simpleNWS->getForecastForToday($lat, $long);

Check out example.php for a complete list of use cases for the library.

Library Methods

After instantiating the library with the latitude and longitude, you can use one of the following self-explanatory methods to retrieve the weather forecast:

  • getCurrentConditions()
  • getForecastForToday()
  • getForecastForWeek()

Forecast Model

All the methods return a forecast model object. You can use the following methods to extract the desired weather data:

Weather Data Method Returns Interval Units
Hourly Recorded Temperature getHourlyRecordedTemperature() array of integers hourly (every 3h) degrees F
Hourly Apparent Temperature getHourlyApparentTemperature() array of integers hourly (every 3h) degrees F
Daily Maximum Temperature getDailyMaximumTemperature() array of integers daily (every 24h) degrees F
Daily Minimum Temperature getDailyMinimumTemperature() array of integers daily (every 24h) degrees F
Hourly Precipitation getHourlyPrecipitation() array of floats hourly (every 6h) inches
Hourly Snow Amount getHourlySnowAmount() array of floats hourly (every 6h) inches
Hourly Wind Speed getHourlyWindSpeed() array of integers hourly (every 3h) knots
Hourly Wind Direction getHourlyWindDirection() array of integers hourly (every 3h) degrees
Hourly Cloud Coverage getHourlyCloudCover() array of integers hourly (every 3h) percent
Hourly Humidity getHourlyHumidity() array of integers hourly (every 3h) percent
Weather Conditions getWeatherConditions() array of arrays hourly (every 3h) description of weather type, intensity, coverage

The resulted arrays will have a time layout timestamp as the key. The format is YYYY-MM-DD-HH. The intervals are determined by the method you used to extract the weather data (see the above table).

Example:

Array (
    [2011-08-21-20] => 78
    [2011-08-21-23] => 74
    [2011-08-22-02] => 70
    [2011-08-22-05] => 67
    [2011-08-22-08] => 68
    [2011-08-22-11] => 75
    [2011-08-22-14] => 80
    [2011-08-22-17] => 79
... )

Extracting the weather conditions will return a timestamped list of arrays containing the description of the weather:

Array (
    [2011-08-21-23] => Array (
        [weather_type] => thunderstorms
        [intensity] => heavy
        [coverage] => likely
        )
... )

You can convert any of the results in degrees Celsius by passing them through the convert method:

$degreesCelsius = $forecast->convertToCelsius($forecast->getHourlyRecordedTemperature());

That's it! Have fun using the library. For questions and comments drop me a line at hi@cristianradu.com

About

PHP library for accessing NOAA's National Weather Service

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages