srcat/weather-underground

用于与 Weather Underground API 一起工作的 Symfony2 扩展包。

dev-master 2013-12-17 13:19 UTC

This package is not auto-updated.

Last update: 2024-09-22 02:57:12 UTC


README

Symfony2 扩展包,用于与 Weather Underground API 一起工作

简介

此扩展包允许将 Weather Underground API 与您的 Symfony 2 项目集成。

安装

将此扩展包和 kriswallsmith Buzz 库添加到您的项目中的 composer.json 文件的 require 部分

...
    {
        "kriswallsmith/buzz": "0.7",
        "suncat/weather-underground": "dev-master"
    }
...

将此扩展包添加到您的应用程序内核中

//app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new SunCat\WeatherUndergroundBundle\WeatherUndergroundBundle(),
        // ...
    );
}

在您的 YAML 配置中配置 weather_underground 服务

#app/config/config.yml
weather_underground:
    apikey: your_api_key

完整配置

#app/config/config.yml
weather_underground:
    apikey: your_api_key
    format: json                                                # json/xml
    host_data_features: http://api.wunderground.com             # default: http://api.wunderground.com
    host_autocomlete: http://autocomplete.wunderground.com      # default: http://autocomplete.wunderground.com

使用示例

<?php
    // src/Acme/YourBundle/Command/WUForecastCommand.php
    class WUForecastCommand extends ContainerAwareCommand
    {
        /**
        * @see Command
        */
       protected function configure()
       {
           $this
               ->setName('weather_underground:forecast')
               ->setDescription('Import data forecast by cities from api.wunderground.com')
               ->addOption('city', 0, InputOption::VALUE_REQUIRED, 'City name')
               ->setHelp(<<<EOF
   The <info>weather_underground:forecast</info> command import forecast data.

   <info>php app/console weather_underground:forecast --city=Moscow</info>

   EOF
               );
       }

        /**
         * {@inheritdoc}
         */
        protected function execute(InputInterface $input, OutputInterface $output)
        {
            $wuApi = $this->getContainer()->get('weather_underground.data_features');
            $wuApi->setFeatures(array('forecast'));
            $cityName = $input->getOption('city');

            if(!$cityName){
                throw new \Exception('Enter city name');
            }

            $wuApi->setQuery('/Russia/' . $cityName, true);
            $data = $wuApi->getData();

            //
            // put your code
            //
        }

    }

运行命令

    php app/console weather_underground:forecast --city=Moscow

数据功能示例

    $wuApi->setRequestData(
        array('forecast', 'geolookup'),     // Features
        array('lang' => 'RU'),              // Settings
        'Russia/Moscow'                     // Query
    );
    $data = $wuApi->getData();
    $wuApi->setFeatures(array('forecast', 'geolookup'));     // Features
    $wuApi->setQuery('Russia/Moscow', true);                 // Query
    $data = $wuApi->getData();

自动完成示例

    $wuAutocomplete = $this->getContainer()->get('weather_underground.autocomplete');
    $wuAutocomplete->setOptions(array('c' => 'RU', 'cities' => 1, 'query' => 'Mosc'));
    $data = $wuAutocomplete->getData();

Weather Underground API 文档

数据功能

自动完成 API