PHPWeatherLib is free for both commercial and personal use as long as all sites include at least one link to the project homepage. ( http://www.ebrueggeman.com/phpweatherlib ) Link examples:
Weather by PHPWeatherLib | Weather on this site generated by the PHPWeatherLib Library
To use PHPWeatherLib, you must do three things:
To include the PHP file, add this php include call at the top of your page source code:
include("phpweatherlib.php");
Before you create the Weather Lib Object you must find the call letters to the NOAA weather reporting station that you wish to reference. Visit the NOAA's National Weather Service page to find the appropriate weather station. http://www.weather.gov/data/current_obs/ Click the Select a State drop down and then click Find. A list of results for that state will be shown followed by four uppercase call letters (example KJFK, KJAN, etc.)
Now you can create the WeatherLib object in your source code. Create an object by assigning a variable to a new instance of the WeatherLib object with the call letters as the only argument to the object constructor. For example, to create the object for New York's JFK Airport, call letters KJFK, use the following PHP code:
$weatherObj=new WeatherLib("KJFK");
WeatherLib includes error reporting and supporting functions. To handle errors gracefully, it is recommended that your use these feature when creating a WeatherLib() object.
The following code displays the appropriate error message if the WeatherLib object cannot be created or has trouble parsing the XML weather feed.
$weatherObj=new WeatherLib("KJFK")
if($weatherObj->has_error())
{
echo $weatherObj->get_error();
}
There are many included functions in the PHPWeatherLib library to retrieve weather data from the station you specified when creating the WeatherLib() object. Below is an example of a few of these functions. The full function list is listed below in the Function Reference section.
The following code displays:
Right now in New York City, Central Park, NY it is 75.0 F!
$location=$weatherObj->get_location(); $temp=$weatherObj->get_temp_f(); echo "Right now in " . $location . " it is " . $temp . " F!";
| Function | Description |
|---|---|
| get_location() | Returns the location string of the weather station. Example: "New York City, Central Park, NY". |
| get_latitude() | Returns the latitude of the weather station. |
| get_longitude() | Returns the longitude of the weather station |
| get_observation_time() | Returns the last observation time string. Example: "Last Updated on Mar 7, 10:51 am EST". |
| get_weather_string() | Returns a short description of the weather conditions. Example: "Light Snow". |
| get_temp_f() | Returns the current temperature in Fahrenheit. |
| get_temp_c() | Returns the current temperature in Celsius. |
| get_humidity() | Returns the current relative humidity. |
| get_wind_string() | Returns a descriptive string about the wind conditions. Example: "Calm". |
| get_wind_dir() | Returns the direction of the wind as a string. Example: "Northwest" . |
| get_wind_mph() | Returns the wind speed in MPH. |
| get_pressure() | Returns the barometric pressure. |
| get_dewpoint_f() | Returns the dew point in Fahrenheit. |
| get_dewpoint_c() | Returns the dew point in Celsius. |
| get_heatindex_f() | Returns the heat index in Fahrenheit. |
| get_heatindex_c() | Returns the heat index in Celsius. |
| get_windchill_f() | Returns the wind chill in Fahrenheit. |
| get_windchill_c() | Returns the wind chill in Celsius. |
| get_visibility() | Returns the visibility in miles. |
| get_icon_base() | Returns the root of the weather icon image associated with the current weather condition. Example: "http://weather.gov/weather/images/fcicons/". |
| get_icon_file() | Returns the image name of the weather icon image associated with the current weather condition. Example: "nskc.jpg". |
| get_icon() | Returns the whole image url of the weather icon image associated with the current weather condition. Example: "http://weather.gov/weather/images/fcicons/nskc.jpg". |
| get_error() | Returns a descriptive error string of any errors encountered while trying to create a WeatherLib() object. |
| has_error() | Returns a true or false if an error has occurred. |