2017 © Pedro Peláez
 

cakephp-plugin openweathermap

Openweathermap plugin for CakePHP

image

cakephp-fr/openweathermap

Openweathermap plugin for CakePHP

  • Tuesday, February 16, 2016
  • by cake17
  • Repository
  • 4 Watchers
  • 0 Stars
  • 11 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 10 % Grown

The README.md

Openweathermap plugin for CakePHP v3

Build Status License Total Downloads, (*1)

Installation

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

Configuration

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)

Using

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)

Future

I'm coding a Weather Helper which can display icon (css) or image from weather information., (*12)

End

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)

The Versions

16/02 2016

dev-master

9999999-dev https://github.com/cakephp-fr/openweathermap

Openweathermap plugin for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

cakephp openweathermap