ctlynl/weather

高德API获取天气接口

v2.0 2023-06-21 05:46 UTC

This package is auto-updated.

Last update: 2025-05-21 13:53:22 UTC


README

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

Tests

安装

$ composer require ctlynl/weather:v2.0 -vvv

配置

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

使用

use Overtrue\Weather\Weather;

$key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx';

$weather = new Weather($key);

参数说明

1、$city - 城市名,比如:“深圳”;
3、$format - 输出的数据格式,默认为 json 格式,当 output 设置为 “xml” 时,输出的为 XML 格式的数据。

获取实时天气

$response = $weather->getLiveWeather('深圳');

获取近期天气预报

$response = $weather->getForecastsWeather('深圳');

获取 XML 格式返回值(默认json)

$response = $weather->getLiveWeather('深圳', 'xml');

LICENSE MIT