dwr / open-weather
Simply web widget showing weather in defined location based on Open Weather API
Requires
- php: ~5.6|~7.0
- guzzlehttp/guzzle: ~6.0
Requires (Dev)
- phpunit/phpunit: ~5.0
- satooshi/php-coveralls: ~1.0
- squizlabs/php_codesniffer: ^2.3
This package is not auto-updated.
Last update: 2024-11-09 21:32:30 UTC
README
DwrOpenWeather
DwrOpenWeather is a simply wrapper for Open Weather API.
In order to start please generate your personal ApiKey first.
You can do it here.
Installation and usage
When you have ApiKey, installation and usage is very easy.
Step 1: Download DwrOpenWeather using composer
Add DwrOpenWeather in your composer.json:
{ "require": { "dwr/open-weather": "1.0.0" } }
or download it by running the command:
$ php composer.phar require dwr/open-weather
Step 2: Use it in your application
GET Weather
// errors reporting ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); require __DIR__ . '/vendor/autoload.php'; use Dwr\OpenWeather\Configuration; use Dwr\OpenWeather\OpenWeather; $apiKey = YOURS-API-KEY; //consider keeping api key in environment variable: getenv('OPEN_WEATHER_API_KEY'); $openWeatherConfig = new Configuration($apiKey); $openWeather = new OpenWeather('Weather', $openWeatherConfig); $weather = $openWeather->getByCityName('London'); var_dump($weather);
You can get weather from OpenWeather API by using:
- getByCityName('London')
- getByCityId('2643743')
List of city ID city.list.json.gz can be downloaded here - getByGeographicCoordinates(-0.12574, 51.50853)
GET Forecast
// errors reporting ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); require __DIR__ . '/vendor/autoload.php'; use Dwr\OpenWeather\Configuration; use Dwr\OpenWeather\OpenWeather; $apiKey = YOURS-API-KEY; //Consider keeping api key in environment variable. //$apiKey = getenv('OPEN_WEATHER_API_KEY'); $openWeatherConfig = new Configuration($apiKey); $openWeather = new OpenWeather('Forecast', $openWeatherConfig); $forecast = $openWeather->getByCityName('London'); var_dump($forecast);
You can get forecast from OpenWeather API by using:
- getByCityName('London')
- getByCityId('2643743')
List of city ID city.list.json.gz can be downloaded here - getByGeographicCoordinates(-0.12574, 51.50853)
Configuration (Optional)
You may configure library on your own if you like. There are several variables which you can set by yourself:
- baseUri,
- version,
- timeout,
- httpClient
- apiKey;
require __DIR__ . '/../vendor/autoload.php'; use Dwr\OpenWeather\Configuration; use Dwr\OpenWeather\OpenWeather; $apiKey = YOURS-API-KEY; //Consider keeping api key in environment variable. //$apiKey = getenv('OPEN_WEATHER_API_KEY'); $openWeatherConfig = new Configuration($apiKey); //CONFIGURATION DwrOpenWeather $openWeatherConfig->setBaseUri(NEW-BASE-URI); $openWeatherConfig->setVersion(NEW-API-VERSION); $openWeatherConfig->setTimeout(NEW-TIMEOUT); $openWeatherConfig->setHttpClient(NEW-HTTP-CLIENT); //Has to implement GuzzleHttp\ClientInterface $openWeatherConfig->setApiKey(NEW-API-URI); $openWeather = new OpenWeather('Weather', $openWeatherConfig);
Examples
Take a moment and check examples directory in DwrOpenWeather. Maybe you will find there a solution which you like.
weather-basic-small
weather-basic-medium
weather-basic-large
forecast-chart
forecast-basic
License
The MIT License (MIT). Please see License File for more information.