1. Go to this page and download the library: Download benjaminhansen/nws-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
benjaminhansen / nws-php example snippets
enjaminHansen\NWS\Api;
/*
** We must provide valid contact information so the National Weather Service
** can properly identify our application on their end
*/
$app_domain = "website/url";
$app_contact = "email/phone";
$api = new Api($app_domain, $app_contact);
/*
** The cache is opt-in. To cache results locally make sure to enable the cache.
** Sometimes, you may not want to use the cache at all, so this allows it to be toggled on/off.
*/
$api->useCache();
// $api->useCache(lifetime: 3600, driver: 'Files'); // override the default cache lifetime seconds and/or storage driver, if necessary
/*
** You can make sure that the API's status returns OK before allowing any
** requests to be made. An exception will be thrown if the API status is not OK.
*/
$api->assertOk();
// $api->assertOk(message: 'custom exception message can be placed here');
/*
** We can set a timezone for the entire API callstack.
** All datetime objects will be converted to this timezone.
**
** If you pass a string version of a timezone, the method will auto-convert it
** to a DateTimeZone object.
*/
$timezone = "America/Chicago";
// $timezone = new DateTimeZone("America/Chicago");
$api->timezone($timezone);
/*
var_dump($api->timezone())
object(DateTimeZone)#18 (2) {
["timezone_type"]=>
int(3)
["timezone"]=>
string(15) "America/Chicago"
}
*/
/*
** We can provide lat/lon coordinates for a specific location
** Dallas, TX for example
*/
$lat = 32.7767;
$lon = -96.7970;
$location = $api->point($lat, $lon);
/*
** Get the location's county information
*/
$county = $location->county;
echo $county->name;
echo $county->id;
echo $county->state->name;
echo $county->state->abbreviation;
var_dump($county->timezone);
var_dump($county->timezones);
/*
** Get information about the location
*/
echo $location->radarStationId;
echo $location->city;
var_dump($location->state);
/*
** Get any active alerts for the location and loop through them
*/
$alerts = $location->activeAlerts->get();
foreach($alerts as $alert) {
var_dump($alert);
}
/*
** Get current observations from the nearest observation station to your location
*/
$observations = $location->latestObservations;
$raw_data = $observations->raw();
var_dump($raw_data);
echo $observations->temperature(show_units: true);
// echo $observations->temperature;
echo $observations->dewpoint;
// other methods are available for the other data points as well
/*
** Get the current forecast for the location
*/
$forecast = $location->forecast;
var_dump($forecast->raw());
var_dump($forecast->periods->get());
/*
** Get the current hourly forecast for the location
*/
$hourly_forecast = $location->hourlyForecast;
var_dump($hourly_forecast->raw());
var_dump($hourly_forecast->periods->get());
/*
** We can also get a specific forecast office
** Little Rock, Arkansas for example
*/
$office_id = "LZK";
$forecast_office = $api->forecastOffice($office_id);
echo $forecast_office->name;
echo $forecast_office->phone;
echo $forecast_office->email;
echo $forecast_office->addresss;
var_dump($forecast_office->counties);
var_dump($forecast_office->forecastZones);
var_dump($forecast_office->observationStations);
var_dump($forecast_office->fireZones);
var_dump($forecast_office->activeAlerts->get());
/*
** We can also get a specific observation station
** Little Rock, Arkansas Airport KLIT for example
*/
$station_id = "KLIT";
$observation_station = $api->observationStation($station_id);
echo $observation_station->name;
echo $observation_station->id;
var_dump($observation_station->timezone);
var_dump($observation_station->county);
var_dump($observation_station->latestObservations);
var_dump($observation_station->activeAlerts->get());
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.