Skip to content

Commit 72ca319

Browse files
committed
Made it PSR2 compatible, added composer, made it all object oriented, and added caching hooks. - BC-Breaks!
1 parent 4b15295 commit 72ca319

15 files changed

+624
-301
lines changed

Example_Cache.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use cmfcmf\OpenWeatherMap;
4+
5+
require('cmfcmf/OpenWeatherMap.php');
6+
7+
// Language of data (try your own language here!):
8+
$lang = 'de';
9+
10+
// Units (can be 'metric' or 'imperial' [default]):
11+
$units = 'metric';
12+
13+
// Example 1: Use your own cache implementation. See the example_cache file.
14+
$owm = new OpenWeatherMap('examplecache', 100);
15+
16+
$weather = $owm->getWeather('Berlin', $units, $lang);
17+
echo "EXAMPLE 1<hr />\n\n\n";
18+
echo $weather->temperature;

Examples.php

+24-19
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,22 @@
1414
* @see http://openweathermap.org/appid
1515
*/
1616

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');
1921

2022
// Language of data (try your own language here!):
2123
$lang = 'de';
2224

2325
// Units (can be 'metric' or 'imperial' [default]):
2426
$units = 'metric';
2527

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+
2631
// Example 1: Get current temperature in Berlin.
27-
$weather = OpenWeatherMap::getWeather('Berlin', $units, $lang);
32+
$weather = $owm->getWeather('Berlin', $units, $lang);
2833
echo "EXAMPLE 1<hr />\n\n\n";
2934

3035
// $weather contains all available weather information for Berlin.
@@ -73,7 +78,7 @@
7378
echo "<br />\n";
7479

7580
// Example 2: Get current pressure and humidity in Hongkong.
76-
$weather = OpenWeatherMap::getWeather('Hongkong', $units, $lang);
81+
$weather = $owm->getWeather('Hongkong', $units, $lang);
7782
echo "<br /><br />\n\n\nEXAMPLE 2<hr />\n\n\n";
7883

7984
/**
@@ -98,14 +103,14 @@
98103
echo "<br />\n";
99104

100105
// 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);
102107
echo "<br /><br />\n\n\nEXAMPLE 4<hr />\n\n\n";
103108

104109
echo "Temperature: " . $weather->temperature;
105110
echo "<br />\n";
106111

107112
// 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);
109114
echo "<br /><br />\n\n\nEXAMPLE 5<hr />\n\n\n";
110115

111116
echo "City: " . $weather->city->name;
@@ -115,7 +120,7 @@
115120
echo "<br />\n";
116121

117122
// Example 6: Get information about a city.
118-
$weather = OpenWeatherMap::getWeather('Paris', $units, $lang);
123+
$weather = $owm->getWeather('Paris', $units, $lang);
119124
echo "<br /><br />\n\n\nEXAMPLE 6<hr />\n\n\n";
120125

121126
echo "Id: " . $weather->city->id;
@@ -174,52 +179,52 @@
174179
// Example 11: Get raw xml data.
175180
echo "<br /><br />\n\n\nEXAMPLE 11<hr />\n\n\n";
176181

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>";
178183
echo "<br />\n";
179184

180185
// Example 12: Get raw json data.
181186
echo "<br /><br />\n\n\nEXAMPLE 12<hr />\n\n\n";
182187

183-
echo "<code>" . htmlspecialchars(OpenWeatherMap::getRawData('Berlin', $units, $lang, null, 'json')) . "</code>";
188+
echo "<code>" . htmlspecialchars($owm->getRawData('Berlin', $units, $lang, null, 'json')) . "</code>";
184189
echo "<br />\n";
185190

186191
// Example 13: Get raw html data.
187192
echo "<br /><br />\n\n\nEXAMPLE 13<hr />\n\n\n";
188193

189-
echo OpenWeatherMap::getRawData('Berlin', $units, $lang, null, 'html');
194+
echo $owm->getRawData('Berlin', $units, $lang, null, 'html');
190195
echo "<br />\n";
191196

192197
// Example 14: Error handling.
193198
echo "<br /><br />\n\n\nEXAMPLE 14<hr />\n\n\n";
194199

195200
// Try wrong city name.
196201
try {
197-
$weather = OpenWeatherMap::getWeather("ThisCityNameIsNotValidAndDoesNotExist", $units, $lang);
198-
} catch(OpenWeatherMap_Exception $e) {
202+
$weather = $owm->getWeather("ThisCityNameIsNotValidAndDoesNotExist", $units, $lang);
203+
} catch(OWMException $e) {
199204
echo $e->getMessage() . ' (Code ' . $e->getCode() . ').';
200205
echo "<br />\n";
201206
}
202207

203208
// Try invalid $query.
204209
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) {
207212
echo $e->getMessage() . ' (Code ' . $e->getCode() . ').';
208213
echo "<br />\n";
209214
}
210215

211216
// Full error handling would look like this:
212217
try {
213-
$weather = OpenWeatherMap::getWeather(-1, $units, $lang);
214-
} catch(OpenWeatherMap_Exception $e) {
218+
$weather = $owm->getWeather(-1, $units, $lang);
219+
} catch(OWMException $e) {
215220
echo 'OpenWeatherMap exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').';
216221
echo "<br />\n";
217-
} catch(Exception $e) {
222+
} catch(\Exception $e) {
218223
echo 'General exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').';
219224
echo "<br />\n";
220225
}
221226

222227
// Example 15: Using an api key:
223228

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

Comments
 (0)