PHPackages                             pudongping/weather - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. pudongping/weather

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

pudongping/weather
==================

A weather SDK.

1.0.0(5y ago)012MITPHP

Since Mar 17Pushed 5y ago1 watchersCompare

[ Source](https://github.com/pudongping/weather)[ Packagist](https://packagist.org/packages/pudongping/weather)[ RSS](/packages/pudongping-weather/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (2)Used By (0)

Weather
=======

[](#weather)

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

安装
--

[](#安装)

```
$ composer require pudongping/weather -vvv
```

配置
--

[](#配置)

在使用本扩展之前，你需要去 [高德开放平台](https://lbs.amap.com/dev/id/newuser) 注册账号，然后创建应用，获取应用的 API Key。

使用
--

[](#使用)

```
use Pudongping\Weather\Weather;

$key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx';

$weather = new Weather($key);
```

### 获取实时天气

[](#获取实时天气)

```
$response = $weather->getLiveWeather('上海');
```

返回值示例：

```
{
    "status": "1",
    "count": "1",
    "info": "OK",
    "infocode": "10000",
    "lives": [
        {
            "province": "上海",
            "city": "上海市",
            "adcode": "310000",
            "weather": "阴",
            "temperature": "10",
            "winddirection": "东",
            "windpower": "≤3",
            "humidity": "73",
            "reporttime": "2021-03-17 09:31:44"
        }
    ]
}
```

### 获取近期天气预报

[](#获取近期天气预报)

```
$response = $weather->getForecastsWeather('上海');

```

返回值示例：

```
{
    "status": "1",
    "count": "1",
    "info": "OK",
    "infocode": "10000",
    "forecasts": [
        {
            "city": "上海市",
            "adcode": "310000",
            "province": "上海",
            "reporttime": "2021-03-17 10:03:15",
            "casts": [
                {
                    "date": "2021-03-17",
                    "week": "3",
                    "dayweather": "小雨",
                    "nightweather": "小雨",
                    "daytemp": "12",
                    "nighttemp": "9",
                    "daywind": "东北",
                    "nightwind": "东北",
                    "daypower": "4",
                    "nightpower": "4"
                },
                {
                    "date": "2021-03-18",
                    "week": "4",
                    "dayweather": "小雨",
                    "nightweather": "小雨",
                    "daytemp": "13",
                    "nighttemp": "10",
                    "daywind": "东",
                    "nightwind": "东",
                    "daypower": "4",
                    "nightpower": "4"
                },
                {
                    "date": "2021-03-19",
                    "week": "5",
                    "dayweather": "小雨",
                    "nightweather": "小雨",
                    "daytemp": "14",
                    "nighttemp": "9",
                    "daywind": "北",
                    "nightwind": "北",
                    "daypower": "4",
                    "nightpower": "4"
                },
                {
                    "date": "2021-03-20",
                    "week": "6",
                    "dayweather": "阴",
                    "nightweather": "阴",
                    "daytemp": "15",
                    "nighttemp": "8",
                    "daywind": "西北",
                    "nightwind": "西北",
                    "daypower": "4",
                    "nightpower": "4"
                }
            ]
        }
    ]
}
```

### 获取 XML 格式返回值

[](#获取-xml-格式返回值)

以上两个方法第二个参数为返回值类型，可选 `json` 与 `xml`，默认 `json`：

```
$response = $weather->getLiveWeather('上海', 'xml');
```

返回值示例：

```

    1
    1
    OK
    10000

            上海
            上海市
            310000
            小雨
            10
            东北
            ≤3
            73
            2021-03-17 10:03:15

```

### 参数说明

[](#参数说明)

```
array | string   getLiveWeather(string $city, string $format = 'json')
array | string   getForecastsWeather(string $city, string $format = 'json')

```

> - `$city` - 城市名/[高德地址位置 adcode](https://lbs.amap.com/api/webservice/guide/api/district)，比如：“上海” 或者（adcode：310000）；
> - `$format` - 输出的数据格式，默认为 json 格式，当 format 设置为 “`xml`” 时，输出的为 XML 格式的数据。

### 在 Laravel 中使用

[](#在-laravel-中使用)

> laravel version &gt;= 8.x 或者直接使用以上通用方法，以上方法适用于任意 php 框架

执行 `artisan` 命令，发布配置文件

```
php artisan vendor:publish --provider="Pudongping\Weather\ServiceProvider"
```

在 Laravel 中使用也是同样的安装方式，配置写在 `config/weather.php` 中，或者自己在 `config` 目录下新建 `weather.php` 文件，填写以下内容亦可。

```
