canducci/weather

Canducci 天气

v0.1.0 2015-11-16 23:50 UTC

This package is auto-updated.

Last update: 2024-08-29 04:31:03 UTC


README

天气预报

Canducci Weather

Build Status Packagist Packagist Packagist

安装

配置

对于Laravel应用程序,按照以下方式配置

在您的composer.json中添加以下键

"canducci/weather": "0.1.*"

现在在您的控制台中执行以下命令

$ composer update

打开config/app.php文件,并在providers配置中添加以下行

Canducci\Weather\Providers\WeatherServiceProvider::class

为了使别名(门面)工作,请在aliases配置中添加以下行

'Weather'   => Canducci\Weather\Facades\Weather::class

好了,现在您只需在您的Laravel 5.*应用程序中使用它即可。

###在Laravel中使用

####城市搜索

使用cities函数根据描述信息获取城市

$items = cities('Sao Paulo');
return $items->getJson();

在以下JavaScript注解的return中将有以下结果

[
    {
        "Id": 244,
        "Name": "S\u00e3o Paulo",
        "Uf": "SP"
    },
    {
        "Id": 5019,
        "Name": "S\u00e3o Paulo das Miss\u00f5es",
        "Uf": "RS"
    },
    {
        "Id": 5020,
        "Name": "S\u00e3o Paulo de Oliven\u00e7a",
        "Uf": "AM"
    },
    {
        "Id": 5021,
        "Name": "S\u00e3o Paulo do Potengi",
        "Uf": "RN"
    }
]

这些找到的城市都有一个ID,它是通过另一个函数来获取天气预兆的,也就是说,圣保罗的Id = 244,那么

$item = forecast(244);
return $item->getJson();

它将在JavaScript注解中返回此模型

{
    "Id": 244,
    "City": "S\u00e3o Paulo",
    "Uf": "SP",
    "Update": {
        "Short": "16\/11\/2015",
        "Date": {
            "date": "2015-11-16 23:00:41.000000",
            "timezone_type": 3,
            "timezone": "UTC"
        }
    },
    "Dates": [
        {
            "Date": {
                "Short": "17\/11\/2015",
                "Date": {
                    "date": "2015-11-17 23:00:41.000000",
                    "timezone_type": 3,
                    "timezone": "UTC"
                }
            },
            "Iuv": 13,
            "Min": 16,
            "Max": 26,
            "Time": {
                "Sigla": "ci",
                "Description": "Chuvas Isoladas"
            }
        },
        {
            "Date": {
                "Short": "18\/11\/2015",
                "Date": {
                    "date": "2015-11-18 23:00:42.000000",
                    "timezone_type": 3,
                    "timezone": "UTC"
                }
            },
            "Iuv": 13,
            "Min": 20,
            "Max": 29,
            "Time": {
                "Sigla": "pnt",
                "Description": "Pancadas de Chuva a Noite"
            }
        },
        {
            "Date": {
                "Short": "19\/11\/2015",
                "Date": {
                    "date": "2015-11-19 23:00:42.000000",
                    "timezone_type": 3,
                    "timezone": "UTC"
                }
            },
            "Iuv": 13,
            "Min": 20,
            "Max": 31,
            "Time": {
                "Sigla": "pt",
                "Description": "Pancadas de Chuva a Tarde"
            }
        },
        {
            "Date": {
                "Short": "20\/11\/2015",
                "Date": {
                    "date": "2015-11-20 23:00:42.000000",
                    "timezone_type": 3,
                    "timezone": "UTC"
                }
            },
            "Iuv": 13,
            "Min": 20,
            "Max": 30,
            "Time": {
                "Sigla": "pc",
                "Description": "Pancadas de Chuva"
            }
        }
    ]
}	

也可以通过门面(别名)以这种方式使用

命名空间

use Canducci\Weather\Facades\Weather;
use Canducci\Weather\ForecastDay;

代码

Weather::cities('Sao Paulo'); 
Weather::forecast(244); //4 datas de previsão (padrão)
Weather::forecast(244, ForecastDay::Day4); //4 datas de previsão
Weather::forecast(244, ForecastDay::Day7); //7 datas de previsão

Weather::cities('Sao Paulo')返回一个具有以下格式的城市集合

代码

$items = Weather::cities('Sao Paulo');
foreach ($items as $key => $value) 
{
    echo sprintf('<p>%s %s %s</p>',
            $value->getId(), 
            $value->getName(), 
            $value->getUf());
}

结果

244 São Paulo SP
5019 São Paulo das Missões RS
5020 São Paulo de Olivença AM
5021 São Paulo do Potengi RN

Weather::forecast(244, ForecastDay::Day4)将返回请求的天气预兆

代码

$item = Weather::forecast(244, ForecastDay::Day4);    

//Dados da cidades
echo sprintf('<p>Id: %s</p><p> Cidade: %s</p><p> Uf: %s</p>
              <p>Data última atualização: %s</p>',
            $item->getId(), 
            $item->getCity(), 
            $item->getUf(), 
            $item->getUpdated()->format('d/m/Y'));

//Dados da previsões
foreach ($item->getDates() as $key => $value) 
{
    echo sprintf('<p>Data: %s</p><p>IUV: %s</p><p>Minima: %s</p>
                  <p>Maxima: %s</p><p>Status: %s - %s</p>', 
        $value->getDate()->format('d/m/Y'),
        $value->getIuv(), 
        $value->getMin(),
        $value->getMax(),
        $value->getTime()->getSigla(),
        $value->getTime()->getDescription());
}

结果

####城市ID:244的数据

Cidade: São Paulo

Uf: SP

Data última atualização: 16/11/2015

####预兆数据

Data: 17/11/2015

IUV: 13

Minima: 16

Maxima: 26

Status: ci - Chuvas Isoladas


Data: 18/11/2015

IUV: 13

Minima: 20

Maxima: 29

Status: pnt - Pancadas de Chuva a Noite


Data: 19/11/2015

IUV: 13

Minima: 20

Maxima: 31

Status: pt - Pancadas de Chuva a Tarde


Data: 20/11/2015

IUV: 13

Minima: 20

Maxima: 30

Status: pc - Pancadas de Chuva

###在没有框架的情况下使用

此包在Laravel之外也能正常工作,也就是说,它独立于框架。要安装,请创建一个这样的composer.json

{    
    "require": {    
        "canducci/weather":"0.1.*"       
    }
}

运行安装程序$ composer update,安装完成后,创建一个名为index.php(或您喜欢的任何名称)的文件,并添加来自vendor文件夹的autoload.phprequire 'vendor/autoload.php';)。

要使用它,请执行以下操作

$weather = new Canducci\Weather\Weather(new Canducci\Weather\WeatherClient());

//busca de cidades
echo $weather->cities('Sao Paulo')->getJson();
//busca da previsão do tempo
echo $weather->forecast(244)->getJson();

完整代码

<?php
	require 'vendor/autoload.php';
	
	$weather = new Canducci\Weather\Weather(new Canducci\Weather\WeatherClient());
	
	//busca de cidades
	echo $weather->cities('Sao Paulo')->getJson();
	//busca da previsão do tempo
	echo $weather->forecast(244)->getJson();