DwrOpenWeather is a simply wrapper for Open Weather API.
In order to start please generate your personal ApiKey first.
You can do it here.
When you have ApiKey, installation and usage is very easy.
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
// 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)
// 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)
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);
Take a moment and check examples directory in DwrOpenWeather. Maybe you will find there a solution which you like.
The MIT License (MIT). Please see License File for more information.