cakephp-fr/openweathermap

CakePHP 的 Openweathermap 插件

安装: 14

依赖: 0

建议: 0

安全: 0

星标: 0

关注者: 4

分支: 0

开放问题: 0

类型:cakephp-plugin

dev-master 2016-02-16 09:35 UTC

This package is auto-updated.

Last update: 2024-09-21 23:05:34 UTC


README

Build Status License Total Downloads

安装

您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。安装 composer 包的推荐方式是

composer require cakephp-fr/openweathermap:dev-master

使用以下方法加载插件

bin/cake plugin load Openweathermap

或者手动在您的 boostrap.php 文件中添加 CakePlugin::load('Localized')

使用插件中的初始迁移文件在 'Migrations' 目录中创建 2 个表:weatherdatas 和 weathersites

bin/cake migrations migrate -p Openweathermap

配置

要配置插件,您可以将 openweathermap 配置添加到 config/app.php 文件中,例如

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'
    ]
]

确保 /config/app.php 文件包含在 .gitignore 文件中。密钥必须保密。API 密钥是必须的,如果您想要 API 密钥,请访问 openweathermap.org 并按照说明操作。

使用

您可以使用以下 3 个函数获取任何城市的天气预报

  • getWeatherByCityId
  • getWeatherByCityName
  • getWeatherByGeoloc

例如在 shell 代码中:在这个例子中,一个 $sites 表包含关于城市(名称、经纬度或 weathersite_id)的每个信息

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
    }
}

每次您获取天气数据时,组件都会检查请求是否超过 6 小时,否则组件将返回数据库中的天气数据而不是更新它。

不要使用 XML 或 HTML 格式,目前尚未实现

未来

我正在编写一个天气助手,它可以显示来自天气信息的图标(CSS)或图像。

结束

我为我的英语非常糟糕表示歉意(我是一个法国人),如果您看到任何错误,请发起一个 pull request。如果您想了解更多信息,可以在本页的问题部分提出问题。

您可以通过 cyberbobjr(at)yahoo(dot)com 发送邮件给我

问候