
DwrOpenWeatherBundle is a simply wrapper bundle for Open Weather API.
In order to start please generate your personal ApiKey first.
You can do it here.
When you have ApiKey, installation is a quick 3 steps process:
Add DwrOpenWeatherBundle in version 2.0 to your composer.json and run 'composer update'
{
"require": {
"dwr/openweather-bundle": "2.0"
}
}
or download the bundle by running the command:
$ php composer.phar require dwr/openweather-bundle
Composer will install the bundle into your project's vendor/dwr/openweather-bundle directory.
Enable the bundle in the kernel:
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Dwr\OpenWeatherBundle\DwrOpenWeatherBundle(),
);
}
Add APIKEY to your config.yml
dwr_open_weather:
api_key: paste-your-api-key-here
dwr_open_weather:
resource: "@DwrOpenWeatherBundle/Controller/"
type: annotation
Congratulations! You're ready to show weather widget in your symfony application.
Example how weather-basic-small looks like you can find on: yours-application-url/weather-basic-small .
In your Controller
public function indexAction()
{
$openWeather = $this->get('dwr_open_weather');
$weather = $openWeather->setType('Weather')->getByCityName('London');
var_dump($weather);
}
You can get weather from OpenWeather API by using:
In your Controller
public function indexAction()
{
$openWeather = $this->get('dwr_open_weather');
$forecast = $openWeather->setType('Forecast')->getByCityName('London');
var_dump($forecast);
}
You can get forecast from OpenWeather API by using:
Take a moment and check examples. Maybe you will find there a solution which you like.
In order to run examples on your local:
dwr_open_weather:
resource: "@DwrOpenWeatherBundle/Controller/"
type: annotation

Example from: Dwr\OpenWeatherBundle\Controller\DefaultController.php
Action: weatherBasicSmallAction()
/**
* @Route("/weather-basic-small")
*/
public function weatherBasicSmallAction()
{
$openWeather = $this->get('dwr_open_weather');
$weather = $openWeather->setType('Weather')->getByCityName('London');
return $this->render('DwrOpenWeatherBundle:Default:weather-basic-small.html.twig', array(
'weather' => $weather,
));
}

Example from: Dwr\OpenWeatherBundle\Controller\DefaultController.php
Action: weatherBasicMediumAction()
/**
* @Route("/weather-basic-medium")
*/
public function weatherBasicMediumAction()
{
$openWeather = $this->get('dwr_open_weather');
$weather = $openWeather->setType('Weather')->getByCityName('New York');
return $this->render('DwrOpenWeatherBundle:Default:weather-basic-medium.html.twig', array(
'weather' => $weather,
));
}

Example from: Dwr\OpenWeatherBundle\Controller\DefaultController.php
Action: weatherBasicLargeAction()
/**
* @Route("/weather-basic-large")
*/
public function weatherBasicLargeAction()
{
$openWeather = $this->get('dwr_open_weather');
$weather = $openWeather->setType('Weather')->getByCityName('Beijing');
return $this->render('DwrOpenWeatherBundle:Default:weather-basic-large.html.twig', array(
'weather' => $weather,
));
}

Example from: Dwr\OpenWeatherBundle\Controller\DefaultController.php
Action: forecastChartAction()
/**
* @Route("/forecast-chart")
*/
public function forecastChartAction()
{
$openWeather = $this->get('dwr_open_weather');
$city1 = 'Warsaw';
$forecastCity1 = $openWeather->setType('Forecast')->getByCityName($city1);
$forecastCity1Labels = json_encode(array_map(function ($value) {
return Converter::intToDate($value['dt'], 'd-m-Y H:i');
}, $forecastCity1->lists()));
$forecastCity1Temps = json_encode(array_map(function ($value) {
return Converter::kelvinToCelsius($value['main']['temp']);
}, $forecastCity1->lists()));
$city2 = 'Berlin';
$forecastCity2 = $openWeather->setType('Forecast')->getByCityName($city2);
$forecastCity2Labels = json_encode(array_map(function ($value) {
return Converter::intToDate($value['dt'], 'd-m-Y H:i');
}, $forecastCity2->lists()));
$forecastCity2Temps = json_encode(array_map(function ($value) {
return Converter::kelvinToCelsius($value['main']['temp']);
}, $forecastCity2->lists()));
$city3 = 'London';
$forecastCity3 = $openWeather->setType('Forecast')->getByCityName($city3);
$forecastCity3Labels = json_encode(array_map(function ($value) {
return Converter::intToDate($value['dt'], 'd-m-Y H:i');
}, $forecastCity3->lists()));
$forecastCity3Temps = json_encode(array_map(function ($value) {
return Converter::kelvinToCelsius($value['main']['temp']);
}, $forecastCity3->lists()));
return $this->render('DwrOpenWeatherBundle:Default:forecast-chart.html.twig', array(
'city1' => $city1,
'forecastCity1' => $forecastCity1,
'forecastCity1Temps' => $forecastCity1Temps,
'forecastCity1Labels' => $forecastCity1Labels,
'city2' => $city2,
'forecastCity2' => $forecastCity2,
'forecastCity2Temps' => $forecastCity2Temps,
'forecastCity2Labels' => $forecastCity2Labels,
'city3' => $city3,
'forecastCity3' => $forecastCity3,
'forecastCity3Temps' => $forecastCity3Temps,
'forecastCity3Labels' => $forecastCity3Labels,
));
}

Example from: Dwr\OpenWeatherBundle\Controller\DefaultController.php
Action: forecastBasicAction()
/**
* @Route("/forecast-basic")
*/
public function forecastBasicAction()
{
$openWeather = $this->get('dwr_open_weather');
$forecastCity = $openWeather->setType('Forecast')->getByCityName('Rome');
$forecast = array_map(function ($value) {
return [
'timestamp' => $value['dt'],
'temp' => $value['main']['temp'],
'pressure' => $value['main']['pressure'],
'humidity' => $value['main']['humidity'],
'description' => ($value['weather'][0]['description'])?$value['weather'][0]['description']:'',
'icon' => ($value['weather'][0]['icon'])?$value['weather'][0]['icon']:'',
];
}, $forecastCity->lists());
return $this->render('DwrOpenWeatherBundle:Default:forecast-basic.html.twig', array(
'forecastCity' => $forecastCity,
'forecast' => $forecast
));
}
Please see CHANGELOG for more information on what has changed recently.
The MIT License (MIT). Please see License File for more information.
How can I help you explore Laravel packages today?