dev-master
9999999-dev https://github.com/cakephp-fr/openweathermapOpenweathermap plugin for CakePHP
MIT
The Requires
- php >=5.4.16
- cakephp/cakephp ~3.1
The Development Requires
cakephp openweathermap
Wallogit.com
2017 © Pedro Peláez
Openweathermap plugin for CakePHP
You can install this plugin into your CakePHP application using composer. The recommended way to install composer packages is:, (*2)
composer require cakephp-fr/openweathermap:dev-master
Load your plugin using:, (*3)
bin/cake plugin load Openweathermap
or add manually CakePlugin::load('Localized') in your boostrap.php., (*4)
Create 2 tables : weatherdatas & weathersites with the help of initial migration file into 'Migrations' directory of the plugin (/Openweathermap/configs/Migrations), (*5)
bin/cake migrations migrate -p Openweathermap
To configure the plugin you can add the openweathermap config to the config/app.php, something like:, (*6)
return [
.... (other configs before)
'Openweathermap' => [
// MANDATORY : Register API keys at openweathermap.org
'key' => 'your-sitekey',
// OPTIONAL : default lang for the plugin. Default to 'fr' if this config is not provided
'lang' => 'en',
// OPTIONAL : default units mesure for the plugin. Default to 'metric' if this config is not provided
'units' => 'metric'
]
]
Make sure that /config/app.php file is in .gitignore. The secret key must stay secret. The API Key is mandatory, if you want an API go to the openweathermap.org and follow instructions., (*7)
You can fetch weather forecast for any cities with theses 3 functions: - getWeatherByCityId - getWeatherByCityName - getWeatherByGeoloc, (*8)
For example into a shell code : for this example, a $sites table contain every information about the city (name, long/lat or weathersite_id):, (*9)
public function main()
{
// parse all sites
$sites = $this->Sites->find('all');
foreach ($sites as $site) {
$this->out('site : ' . $site->libelle);
if ($site->has('weathersite_id')) {
$data = $this->Openweathermap->getWeatherByCityId($site->weathersite_id);
} elseif ($site->has('latitude') || !$site->has('longitude')) {
$data = $this->Openweathermap->getWeatherByGeoloc($site->latitude, $site->longitude);
if ($data['success']) {
$this->Sites->associateOpenweatherSite($site->id, $data['data']['city']['id']); // $data['data']['city']['id'] will contain the id from Openweathermap of the city
}
} else {
$data = $this->Openweathermap->getWeatherByCityName($site->ville, 'FR');
if ($data['success']) {
$this->Sites->associateOpenweatherSite($site->id, $data['data']['city']['id']); // $data['data']['city']['id'] will contain the id from Openweathermap of the city
}
}
sleep(2); // for unstress the server
}
}
Everytime you fetch weatherdata, the component check if the request is more than 6 hours old, otherwise the component will return weatherdata from database instead of updating it., (*10)
Don't use XML or HTML format, it's not implemented for the moment, (*11)
I'm coding a Weather Helper which can display icon (css) or image from weather information., (*12)
Sorry for my very bad english (i'm a french guy), if you see some errors, please do a pull request. If you want more informations, you can ask your questions into the issues section of this page., (*13)
you can send me a mail at cyberbobjr(at)yahoo(dot)com, (*14)
Regards, (*15)
Openweathermap plugin for CakePHP
MIT
cakephp openweathermap