基于高德开放API获取天气预报SDK.
- crackersw/weater
composer require crackersw/chinaumspay -vvv
在使用本扩展之前,你需要去高德开放平台注册账号,然后创建应用,获取应用的API Key。
use CrackerSw\Weather\ChinaUmsPay;
$key = "xxxxxxxxxxxxxxxxxxxxx";
$weather = new ChinaUmsPay($key);
$response = $weather->getLiveWeather("无锡");
返回示例
{
"status": "1",
"count": "1",
"info": "OK",
"infocode": "10000",
"lives": [
{
"province": "江苏",
"city": "无锡市",
"adcode": "320200",
"weather": "多云",
"temperature": "28",
"winddirection": "西",
"windpower": "≤3",
"humidity": "40",
"reporttime": "2022-04-22 13:37:16"
}
]
}
$response = $weather->getForecastsWeather("无锡");
返回示例
{
"status": "1",
"count": "1",
"info": "OK",
"infocode": "10000",
"forecasts": [
{
"city": "无锡市",
"adcode": "320200",
"province": "江苏",
"reporttime": "2022-04-22 13:37:16",
"casts": [
{
"date": "2022-04-22",
"week": "5",
"dayweather": "多云",
"nightweather": "中雨",
"daytemp": "30",
"nighttemp": "19",
"daywind": "南",
"nightwind": "南",
"daypower": "≤3",
"nightpower": "≤3"
},
{
"date": "2022-04-23",
"week": "6",
"dayweather": "小雨",
"nightweather": "多云",
"daytemp": "26",
"nighttemp": "16",
"daywind": "东南",
"nightwind": "东南",
"daypower": "≤3",
"nightpower": "≤3"
},
{
"date": "2022-04-24",
"week": "7",
"dayweather": "多云",
"nightweather": "多云",
"daytemp": "29",
"nighttemp": "18",
"daywind": "东南",
"nightwind": "东南",
"daypower": "≤3",
"nightpower": "≤3"
},
{
"date": "2022-04-25",
"week": "1",
"dayweather": "暴雨",
"nightweather": "小雨",
"daytemp": "28",
"nighttemp": "21",
"daywind": "东南",
"nightwind": "东南",
"daypower": "≤3",
"nightpower": "≤3"
}
]
}
]
}
第三个参数为返回值类型,可选json
与xml
,默认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>320200</adcode>
<weather>多云</weather>
<temperature>28</temperature>
<winddirection>西</winddirection>
<windpower>≤3</windpower>
<humidity>40</humidity>
<reporttime>2022-04-22 13:37:16</reporttime>
</live>
</lives>
</response>
array|string getWeather(string $city,$string $type = "base",string $format = "json")
$city
-城市名,比如:"无锡";$type
-返回内容类型:base
:返回实况天气/all
:返回预报天气;$format
-输出数据格式,默认为json
格式,当output设置为xml
时,输出的为XML格式数据;
在Laravel中使用也是同样的安装方式,配置写在config/services.php中
.
.
.
"weather" => [
"key" => env("WEATHER_API_KEY"),
],
然后在.env
中配置WEATHER_API_KEY
:
WEATHER_API_KEY=xxxxxxxxxxxxxxxxxxxxx
可以用两种方式来获取CrackerSw\Weather\Weather
实例:
方法参数注入
.
.
.
public function getWeather(Weather $weather)
{
$response = $weather->getLiveWeather('无锡');
}
.
.
.
服务名访问
.
.
.
public function getWeather(Weather $weather)
{
$response = app('weather')->getLiveWeather('无锡');
}
.
.
.
MIT