1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace icy2003\php\iapis; |
4
|
|
|
|
5
|
|
|
use icy2003\php\I; |
6
|
|
|
use icy2003\php\iapis\Api; |
7
|
|
|
use icy2003\php\ihelpers\Arrays; |
8
|
|
|
use icy2003\php\ihelpers\Http; |
9
|
|
|
use icy2003\php\ihelpers\Json; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* 气象(meteorology)接口 |
13
|
|
|
*/ |
14
|
|
|
class Meteorology extends Api |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* 获取省份代码 |
18
|
|
|
* |
19
|
|
|
* @return static |
20
|
|
|
*/ |
21
|
|
|
public function fetchProvinces() |
22
|
|
|
{ |
23
|
|
|
$this->fetchCities(''); |
24
|
|
|
$this->_toArrayCall = function ($array) { |
25
|
|
|
return Arrays::columns2($array, ['code', 'name']); |
26
|
|
|
}; |
27
|
|
|
|
28
|
|
|
return $this; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* 获取城市列表 |
33
|
|
|
* |
34
|
|
|
* @param string $provinceCode 省份代码 |
35
|
|
|
* |
36
|
|
|
* @return static |
37
|
|
|
*/ |
38
|
|
|
public function fetchCities($provinceCode) |
39
|
|
|
{ |
40
|
|
|
$this->_result = (array) Json::decode(Http::get('http://www.nmc.cn/f/rest/province/' . $provinceCode)); |
41
|
|
|
$this->_toArrayCall = function ($array) { |
42
|
|
|
return Arrays::columns2($array, ['code', 'city', 'province']); |
43
|
|
|
}; |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* 获取天气状况 |
50
|
|
|
* - publish_time:更新时间(2020-01-01 19:35) |
51
|
|
|
* - airpressure:气压(hPa) |
52
|
|
|
* - feelst:体感温度(℃) |
53
|
|
|
* - humidity:相对湿度(%) |
54
|
|
|
* - icomfort:舒适度 |
55
|
|
|
* - 温暖,较舒适:1 |
56
|
|
|
* - 舒适,最可接受:0 |
57
|
|
|
* - 凉爽,较舒适:-1 |
58
|
|
|
* - 凉,不舒适:-2 |
59
|
|
|
* - 冷,很不舒适:-3 |
60
|
|
|
* - 很冷,极不适应:-4 |
61
|
|
|
* - info:天气(如:晴) |
62
|
|
|
* - rain:降水(mm) |
63
|
|
|
* - temperature:气温(℃) |
64
|
|
|
* - direct:风向(东南风) |
65
|
|
|
* - power:风强(微风、1 级……) |
66
|
|
|
* |
67
|
|
|
* @param string $cityId 城市 ID |
68
|
|
|
* |
69
|
|
|
* @return static |
70
|
|
|
*/ |
71
|
|
|
public function fetchWeather($cityId) |
72
|
|
|
{ |
73
|
|
|
$this->_result = (array) Json::get(Http::get('http://www.nmc.cn/rest/weather', [ |
74
|
|
|
'stationid' => $cityId, |
75
|
|
|
]), 'data.real'); |
76
|
|
|
$this->_toArrayCall = function ($array) { |
77
|
|
|
return Arrays::columns1($array, [ |
78
|
|
|
'publish_time', |
79
|
|
|
'airpressure' => 'weather.airpressure', |
80
|
|
|
'feelst' => 'weather.feelst', |
81
|
|
|
'humidity' => 'weather.humidity', |
82
|
|
|
'info' => 'weather.info', |
83
|
|
|
'rain' => 'weather.rain', |
84
|
|
|
'temperature' => 'weather.temperature', |
85
|
|
|
'direct' => 'wind.direct', |
86
|
|
|
'power' => 'wind.power', |
87
|
|
|
]); |
88
|
|
|
}; |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* 空气质量 |
95
|
|
|
* - forecasttime:发布时间(2020-01-01 19:00) |
96
|
|
|
* - aq:空气质量 |
97
|
|
|
* - 优:1 |
98
|
|
|
* - 良:2 |
99
|
|
|
* - 轻度污染:3 |
100
|
|
|
* - 中度污染:4 |
101
|
|
|
* - text:空气质量文本 |
102
|
|
|
* |
103
|
|
|
* @param string $cityId 城市 ID |
104
|
|
|
* |
105
|
|
|
* @return static |
106
|
|
|
*/ |
107
|
|
|
public function fetchAirQuality($cityId) |
108
|
|
|
{ |
109
|
|
|
$this->_result = (array) Json::decode(Http::get('http://www.nmc.cn/f/rest/aqi/' . $cityId)); |
110
|
|
|
$this->_toArrayCall = function ($array) { |
111
|
|
|
return Arrays::columns1($array, [ |
112
|
|
|
'forecasttime', |
113
|
|
|
'aq', |
114
|
|
|
'text', |
115
|
|
|
]); |
116
|
|
|
}; |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* 今天 24 小时实况 |
123
|
|
|
* - time:时间(2020-01-01 20:00) |
124
|
|
|
* - humidity:相对湿度(%) |
125
|
|
|
* - pressure:气压(hPa) |
126
|
|
|
* - rain:降水(mm) |
127
|
|
|
* - temperature:温度(℃) |
128
|
|
|
* - windDirection:风向(?) |
129
|
|
|
* - windSpeed:风速(?) |
130
|
|
|
* |
131
|
|
|
* @param string $cityId 城市 ID |
132
|
|
|
* |
133
|
|
|
* @return static |
134
|
|
|
*/ |
135
|
|
|
public function fetch24Hours($cityId) |
136
|
|
|
{ |
137
|
|
|
$this->_result = (array) Json::decode(Http::get('http://www.nmc.cn/f/rest/passed/' . $cityId)); |
138
|
|
|
$this->_toArrayCall = function ($array) { |
139
|
|
|
return Arrays::columns2($array, [ |
140
|
|
|
'time', |
141
|
|
|
'humidity', |
142
|
|
|
'pressure', |
143
|
|
|
'rain' => 'rain1h', |
144
|
|
|
'temperature', |
145
|
|
|
'windDirection', |
146
|
|
|
'windSpeed', |
147
|
|
|
]); |
148
|
|
|
}; |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* 获取 7 天天气 |
155
|
|
|
* |
156
|
|
|
* @param string $cityId 城市 ID |
157
|
|
|
* |
158
|
|
|
* @return static |
159
|
|
|
*/ |
160
|
|
|
public function fetch7Days($cityId) |
161
|
|
|
{ |
162
|
|
|
$this->_result = (array) Json::decode(Http::get('http://www.nmc.cn/rest/weather', [ |
163
|
|
|
'stationid' => $cityId, |
164
|
|
|
])); |
165
|
|
|
$this->_toArrayCall = function ($array) { |
166
|
|
|
return Arrays::columns2(I::get($array,'data.predict.detail'), [ |
167
|
|
|
'date', |
168
|
|
|
'day', |
169
|
|
|
'night' |
170
|
|
|
]); |
171
|
|
|
}; |
172
|
|
|
|
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
} |