|
14 | 14 | * @see http://openweathermap.org/appid |
15 | 15 | */ |
16 | 16 |
|
17 | | -// Include api class |
18 | | -require_once('OpenWeatherMap.php'); |
| 17 | +use cmfcmf\OpenWeatherMap; |
| 18 | +use cmfcmf\OpenWeatherMap\Exception as OWMException; |
| 19 | + |
| 20 | +require('cmfcmf/OpenWeatherMap.php'); |
19 | 21 |
|
20 | 22 | // Language of data (try your own language here!): |
21 | 23 | $lang = 'de'; |
22 | 24 |
|
23 | 25 | // Units (can be 'metric' or 'imperial' [default]): |
24 | 26 | $units = 'metric'; |
25 | 27 |
|
| 28 | +// Get OpenWeatherMap object. Don't use caching (take a look into Example_Cache.php to see how it works). |
| 29 | +$owm = new OpenWeatherMap(); |
| 30 | + |
26 | 31 | // Example 1: Get current temperature in Berlin. |
27 | | -$weather = OpenWeatherMap::getWeather('Berlin', $units, $lang); |
| 32 | +$weather = $owm->getWeather('Berlin', $units, $lang); |
28 | 33 | echo "EXAMPLE 1<hr />\n\n\n"; |
29 | 34 |
|
30 | 35 | // $weather contains all available weather information for Berlin. |
|
73 | 78 | echo "<br />\n"; |
74 | 79 |
|
75 | 80 | // Example 2: Get current pressure and humidity in Hongkong. |
76 | | -$weather = OpenWeatherMap::getWeather('Hongkong', $units, $lang); |
| 81 | +$weather = $owm->getWeather('Hongkong', $units, $lang); |
77 | 82 | echo "<br /><br />\n\n\nEXAMPLE 2<hr />\n\n\n"; |
78 | 83 |
|
79 | 84 | /** |
|
98 | 103 | echo "<br />\n"; |
99 | 104 |
|
100 | 105 | // Example 4: Get current temperature from coordinates (Greenland :-) ). |
101 | | -$weather = OpenWeatherMap::getWeather(array('lat' => 77.73038, 'lon' => 41.89604), $units, $lang); |
| 106 | +$weather = $owm->getWeather(array('lat' => 77.73038, 'lon' => 41.89604), $units, $lang); |
102 | 107 | echo "<br /><br />\n\n\nEXAMPLE 4<hr />\n\n\n"; |
103 | 108 |
|
104 | 109 | echo "Temperature: " . $weather->temperature; |
105 | 110 | echo "<br />\n"; |
106 | 111 |
|
107 | 112 | // Example 5: Get current temperature from city id. The city is an internal id used by OpenWeatherMap. See example 6 too. |
108 | | -$weather = OpenWeatherMap::getWeather(2172797, $units, $lang); |
| 113 | +$weather = $owm->getWeather(2172797, $units, $lang); |
109 | 114 | echo "<br /><br />\n\n\nEXAMPLE 5<hr />\n\n\n"; |
110 | 115 |
|
111 | 116 | echo "City: " . $weather->city->name; |
|
115 | 120 | echo "<br />\n"; |
116 | 121 |
|
117 | 122 | // Example 6: Get information about a city. |
118 | | -$weather = OpenWeatherMap::getWeather('Paris', $units, $lang); |
| 123 | +$weather = $owm->getWeather('Paris', $units, $lang); |
119 | 124 | echo "<br /><br />\n\n\nEXAMPLE 6<hr />\n\n\n"; |
120 | 125 |
|
121 | 126 | echo "Id: " . $weather->city->id; |
|
174 | 179 | // Example 11: Get raw xml data. |
175 | 180 | echo "<br /><br />\n\n\nEXAMPLE 11<hr />\n\n\n"; |
176 | 181 |
|
177 | | -echo "<pre><code>" . htmlspecialchars(OpenWeatherMap::getRawData('Berlin', $units, $lang, null, 'xml')) . "</code></pre>"; |
| 182 | +echo "<pre><code>" . htmlspecialchars($owm->getRawData('Berlin', $units, $lang, null, 'xml')) . "</code></pre>"; |
178 | 183 | echo "<br />\n"; |
179 | 184 |
|
180 | 185 | // Example 12: Get raw json data. |
181 | 186 | echo "<br /><br />\n\n\nEXAMPLE 12<hr />\n\n\n"; |
182 | 187 |
|
183 | | -echo "<code>" . htmlspecialchars(OpenWeatherMap::getRawData('Berlin', $units, $lang, null, 'json')) . "</code>"; |
| 188 | +echo "<code>" . htmlspecialchars($owm->getRawData('Berlin', $units, $lang, null, 'json')) . "</code>"; |
184 | 189 | echo "<br />\n"; |
185 | 190 |
|
186 | 191 | // Example 13: Get raw html data. |
187 | 192 | echo "<br /><br />\n\n\nEXAMPLE 13<hr />\n\n\n"; |
188 | 193 |
|
189 | | -echo OpenWeatherMap::getRawData('Berlin', $units, $lang, null, 'html'); |
| 194 | +echo $owm->getRawData('Berlin', $units, $lang, null, 'html'); |
190 | 195 | echo "<br />\n"; |
191 | 196 |
|
192 | 197 | // Example 14: Error handling. |
193 | 198 | echo "<br /><br />\n\n\nEXAMPLE 14<hr />\n\n\n"; |
194 | 199 |
|
195 | 200 | // Try wrong city name. |
196 | 201 | try { |
197 | | - $weather = OpenWeatherMap::getWeather("ThisCityNameIsNotValidAndDoesNotExist", $units, $lang); |
198 | | -} catch(OpenWeatherMap_Exception $e) { |
| 202 | + $weather = $owm->getWeather("ThisCityNameIsNotValidAndDoesNotExist", $units, $lang); |
| 203 | +} catch(OWMException $e) { |
199 | 204 | echo $e->getMessage() . ' (Code ' . $e->getCode() . ').'; |
200 | 205 | echo "<br />\n"; |
201 | 206 | } |
202 | 207 |
|
203 | 208 | // Try invalid $query. |
204 | 209 | try { |
205 | | - $weather = OpenWeatherMap::getWeather(new DateTime('now'), $units, $lang); |
206 | | -} catch(Exception $e) { |
| 210 | + $weather = $owm->getWeather(new \DateTime('now'), $units, $lang); |
| 211 | +} catch(\Exception $e) { |
207 | 212 | echo $e->getMessage() . ' (Code ' . $e->getCode() . ').'; |
208 | 213 | echo "<br />\n"; |
209 | 214 | } |
210 | 215 |
|
211 | 216 | // Full error handling would look like this: |
212 | 217 | try { |
213 | | - $weather = OpenWeatherMap::getWeather(-1, $units, $lang); |
214 | | -} catch(OpenWeatherMap_Exception $e) { |
| 218 | + $weather = $owm->getWeather(-1, $units, $lang); |
| 219 | +} catch(OWMException $e) { |
215 | 220 | echo 'OpenWeatherMap exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').'; |
216 | 221 | echo "<br />\n"; |
217 | | -} catch(Exception $e) { |
| 222 | +} catch(\Exception $e) { |
218 | 223 | echo 'General exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').'; |
219 | 224 | echo "<br />\n"; |
220 | 225 | } |
221 | 226 |
|
222 | 227 | // Example 15: Using an api key: |
223 | 228 |
|
224 | | -# OpenWeatherMap::getWeather('Berlin', $units, $lang, 'Your-Api-Key-Here'); |
225 | | -# OpenWeatherMap::getRawData('Berlin', $units, $lang, 'Your-Api-Key-Here', 'json'); |
| 229 | +# $owm->getWeather('Berlin', $units, $lang, 'Your-Api-Key-Here'); |
| 230 | +# $owm->getRawData('Berlin', $units, $lang, 'Your-Api-Key-Here', 'json'); |
0 commit comments