dwr/openweather-bundle

Symfony3 包封装 Open Weather API。

2.0 2017-03-01 14:55 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:30:33 UTC


README

Latest Stable Version Total Downloads Software License Build Status Scrutinizer Code Quality Coverage Status composer.lock

DwrOpenWeatherBundle

DwrOpenWeatherBundle 是一个简单的 Open Weather API 封装包。
为了开始,请首先生成您的个人 ApiKey。
您可以在这里完成。

安装

有了 ApiKey,安装只需三个简单步骤

  1. 使用 composer 下载 DwrOpenWeatherBundle
  2. 启用 Bundle
  3. 将路由添加到 routing.yml,以便在浏览器中打开示例

第一步:使用 composer 下载 DwrOpenWeatherBundle

将 DwrOpenWeatherBundle 版本 2.0 添加到 composer.json 并运行 'composer update'

{
    "require": {
        "dwr/openweather-bundle": "2.0"
    }
}

或者运行以下命令下载 Bundle

$ php composer.phar require dwr/openweather-bundle

Composer 会将 Bundle 安装到您的项目目录 vendor/dwr/openweather-bundle 中。

第二步:启用 Bundle 并将 APIKEY 添加到 config.yml

在 kernel 中启用 Bundle

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Dwr\OpenWeatherBundle\DwrOpenWeatherBundle(),
    );
}

APIKEY 添加到您的 config.yml

    dwr_open_weather:
        api_key: paste-your-api-key-here

第三步:将路由添加到 routing.yml,以便在浏览器中打开示例

dwr_open_weather:
    resource: "@DwrOpenWeatherBundle/Controller/"
    type:     annotation

恭喜!您现在可以在 symfony 应用程序中展示天气小部件了。
您可以在 yours-application-url/weather-basic-small 上找到 weather-basic-small 的示例。

用法

获取天气

在您的控制器中

    public function indexAction()
    {
        $openWeather = $this->get('dwr_open_weather');
        $weather = $openWeather->setType('Weather')->getByCityName('London');
        var_dump($weather);
    }

您可以通过使用以下方式从 OpenWeather API 获取天气

  • getByCityName('London')
  • getByCityId('2643743')
    城市 ID 列表 city.list.json.gz 可以从 这里 下载
  • getByGeographicCoordinates(-0.12574, 51.50853)

获取预报

在您的控制器中

    public function indexAction()
    {
        $openWeather = $this->get('dwr_open_weather');
        $forecast = $openWeather->setType('Forecast')->getByCityName('London');
        var_dump($forecast);
    }

您可以通过使用以下方式从 OpenWeather API 获取预报

  • getByCityName('London')
  • getByCityId('2643743')
    城市 ID 列表 city.list.json.gz 可以从 这里 下载
  • getByGeographicCoordinates(-0.12574, 51.50853)

示例

花点时间查看示例。也许您会找到您喜欢的解决方案。

为了在本地运行示例

  1. 在 routing.yml (app/config/routing.yml) 中添加路由
    dwr_open_weather:
        resource: "@DwrOpenWeatherBundle/Controller/"
        type:     annotation
  • 之后,示例应该可以在以下 URL 地址中访问
    • yours-application-url/weather-basic-small
    • yours-application-url/weather-basic-medium
    • yours-application-url/weather-basic-large
    • yours-application-url/forecast-chart
    • yours-application-url/forecast-basic

具体看起来是什么样子,您可以在下面查看。

weather-basic-small

weather-basic-small

示例来自:Dwr\OpenWeatherBundle\Controller\DefaultController.php
操作: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,
        ));
    }

weather-basic-medium

weather-basic-medium

示例来自:Dwr\OpenWeatherBundle\Controller\DefaultController.php
操作: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,
        ));
    }

weather-basic-large

weather-basic-large

示例来自:Dwr\OpenWeatherBundle\Controller\DefaultController.php
操作: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,
        ));
    }

forecast-chart

forecast-chart

示例来自:Dwr\OpenWeatherBundle\Controller\DefaultController.php
操作: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,
        ));
    }

forecast-basic

forecast-basic

示例来自:Dwr\OpenWeatherBundle\Controller\DefaultController.php
操作: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
        ));
    }

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件