PHPackages                             yulinzhihou/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. yulinzhihou/weather

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

yulinzhihou/weather
===================

A Weather SDK for AMAP

10PHP

Since Mar 8Pushed 3y ago1 watchersCompare

[ Source](https://github.com/yulinzhihou/weather)[ Packagist](https://packagist.org/packages/yulinzhihou/weather)[ RSS](/packages/yulinzhihou-weather/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependenciesVersions (2)Used By (0)

 weather
=========

[](#-weather-)

 🌈 基于高德地图接口的 PHP 天气信息组件包 🌈

[![Tests](https://github.com/yulinzhihou/weather/actions/workflows/tests.yml/badge.svg)](https://github.com/yulinzhihou/weather/actions/workflows/tests.yml)[![StyleCI build status](https://camo.githubusercontent.com/c5695f7bb50eb85bdc1c10531aadc44ec4b25af030697c251b19faa8df292b0d/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3631303132343737342f736869656c64)](https://camo.githubusercontent.com/c5695f7bb50eb85bdc1c10531aadc44ec4b25af030697c251b19faa8df292b0d/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3631303132343737342f736869656c64)

安装
--

[](#安装)

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

配置
--

[](#配置)

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

使用
--

[](#使用)

```
use Beans\Weather\Weather;

$key = "XXXXXXXXXXXXX";

$weather = new Weather($key);

$weather->getWeather('长沙');
```

### 获取实时天气

[](#获取实时天气)

```
$weather->getWeather('长沙');
```

示例

```
{
    "status": "1",
    "count": "2",
    "info": "OK",
    "infocode": "10000",
    "lives": [
        {
            "province": "湖南",
            "city": "长沙市",
            "adcode": "430100",
            "weather": "晴",
            "temperature": "18",
            "winddirection": "南",
            "windpower": "≤3",
            "humidity": "41",
            "reporttime": "2023-03-06 10:01:29",
            "temperature_float": "18.0",
            "humidity_float": "41.0"
        },
        {
            "province": "湖南",
            "city": "长沙县",
            "adcode": "430121",
            "weather": "晴",
            "temperature": "18",
            "winddirection": "北",
            "windpower": "≤3",
            "humidity": "41",
            "reporttime": "2023-03-06 10:01:29",
            "temperature_float": "18.0",
            "humidity_float": "41.0"
        }
    ]
}
```

### 获取近期天气预报

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

```
$weather->getWeather('长沙','all');
```

示例

```
{
    "status": "1",
    "count": "2",
    "info": "OK",
    "infocode": "10000",
    "forecasts": [
        {
            "city": "长沙市",
            "adcode": "430100",
            "province": "湖南",
            "reporttime": "2023-03-06 11:31:28",
            "casts": [
                {
                    "date": "2023-03-06",
                    "week": "1",
                    "dayweather": "晴",
                    "nightweather": "晴",
                    "daytemp": "26",
                    "nighttemp": "10",
                    "daywind": "东南",
                    "nightwind": "东南",
                    "daypower": "≤3",
                    "nightpower": "≤3",
                    "daytemp_float": "26.0",
                    "nighttemp_float": "10.0"
                },
                {
                    "date": "2023-03-07",
                    "week": "2",
                    "dayweather": "小雨",
                    "nightweather": "小雨",
                    "daytemp": "20",
                    "nighttemp": "15",
                    "daywind": "东",
                    "nightwind": "东",
                    "daypower": "≤3",
                    "nightpower": "≤3",
                    "daytemp_float": "20.0",
                    "nighttemp_float": "15.0"
                },
                {
                    "date": "2023-03-08",
                    "week": "3",
                    "dayweather": "小雨",
                    "nightweather": "多云",
                    "daytemp": "22",
                    "nighttemp": "13",
                    "daywind": "东南",
                    "nightwind": "东南",
                    "daypower": "≤3",
                    "nightpower": "≤3",
                    "daytemp_float": "22.0",
                    "nighttemp_float": "13.0"
                },
                {
                    "date": "2023-03-09",
                    "week": "4",
                    "dayweather": "晴",
                    "nightweather": "晴",
                    "daytemp": "24",
                    "nighttemp": "15",
                    "daywind": "东",
                    "nightwind": "东",
                    "daypower": "≤3",
                    "nightpower": "≤3",
                    "daytemp_float": "24.0",
                    "nighttemp_float": "15.0"
                }
            ]
        },
        {
            "city": "长沙县",
            "adcode": "430121",
            "province": "湖南",
            "reporttime": "2023-03-06 11:31:28",
            "casts": [
                {
                    "date": "2023-03-06",
                    "week": "1",
                    "dayweather": "晴",
                    "nightweather": "晴",
                    "daytemp": "26",
                    "nighttemp": "10",
                    "daywind": "东南",
                    "nightwind": "东南",
                    "daypower": "≤3",
                    "nightpower": "≤3",
                    "daytemp_float": "26.0",
                    "nighttemp_float": "10.0"
                },
                {
                    "date": "2023-03-07",
                    "week": "2",
                    "dayweather": "小雨",
                    "nightweather": "小雨",
                    "daytemp": "20",
                    "nighttemp": "15",
                    "daywind": "东",
                    "nightwind": "东",
                    "daypower": "≤3",
                    "nightpower": "≤3",
                    "daytemp_float": "20.0",
                    "nighttemp_float": "15.0"
                },
                {
                    "date": "2023-03-08",
                    "week": "3",
                    "dayweather": "小雨",
                    "nightweather": "多云",
                    "daytemp": "22",
                    "nighttemp": "13",
                    "daywind": "东南",
                    "nightwind": "东南",
                    "daypower": "≤3",
                    "nightpower": "≤3",
                    "daytemp_float": "22.0",
                    "nighttemp_float": "13.0"
                },
                {
                    "date": "2023-03-09",
                    "week": "4",
                    "dayweather": "晴",
                    "nightweather": "晴",
                    "daytemp": "24",
                    "nighttemp": "15",
                    "daywind": "东",
                    "nightwind": "东",
                    "daypower": "≤3",
                    "nightpower": "≤3",
                    "daytemp_float": "24.0",
                    "nighttemp_float": "15.0"
                }
            ]
        }
    ]
}
```

### 获取 XML 格式返回值

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

第三个参数为返回值类型，可选 `json` 与 `xml` ，默认 `json`

```
$response = $weather->getWeather('长沙','all','xml');
```

示例

```
\n

	1
	2
	OK
	10000

			\u6e56\u5357
			\u957f\u6c99\u5e02
			430100
			\u6674
			20
			\u897f\u5357
			\u22643
			36
			2023-03-06 11:31:28
			20.0
			36.0

			\u6e56\u5357
			\u957f\u6c99\u53bf
			430121
			\u6674
			20
			\u5357
			4
			36
			2023-03-06 11:31:28
			20.0
			36.0

```

### 参数说明

[](#参数说明)

```
array|string getWeather(string $city,string $type = 'base',string $format = 'json')
```

- `$city` - 城市名，比如："长沙"
- `$type` - 返回内容类型：`base`：返回实况天气 / `all`：返回天气预报
- `$format` - 输出的数据格式，默认为 `json` 格式，当 output 设置为 "`xml`" 时，输出的为 XML 格式的数据。

### 在 Laravel 中使用

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

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

```
.
.
.

'weather'   => [
    'key' => env('WEATHER_API_KEY')
];
.
.
.
```

然后在 `.env` 中配置 `WEATHER_API_KEY` :

```
WEATHER_API_KEY=XXXXXXXXXX

```

可以用两种方式来获取 `Beans\Weather\Weather` 实例：

**方法注入**

```
.
.
.

public function edit(Weather $weather)
{
    $response = $weather->getWeather('长沙');
}
.
.
.
```

**服务名访问**

```
public function edit()
{
    $response = app('weather')->getWeather('长沙');
}
```

参考
--

[](#参考)

- [高德开放平台天气接口](https://lbs.amap.com/api/webservice/guide/api/weatherinfo/)

贡献
--

[](#贡献)

您可以通过以下三种方式之一做出贡献：

1. File bug reports using the [issue tracker](https://github.com/beans/weather/issues).
2. Answer questions or fix bugs on the [issue tracker](https://github.com/beans/weather/issues).
3. Contribute new features or update the wiki.

\_The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable.

License
-------

[](#license)

MIT

###  Health Score

14

—

LowBetter than 2% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity2

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity25

Early-stage or recently created project

 Bus Factor1

Top contributor holds 90% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/aa9d261931ebe8d41906a6789e20cabd1efac0d8186c54b1d0a7109e461d0bdf?d=identicon)[yulinzhihou](/maintainers/yulinzhihou)

---

Top Contributors

[![yulinzhihou](https://avatars.githubusercontent.com/u/33444126?v=4)](https://github.com/yulinzhihou "yulinzhihou (9 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (1 commits)")

### Embed Badge

![Health badge](/badges/yulinzhihou-weather/health.svg)

```
[![Health](https://phpackages.com/badges/yulinzhihou-weather/health.svg)](https://phpackages.com/packages/yulinzhihou-weather)
```

###  Alternatives

[afragen/git-updater-lite

A simple class to integrate with Git Updater for standalone plugin/theme updates.

201.7k](/packages/afragen-git-updater-lite)[aluguest/ical-easy-reader

An easy to understood class, loads a "ics" format string and returns an array with the traditional iCal fields.

122.7k](/packages/aluguest-ical-easy-reader)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
