, (*1)
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., (*2)
Installation and usage
When you have ApiKey, installation and usage is very easy., (*3)
Step 1: Download DwrOpenWeather using composer
Add DwrOpenWeather in your composer.json:, (*4)
{
"require": {
"dwr/open-weather": "1.0.0"
}
}
or download it by running the command:, (*5)
$ 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), (*6)
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), (*7)
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;, (*8)
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., (*9)
weather-basic-small
, (*10)
weather-basic-medium
, (*11)
weather-basic-large
, (*12)
forecast-chart
, (*13)
forecast-basic
, (*14)
License
The MIT License (MIT). Please see License File for more information., (*15)