├── .editorconfig ├── README.md ├── composer.json └── src ├── .gitkeep ├── Exceptions ├── Exception.php ├── HttpException.php └── InvalidArgumentException.php ├── ServiceProvider.php └── Weather.php /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 4 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = false 10 | 11 | [*.{vue,js,scss}] 12 | charset = utf-8 13 | indent_style = space 14 | indent_size = 2 15 | end_of_line = lf 16 | insert_final_newline = true 17 | trim_trailing_whitespace = true 18 | 19 | [*.md] 20 | trim_trailing_whitespace = false -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/Everan-1994/weather.svg?branch=master)](https://travis-ci.org/Everan-1994/weather) 2 | 3 | # Weather 4 | 5 | 基于 [高德开放平台](https://lbs.amap.com/dev/id/newuser) 的 PHP 天气信息组件。 6 | 7 | ## 安装 8 | 9 | ```sh 10 | $ composer require everan/weather -vvv 11 | ``` 12 | 13 | ## 配置 14 | 15 | 在使用本扩展之前,你需要去 [高德开放平台](https://lbs.amap.com/dev/id/newuser) 注册账号,然后创建应用,获取应用的 API Key。 16 | 17 | ## 使用 18 | 19 | ```php 20 | use Everan\Weather\Weather; 21 | 22 | $key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'; 23 | 24 | $weather = new Weather($key); 25 | ``` 26 | 27 | ### 获取实时天气 28 | 29 | ```php 30 | $response = $weather->getLiveWeather('深圳'); 31 | ``` 32 | 示例: 33 | 34 | ``` 35 | { 36 | "status": "1", 37 | "count": "1", 38 | "info": "OK", 39 | "infocode": "10000", 40 | "lives": [ 41 | { 42 | "province": "广东", 43 | "city": "深圳市", 44 | "adcode": "440300", 45 | "weather": "阴", 46 | "temperature": "20", 47 | "winddirection": "东", 48 | "windpower": "6", 49 | "humidity": "82", 50 | "reporttime": "2018-11-03 12:00:00" 51 | } 52 | ] 53 | } 54 | ``` 55 | 56 | ### 获取近期天气预报 57 | 58 | ``` 59 | $response = $weather->getForecastsWeather('深圳'); 60 | ``` 61 | 示例: 62 | 63 | ```json 64 | { 65 | "status": "1", 66 | "count": "1", 67 | "info": "OK", 68 | "infocode": "10000", 69 | "forecasts": [ 70 | { 71 | "city": "深圳市", 72 | "adcode": "440300", 73 | "province": "广东", 74 | "reporttime": "2018-11-03 11:00:00", 75 | "casts": [ 76 | { 77 | "date": "2018-11-03", 78 | "week": "6", 79 | "dayweather": "多云", 80 | "nightweather": "多云", 81 | "daytemp": "24", 82 | "nighttemp": "19", 83 | "daywind": "无风向", 84 | "nightwind": "无风向", 85 | "daypower": "≤3", 86 | "nightpower": "≤3" 87 | }, 88 | { 89 | "date": "2018-11-04", 90 | "week": "7", 91 | "dayweather": "多云", 92 | "nightweather": "多云", 93 | "daytemp": "26", 94 | "nighttemp": "22", 95 | "daywind": "无风向", 96 | "nightwind": "无风向", 97 | "daypower": "≤3", 98 | "nightpower": "≤3" 99 | }, 100 | { 101 | "date": "2018-11-05", 102 | "week": "1", 103 | "dayweather": "多云", 104 | "nightweather": "多云", 105 | "daytemp": "27", 106 | "nighttemp": "23", 107 | "daywind": "无风向", 108 | "nightwind": "无风向", 109 | "daypower": "≤3", 110 | "nightpower": "≤3" 111 | }, 112 | { 113 | "date": "2018-11-06", 114 | "week": "2", 115 | "dayweather": "多云", 116 | "nightweather": "多云", 117 | "daytemp": "26", 118 | "nighttemp": "21", 119 | "daywind": "无风向", 120 | "nightwind": "无风向", 121 | "daypower": "≤3", 122 | "nightpower": "≤3" 123 | } 124 | ] 125 | } 126 | ] 127 | } 128 | ``` 129 | 130 | ### 获取 XML 格式返回值 131 | 132 | 第二个参数为返回值类型,可选 `json` 与 `xml`,默认 `json`: 133 | 134 | ```php 135 | // $response = $weather->getLiveWeather('深圳', 'xml'); 136 | $response = $weather->getForecastsWeather('深圳', 'xml'); 137 | ``` 138 | 139 | 示例: 140 | 141 | ```xml 142 | 143 | 1 144 | 1 145 | OK 146 | 10000 147 | 148 | 149 | 广东 150 | 深圳市 151 | 440300 152 | 153 | 20 154 | 155 | 6 156 | 82 157 | 2018-11-03 12:00:00 158 | 159 | 160 | 161 | ``` 162 | 163 | ### 参数说明 164 | 165 | ``` 166 | array | string getLiveWeather(string $city, string $format = 'json') 167 | array | string getForecastsWeather(string $city, string $format = 'json') 168 | ``` 169 | 170 | > - `$city` - 城市名,比如:“深圳”,可以是城市代码:440300 171 | > - `$format` - 输出的数据格式,默认为 json 格式,当 output 设置为 “`xml`” 时,输出的为 XML 格式的数据。 172 | 173 | ### 在 Laravel 中使用 174 | 175 | 在 Laravel 中使用也是同样的安装方式,配置写在 `config/services.php` 中: 176 | 177 | ```php 178 | . 179 | . 180 | . 181 | 'weather' => [ 182 | 'key' => env('WEATHER_API_KEY'), 183 | ], 184 | ``` 185 | 186 | 然后在 `.env` 中配置 `WEATHER_API_KEY` : 187 | 188 | ```env 189 | WEATHER_API_KEY=xxxxxxxxxxxxxxxxxxxxx 190 | ``` 191 | 192 | 可以用两种方式来获取 `Everan\Weather\Weather` 实例: 193 | 194 | #### 方法参数注入 195 | 196 | ```php 197 | . 198 | . 199 | . 200 | public function edit(Weather $weather) 201 | { 202 | // $response = $weather->getLiveWeather('深圳'); 203 | $response = $weather->getForecastsWeather('深圳'); 204 | } 205 | . 206 | . 207 | . 208 | ``` 209 | 210 | #### 服务名访问 211 | 212 | ```php 213 | . 214 | . 215 | . 216 | public function edit() 217 | { 218 | // $response = app('weather')->getLiveWeather('深圳'); 219 | $response = app('weather')->getForecastsWeather('深圳'); 220 | } 221 | . 222 | . 223 | . 224 | 225 | ``` 226 | 227 | ## 参考 228 | 229 | - [高德开放平台天气接口](https://lbs.amap.com/api/webservice/guide/api/weatherinfo/) 230 | 231 | ## License 232 | 233 | MIT 234 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "everan\/weather", 3 | "description": "Package description here.", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "everan-1994", 8 | "email": "everan@aliyun.com" 9 | } 10 | ], 11 | "require": { 12 | "guzzlehttp/guzzle": "^6.3", 13 | "mockery/mockery": "^1.2", 14 | "phpunit/phpunit": "~5" 15 | }, 16 | "autoload": { 17 | "psr-4": { 18 | "Everan\\Weather\\": "src" 19 | } 20 | }, 21 | "extra": { 22 | "laravel": { 23 | "providers": [ 24 | "Everan\\Weather\\ServiceProvider" 25 | ] 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Everan-1994/weather/c4d7e2b4e8dbb26bbe31487ebd5a90f88bd6334e/src/.gitkeep -------------------------------------------------------------------------------- /src/Exceptions/Exception.php: -------------------------------------------------------------------------------- 1 | app->singleton(Weather::class, function(){ 12 | return new Weather(config('services.weather.key')); 13 | }); 14 | 15 | $this->app->alias(Weather::class, 'weather'); 16 | } 17 | 18 | public function provides() 19 | { 20 | return [Weather::class, 'weather']; 21 | } 22 | } -------------------------------------------------------------------------------- /src/Weather.php: -------------------------------------------------------------------------------- 1 | key = $key; 17 | } 18 | 19 | /** 20 | * @return Client 21 | */ 22 | public function getHttpClient() 23 | { 24 | return new Client($this->guzzleOptions); 25 | } 26 | 27 | public function setGuzzleOptions(array $options) 28 | { 29 | $this->guzzleOptions = $options; 30 | } 31 | 32 | /** 33 | * @param $city 34 | * @param string $type 35 | * @param string $format 36 | * @return mixed|string 37 | * @throws HttpException 38 | * @throws InvalidArgumentException 39 | */ 40 | public function getWeather($city, $type = 'live', $format = 'json') 41 | { 42 | $url = 'https://restapi.amap.com/v3/weather/weatherInfo'; 43 | 44 | $types = [ 45 | 'live' => 'base', 46 | 'forecast' => 'all', 47 | ]; 48 | 49 | if (!\in_array(\strtolower($format), ['xml', 'json'])) { 50 | throw new InvalidArgumentException('Invalid response format: ' . $format); 51 | } 52 | 53 | if (!\array_key_exists(\strtolower($type), $types)) { 54 | throw new InvalidArgumentException('Invalid type value(live/forecast): ' . $type); 55 | } 56 | 57 | $query = array_filter([ 58 | 'key' => $this->key, 59 | 'city' => $city, 60 | 'output' => \strtolower($format), 61 | 'extensions' => \strtolower($types[$type]), 62 | ]); 63 | 64 | try { 65 | $response = $this->getHttpClient()->get($url, [ 66 | 'query' => $query, 67 | ])->getBody()->getContents(); 68 | 69 | return 'json' === $format ? \json_decode($response, true) : $response; 70 | } catch (\Exception $e) { 71 | throw new HttpException($e->getMessage(), $e->getCode(), $e); 72 | } 73 | 74 | } 75 | 76 | public function getLiveWeather($city, $format = 'json') 77 | { 78 | return $this->getWeather($city, 'live', $format); 79 | } 80 | 81 | public function getForecastsWeather($city, $format = 'json') 82 | { 83 | return $this->getWeather($city, 'forecast', $format); 84 | } 85 | } --------------------------------------------------------------------------------