Skip to content

🔥基于高德开放平台的 PHP 天气信息组件

License

Notifications You must be signed in to change notification settings

mcxzyang/weather

Repository files navigation

weather

🌈 基于高德开放平台的 PHP 天气信息组件。

Build Status

安装

$ composer require cherrybeal/weather -vvv

配置

在使用本扩展之前,你需要去 高德开放平台 注册账号,然后创建应用,获取应用的 API Key。

weather.php 里面填上 key=xxx

使用

use Cherrybeal\Weather\Weather;

$key = 'xxxxxxxx';
$weather = new Weather($key);

获取实时天气

$response = $weather->getLiveWeather('长沙');

示例:

{
    "status": "1",
    "count": "2",
    "info": "OK",
    "infocode": "10000",
    "lives": [
        {
            "province": "湖南",
            "city": "长沙市",
            "adcode": "430100",
            "weather": "",
            "temperature": "7",
            "winddirection": "西北",
            "windpower": "≤3",
            "humidity": "65",
            "reporttime": "2019-01-22 21:17:37"
        },
        {
            "province": "湖南",
            "city": "长沙县",
            "adcode": "430121",
            "weather": "",
            "temperature": "7",
            "winddirection": "西北",
            "windpower": "≤3",
            "humidity": "65",
            "reporttime": "2019-01-22 21:17:38"
        }
    ]
}

获取近期天气预报

$response = $weather->getForecastsWeather('长沙', 'all');

示例:

{
    "status": "1",
    "count": "2",
    "info": "OK",
    "infocode": "10000",
    "forecasts": [
        {
            "city": "长沙市",
            "adcode": "430100",
            "province": "湖南",
            "reporttime": "2019-01-22 21:17:38",
            "casts": [
                {
                    "date": "2019-01-22",
                    "week": "2",
                    "dayweather": "",
                    "nightweather": "",
                    "daytemp": "14",
                    "nighttemp": "3",
                    "daywind": "东南",
                    "nightwind": "东南",
                    "daypower": "≤3",
                    "nightpower": "≤3"
                },
                {
                    "date": "2019-01-23",
                    "week": "3",
                    "dayweather": "",
                    "nightweather": "",
                    "daytemp": "15",
                    "nighttemp": "3",
                    "daywind": "",
                    "nightwind": "",
                    "daypower": "≤3",
                    "nightpower": "≤3"
                },
                {
                    "date": "2019-01-24",
                    "week": "4",
                    "dayweather": "多云",
                    "nightweather": "多云",
                    "daytemp": "16",
                    "nighttemp": "4",
                    "daywind": "",
                    "nightwind": "",
                    "daypower": "≤3",
                    "nightpower": "≤3"
                },
                {
                    "date": "2019-01-25",
                    "week": "5",
                    "dayweather": "多云",
                    "nightweather": "多云",
                    "daytemp": "12",
                    "nighttemp": "6",
                    "daywind": "",
                    "nightwind": "",
                    "daypower": "≤3",
                    "nightpower": "≤3"
                }
            ]
        },
        {
            "city": "长沙县",
            "adcode": "430121",
            "province": "湖南",
            "reporttime": "2019-01-22 21:17:38",
            "casts": [
                {
                    "date": "2019-01-22",
                    "week": "2",
                    "dayweather": "",
                    "nightweather": "",
                    "daytemp": "13",
                    "nighttemp": "3",
                    "daywind": "东南",
                    "nightwind": "东南",
                    "daypower": "≤3",
                    "nightpower": "≤3"
                },
                {
                    "date": "2019-01-23",
                    "week": "3",
                    "dayweather": "",
                    "nightweather": "",
                    "daytemp": "15",
                    "nighttemp": "3",
                    "daywind": "",
                    "nightwind": "",
                    "daypower": "≤3",
                    "nightpower": "≤3"
                },
                {
                    "date": "2019-01-24",
                    "week": "4",
                    "dayweather": "多云",
                    "nightweather": "多云",
                    "daytemp": "16",
                    "nighttemp": "4",
                    "daywind": "",
                    "nightwind": "",
                    "daypower": "≤3",
                    "nightpower": "≤3"
                },
                {
                    "date": "2019-01-25",
                    "week": "5",
                    "dayweather": "多云",
                    "nightweather": "多云",
                    "daytemp": "12",
                    "nighttemp": "6",
                    "daywind": "",
                    "nightwind": "",
                    "daypower": "≤3",
                    "nightpower": "≤3"
                }
            ]
        }
    ]
}

获取 XML 格式返回值

第二个参数为返回值类型,可选 jsonxml, 默认 json:

$response = $weather->getLiveWeather('长沙', 'xml');

示例:

<response>
    <status>1</status>
    <count>1</count>
    <info>OK</info>
    <infocode>10000</infocode>
    <lives type="list">
        <live>
            <province>广东</province>
            <city>深圳市</city>
            <adcode>440300</adcode>
            <weather>中雨</weather>
            <temperature>27</temperature>
            <winddirection>西南</winddirection>
            <windpower>5</windpower>
            <humidity>94</humidity>
            <reporttime>2018-08-21 16:00:00</reporttime>
        </live>
    </lives>
</response>

参数说明

array | string getLiveWeather(string $city, string $format = 'json')
array | string getForecastsWeather(string $city, string $format = 'json')
  • $city - 城市名,比如:"长沙"
  • $format - 输出的格式类型,默认为 json 格式,当 output 为 "xml" 时,输出为 xml 格式的数据

在 Laravel 中使用

在 Laravel 中使用也是同样的安装方式,配置写在 config/services.php

'weather' => [
        'key' => env('WEATHER_API_KEY'),
    ],

然后在 .env 中配置 WEATHER_API_KEY:

WEATHER_API_KEY=xxxxxxxxxxxxxxxxxxxxx

可以用两种方式来获取 Cherrybeal\Weather\Weather 实例:

方法参数注入

public function edit(Weather $weather) 
{
    $response = $weather->getLiveWeather('深圳');
}

服务名访问

public function edit() 
{
    $response = app('weather')->getLiveWeather('深圳');
}

参考

License

MIT

About

🔥基于高德开放平台的 PHP 天气信息组件

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages