|
| 1 | +--- |
| 2 | +title: 當前天氣 API |
| 3 | +sidebar_label: '當前天氣' |
| 4 | +--- |
| 5 | + |
| 6 | +此 API 允許您檢索當前的天氣數據。 |
| 7 | + |
| 8 | +> [對應的 OpenWeatherMap 文件](https://openweathermap.org/current) |
| 9 | +
|
| 10 | +`$owm->getWeather()` 函式接受以下參數: |
| 11 | + |
| 12 | +| 名稱 | 類型 | 預設值 | 描述 | |
| 13 | +|------|------|---------|-------------| |
| 14 | +| `$query` | `mixed` | -- | 請參見下文 | |
| 15 | +| `$units` | `"imperial"`|`"metric"` | `"imperial"` | 使用的單位 | |
| 16 | +| `$lang` | `string` | `en` | 語言之一,請參見 [官方文件底部](https://openweathermap.org/current#multi) | |
| 17 | + |
| 18 | +## `$query` 參數 |
| 19 | + |
| 20 | +第一個參數確定要獲取天氣數據的位置。有多種函式可用: |
| 21 | + |
| 22 | +### 根據城市名稱 |
| 23 | + |
| 24 | +指定國家是可選的。 |
| 25 | + |
| 26 | +```php |
| 27 | +$weather = $owm->getWeather('Berlin,DE', $units, $lang); |
| 28 | +``` |
| 29 | + |
| 30 | +### 根據城市 ID |
| 31 | + |
| 32 | +單個城市 ID: |
| 33 | +```php |
| 34 | +$weather = $owm->getWeather(2172797, $units, $lang); |
| 35 | +``` |
| 36 | + |
| 37 | +多個城市 ID: |
| 38 | +```php |
| 39 | +// 警告:這使用了一個不同的函式 (getWeatherGroup) |
| 40 | +// 與其他查詢格式 (getWeather) 不同! |
| 41 | +$weathers = $owm->getWeatherGroup([2172797, 2172798], $units, $lang); |
| 42 | +foreach ($weathers as $weather) { |
| 43 | + // 處理 |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +### 根據郵政編碼 |
| 48 | + |
| 49 | +指定國家是可選的。 |
| 50 | + |
| 51 | +```php |
| 52 | +// 印度,海得拉巴 |
| 53 | +$weather = $owm->getWeather('zip:500001,IN', $units, $lang); |
| 54 | +``` |
| 55 | + |
| 56 | +### 根據座標 |
| 57 | + |
| 58 | +```php |
| 59 | +$weather = $owm->getWeather(['lat' => 77.73038, 'lon' => 41.89604], |
| 60 | + $units, $lang); |
| 61 | +``` |
| 62 | + |
| 63 | +## `$weather` 對象 |
| 64 | + |
| 65 | +返回的對象是一個 `Cmfcmf\OpenWeatherMap\CurrentWeather` 實例。它提供以下數據: |
| 66 | + |
| 67 | +| 名稱 | 類型 | 描述 | |
| 68 | +|------|------|-------------| |
| 69 | +| `lastUpdate` | `\DateTimeInterface` | 數據的最後更新時間 | |
| 70 | +| `temperature->now` | `Unit` | 注意:這應命名為 `temperature->avg`,僅為向後兼容而命名為 `temperature->now`!返回給定位置的當前平均溫度(例如,一個大城市可能有多個溫度測量站) | |
| 71 | +| `temperature->min` | `Unit` | 給定位置的當前最低溫度 | |
| 72 | +| `temperature->max` | `Unit` | 給定位置的當前最高溫度 | |
| 73 | +| `pressure` | `Unit` | 氣壓 | |
| 74 | +| `humidity` | `Unit` | 濕度 | |
| 75 | +| `sun->rise` | `\DateTimeInterface` | 日出時間 | |
| 76 | +| `sun->set` | `\DateTimeInterface` | 日落時間 | |
| 77 | +| `wind->speed` | `Unit` | 風速 | |
| 78 | +| `wind->direction` | `Unit` | 風向 | |
| 79 | +| `clouds` | `Unit` | 雲量百分比 | |
| 80 | +| `precipitation` | `Unit` | 最近的降水 | |
| 81 | +| `weather->id` | `int` | 當前天氣現象 ID | |
| 82 | +| `weather->description` | `string` | 當前天氣描述 | |
| 83 | +| `weather->icon` | `string` | 當前天氣圖標名稱。使用 `weather->getIconUrl()` 獲取 OpenWeatherMap 圖標的 URL | |
| 84 | +| `city->id` | `int` | 內部城市 ID | |
| 85 | +| `city->name` | `string` | 城市名稱 | |
| 86 | +| `city->country` | `string` | 城市國家代碼 | |
| 87 | +| `city->timezone` | `\DateTimeZone`|`null` | 城市時區 | |
| 88 | +| `city->lon` | `float` | 城市經度 | |
| 89 | +| `city->lat` | `float` | 城市緯度 | |
| 90 | + |
| 91 | +## 獲取原始數據 |
| 92 | + |
| 93 | +### HTML |
| 94 | + |
| 95 | +您還可以請求以 HTML 頁面形式的數據: |
| 96 | + |
| 97 | +```php |
| 98 | +$html = $owm->getRawWeatherData('Berlin', $units, $lang, null, 'html'); |
| 99 | +``` |
| 100 | + |
| 101 | +結果: |
| 102 | + |
| 103 | +```html |
| 104 | +<!DOCTYPE html> |
| 105 | +<html lang="en"> |
| 106 | +<head> |
| 107 | + <meta charset="utf-8"> |
| 108 | + <meta name="keywords" content="weather, world, openweathermap, weather, layer" /> |
| 109 | + <meta name="description" content="A layer with current weather conditions in cities for world wide" /> |
| 110 | + <meta name="domain" content="openweathermap.org" /> |
| 111 | + <meta http-equiv="pragma" content="no-cache" /> |
| 112 | + <meta http-equiv="Expires" content="-1" /> |
| 113 | +</head> |
| 114 | +<body> |
| 115 | + <div style="font-size: medium; font-weight: bold; margin-bottom: 0px;">Berlin</div> |
| 116 | + <div style="float: left; width: 130px;"> |
| 117 | + <div style="display: block; clear: left;"> |
| 118 | + <div style="float: left;" title="Titel"> |
| 119 | + <img height="45" width="45" style="border: medium none; width: 45px; height: 45px; background: url("http://openweathermap.org/img/w/04d.png") repeat scroll 0% 0% transparent;" alt="title" src="http://openweathermap.org/images/transparent.png"/> |
| 120 | + </div> |
| 121 | + <div style="float: left;"> |
| 122 | + <div style="display: block; clear: left; font-size: medium; font-weight: bold; padding: 0pt 3pt;" title="Current Temperature">12.73°C</div> |
| 123 | + <div style="display: block; width: 85px; overflow: visible;"></div> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + <div style="display: block; clear: left; font-size: small;">雲量:89%</div> |
| 127 | + <div style="display: block; clear: left; color: gray; font-size: x-small;" >濕度:62%</div> |
| 128 | + <div style="display: block; clear: left; color: gray; font-size: x-small;" >風速:6.2 m/s</div> |
| 129 | + <div style="display: block; clear: left; color: gray; font-size: x-small;" >氣壓:1014hpa</div> |
| 130 | + </div> |
| 131 | + <div style="display: block; clear: left; color: gray; font-size: x-small;"> |
| 132 | + <a href="http://openweathermap.org/city/2950159?utm_source=openweathermap&utm_medium=widget&utm_campaign=html_old" target="_blank">更多..</a> |
| 133 | + </div> |
| 134 | + <script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ |
| 135 | +(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), |
| 136 | +m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) |
| 137 | +})(window,document,'script','//www.google-analytics.com/analytics.js','ga');ga('create', 'UA-31601618-9', 'auto');ga('send', 'pageview');</script> |
| 138 | +</body> |
| 139 | +</html> |
| 140 | +``` |
| 141 | + |
| 142 | +### JSON |
| 143 | + |
| 144 | +```php |
| 145 | +$json = $owm->getRawWeatherData('Berlin', 'metric', 'de', null, 'json'); |
| 146 | +``` |
| 147 | +結果: |
| 148 | + |
| 149 | +```json |
| 150 | +{ |
| 151 | + "coord":{"lon":13.41,"lat":52.52}, |
| 152 | + "weather":[{"id":804,"main":"Clouds","description":"Bedeckt","icon":"04d"}], |
| 153 | + "base":"stations", |
| 154 | + "main":{"temp":12.73,"feels_like":7.4,"temp_min":11.67, |
| 155 | + "temp_max":13.89,"pressure":1014,"humidity":62}, |
| 156 | + "visibility":10000, |
| 157 | + "wind":{"speed":6.2,"deg":200}, |
| 158 | + "clouds":{"all":89}, |
| 159 | + "dt":1579089181, |
| 160 | + "sys":{"type":1,"id":1275,"country":"DE","sunrise":1579072219,"sunset":1579101619}, |
| 161 | + "timezone":3600, |
| 162 | + "id":2950159, |
| 163 | + "name":"Berlin", |
| 164 | + "cod":200 |
| 165 | +} |
| 166 | +``` |
| 167 | + |
| 168 | +### XML |
| 169 | + |
| 170 | +```php |
| 171 | +$xml = $owm->getRawWeatherData('Berlin', 'metric', 'de', null, 'xml'); |
| 172 | +``` |
| 173 | + |
| 174 | +結果: |
| 175 | + |
| 176 | +```xml |
| 177 | +<?xml version="1.0" encoding="UTF-8"?> |
| 178 | +<current><city id="2950159" name="Berlin"><coord lon="13.41" lat="52.52"></coord><country>DE</country><timezone>3600</timezone><sun rise="2020-01-15T07:10:19 |
| 179 | +
|
| 180 | +" set="2020-01-15T15:20:19"></sun></city><temperature value="12.73" min="11.67" max="13.89" unit="celsius"></temperature><feels_like value="7.4" unit="celsius"></feels_like><humidity value="62" unit="%"></humidity><pressure value="1014" unit="hPa"></pressure><wind><speed value="6.2" unit="m/s" name="Moderate breeze"></speed><gusts></gusts><direction value="200" code="SSW" name="South-southwest"></direction></wind><clouds value="89" name="Bedeckt"></clouds><visibility value="10000"></visibility><precipitation mode="no"></precipitation><weather number="804" value="Bedeckt" icon="04d"></weather><lastupdate value="2020-01-15T11:53:01"></lastupdate></current> |
| 181 | +``` |
0 commit comments