Skip to content

Commit e5dd750

Browse files
committed
Added forecast functionality.
1 parent 1784064 commit e5dd750

10 files changed

+640
-64
lines changed

Examples_Current.php

+230
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
<?php
2+
/**
3+
* OpenWeatherMap-PHP-API — An php api to parse weather data from http://www.OpenWeatherMap.org .
4+
*
5+
* @license MIT
6+
*
7+
* Please see the LICENSE file distributed with this source code for further
8+
* information regarding copyright and licensing.
9+
*
10+
* Please visit the following links to read about the usage policies and the license of OpenWeatherMap before using this class.
11+
* @see http://www.OpenWeatherMap.org
12+
* @see http://www.OpenWeatherMap.org/about
13+
* @see http://www.OpenWeatherMap.org/copyright
14+
* @see http://openweathermap.org/appid
15+
*/
16+
17+
use cmfcmf\OpenWeatherMap;
18+
use cmfcmf\OpenWeatherMap\Exception as OWMException;
19+
20+
require('cmfcmf/OpenWeatherMap.php');
21+
22+
// Language of data (try your own language here!):
23+
$lang = 'de';
24+
25+
// Units (can be 'metric' or 'imperial' [default]):
26+
$units = 'metric';
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+
31+
// Example 1: Get current temperature in Berlin.
32+
$weather = $owm->getWeather('Berlin', $units, $lang);
33+
echo "EXAMPLE 1<hr />\n\n\n";
34+
35+
// $weather contains all available weather information for Berlin.
36+
// Let's get the temperature:
37+
38+
// Returns it as formatted string (using __toString()):
39+
echo $weather->temperature;
40+
echo "<br />\n";
41+
42+
// Returns it as formatted string (using a method):
43+
echo $weather->temperature->getFormatted();
44+
echo "<br />\n";
45+
46+
// Returns the value only:
47+
echo $weather->temperature->getValue();
48+
echo "<br />\n";
49+
50+
// Returns the unit only:
51+
echo $weather->temperature->getUnit();
52+
echo "<br />\n";
53+
54+
/**
55+
* @notice In the example above we're using a "shortcut". OpenWeatherMap returns the minimum temperature of a day,
56+
* the maximum temperature and the temperature right now. If you don't specify which temperature you want, it will default
57+
* to the current temperature. See below how to access the other values. Notice that each of them has implemented the methods
58+
* "getFormatted()", "getValue()", "getUnit()".
59+
*/
60+
61+
// Returns the current temperature:
62+
echo "Current: " . $weather->temperature->now;
63+
echo "<br />\n";
64+
65+
// Returns the minimum temperature:
66+
echo "Minimum: " . $weather->temperature->min;
67+
echo "<br />\n";
68+
69+
// Returns the maximum temperature:
70+
echo "Maximum: " . $weather->temperature->max;
71+
echo "<br />\n";
72+
73+
/**
74+
* @notice When speaking about "current" and "now", this means when the weather data was last updated. You can get this
75+
* via a DateTime object:
76+
*/
77+
echo "Last update: " . $weather->lastUpdate->format('r');
78+
echo "<br />\n";
79+
80+
// Example 2: Get current pressure and humidity in Hongkong.
81+
$weather = $owm->getWeather('Hongkong', $units, $lang);
82+
echo "<br /><br />\n\n\nEXAMPLE 2<hr />\n\n\n";
83+
84+
/**
85+
* @notice You can use the methods above to only get the value or the unit.
86+
*/
87+
88+
echo "Pressure: " . $weather->pressure;
89+
echo "<br />\n";
90+
echo "Humidity: " . $weather->humidity;
91+
echo "<br />\n";
92+
93+
// Example 3: Get today's sunrise and sunset times.
94+
echo "<br /><br />\n\n\nEXAMPLE 3<hr />\n\n\n";
95+
96+
/**
97+
* @notice These functions return a DateTime object.
98+
*/
99+
100+
echo "Sunrise: " . $weather->sun->rise->format('r');
101+
echo "<br />\n";
102+
echo "Sunset: " . $weather->sun->set->format('r');
103+
echo "<br />\n";
104+
105+
// Example 4: Get current temperature from coordinates (Greenland :-) ).
106+
$weather = $owm->getWeather(array('lat' => 77.73038, 'lon' => 41.89604), $units, $lang);
107+
echo "<br /><br />\n\n\nEXAMPLE 4<hr />\n\n\n";
108+
109+
echo "Temperature: " . $weather->temperature;
110+
echo "<br />\n";
111+
112+
// Example 5: Get current temperature from city id. The city is an internal id used by OpenWeatherMap. See example 6 too.
113+
$weather = $owm->getWeather(2172797, $units, $lang);
114+
echo "<br /><br />\n\n\nEXAMPLE 5<hr />\n\n\n";
115+
116+
echo "City: " . $weather->city->name;
117+
echo "<br />\n";
118+
119+
echo "Temperature: " . $weather->temperature;
120+
echo "<br />\n";
121+
122+
// Example 6: Get information about a city.
123+
$weather = $owm->getWeather('Paris', $units, $lang);
124+
echo "<br /><br />\n\n\nEXAMPLE 6<hr />\n\n\n";
125+
126+
echo "Id: " . $weather->city->id;
127+
echo "<br />\n";
128+
129+
echo "Name: " . $weather->city->name;
130+
echo "<br />\n";
131+
132+
echo "Lon: " . $weather->city->lon;
133+
echo "<br />\n";
134+
135+
echo "Lat: " . $weather->city->lat;
136+
echo "<br />\n";
137+
138+
echo "Country: " . $weather->city->country;
139+
echo "<br />\n";
140+
141+
// Example 7: Get wind information.
142+
echo "<br /><br />\n\n\nEXAMPLE 7<hr />\n\n\n";
143+
144+
echo "Speed: " . $weather->wind->speed;
145+
echo "<br />\n";
146+
147+
echo "Direction: " . $weather->wind->direction;
148+
echo "<br />\n";
149+
150+
/**
151+
* @notice For speed and direction there is a description available, which isn't always translated.
152+
*/
153+
154+
echo "Speed: " . $weather->wind->speed->getDescription();
155+
echo "<br />\n";
156+
157+
echo "Direction: " . $weather->wind->direction->getDescription();
158+
echo "<br />\n";
159+
160+
// Example 8: Get information about the clouds.
161+
echo "<br /><br />\n\n\nEXAMPLE 8<hr />\n\n\n";
162+
163+
// The number in braces seems to be an indicator how cloudy the sky is.
164+
echo "Clouds: " . $weather->clouds->getDescription() . " (" . $weather->clouds . ")";
165+
echo "<br />\n";
166+
167+
// Example 9: Get information about precipitation.
168+
echo "<br /><br />\n\n\nEXAMPLE 9<hr />\n\n\n";
169+
170+
echo "Precipation: " . $weather->precipitation->getDescription() . " (" . $weather->precipitation . ")";
171+
echo "<br />\n";
172+
173+
// Example 10: Show copyright notice. WARNING: This is no offical text. This hint was made regarding to http://www.http://openweathermap.org/copyright .
174+
echo "<br /><br />\n\n\nEXAMPLE 10<hr />\n\n\n";
175+
176+
echo $weather->copyright;
177+
echo "<br />\n";
178+
179+
// Example 11: Get raw xml data.
180+
echo "<br /><br />\n\n\nEXAMPLE 11<hr />\n\n\n";
181+
182+
echo "<pre><code>" . htmlspecialchars($owm->getRawData('Berlin', $units, $lang, null, 'xml')) . "</code></pre>";
183+
echo "<br />\n";
184+
185+
// Example 12: Get raw json data.
186+
echo "<br /><br />\n\n\nEXAMPLE 12<hr />\n\n\n";
187+
188+
echo "<code>" . htmlspecialchars($owm->getRawData('Berlin', $units, $lang, null, 'json')) . "</code>";
189+
echo "<br />\n";
190+
191+
// Example 13: Get raw html data.
192+
echo "<br /><br />\n\n\nEXAMPLE 13<hr />\n\n\n";
193+
194+
echo $owm->getRawData('Berlin', $units, $lang, null, 'html');
195+
echo "<br />\n";
196+
197+
// Example 14: Error handling.
198+
echo "<br /><br />\n\n\nEXAMPLE 14<hr />\n\n\n";
199+
200+
// Try wrong city name.
201+
try {
202+
$weather = $owm->getWeather("ThisCityNameIsNotValidAndDoesNotExist", $units, $lang);
203+
} catch(OWMException $e) {
204+
echo $e->getMessage() . ' (Code ' . $e->getCode() . ').';
205+
echo "<br />\n";
206+
}
207+
208+
// Try invalid $query.
209+
try {
210+
$weather = $owm->getWeather(new \DateTime('now'), $units, $lang);
211+
} catch(\Exception $e) {
212+
echo $e->getMessage() . ' (Code ' . $e->getCode() . ').';
213+
echo "<br />\n";
214+
}
215+
216+
// Full error handling would look like this:
217+
try {
218+
$weather = $owm->getWeather(-1, $units, $lang);
219+
} catch(OWMException $e) {
220+
echo 'OpenWeatherMap exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').';
221+
echo "<br />\n";
222+
} catch(\Exception $e) {
223+
echo 'General exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').';
224+
echo "<br />\n";
225+
}
226+
227+
// Example 15: Using an api key:
228+
229+
# $owm->getWeather('Berlin', $units, $lang, 'Your-Api-Key-Here');
230+
# $owm->getRawData('Berlin', $units, $lang, 'Your-Api-Key-Here', 'json');

Examples_Forecast.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* OpenWeatherMap-PHP-API — An php api to parse weather data from http://www.OpenWeatherMap.org .
4+
*
5+
* @license MIT
6+
*
7+
* Please see the LICENSE file distributed with this source code for further
8+
* information regarding copyright and licensing.
9+
*
10+
* Please visit the following links to read about the usage policies and the license of OpenWeatherMap before using this class.
11+
* @see http://www.OpenWeatherMap.org
12+
* @see http://www.OpenWeatherMap.org/about
13+
* @see http://www.OpenWeatherMap.org/copyright
14+
* @see http://openweathermap.org/appid
15+
*/
16+
17+
use cmfcmf\OpenWeatherMap;
18+
use cmfcmf\OpenWeatherMap\Exception as OWMException;
19+
20+
require('cmfcmf/OpenWeatherMap.php');
21+
22+
// Language of data (try your own language here!):
23+
$lang = 'de';
24+
25+
// Units (can be 'metric' or 'imperial' [default]):
26+
$units = 'metric';
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+
31+
// Example 1: Get forecast for the next 10 days for Berlin.
32+
$forecast = $owm->getWeatherForecast('Berlin', $units, $lang, '', 10);
33+
echo "EXAMPLE 1<hr />\n\n\n";
34+
35+
echo "City: " . $forecast->city->name;
36+
echo "<br />\n";
37+
echo "LastUpdate: " . $forecast->lastUpdate->format('d.m.Y H:i');
38+
echo "<br />\n";
39+
echo "<br />\n";
40+
41+
foreach($forecast as $weather) {
42+
// Each $weather contains a cmfcmf\ForecastWeather object which is almost the same as the cmfcmf\Weather object.
43+
// Take a look into 'Examples_Current.php' to see the available options.
44+
echo "Weather forecast at " . $weather->time->day->format('d.m.Y') . " from " . $weather->time->from->format('H:i') . " to " . $weather->time->to->format('H:i');
45+
echo "<br />\n";
46+
echo $weather->temperature;
47+
echo "<br />\n";
48+
echo "---";
49+
echo "<br />\n";
50+
}
51+
52+
// Example 1: Get forecast for the next 3 days for Berlin.
53+
$forecast = $owm->getWeatherForecast('Berlin', $units, $lang, '', 3);
54+
echo "EXAMPLE 2<hr />\n\n\n";
55+
56+
foreach($forecast as $weather) {
57+
echo "Weather forecast at " . $weather->time->day->format('d.m.Y') . " from " . $weather->time->from->format('H:i') . " to " . $weather->time->to->format('H:i') . "<br />";
58+
echo $weather->temperature . "<br />";
59+
echo "---<br />";
60+
}

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ An php api to parse weather data from [OpenWeatherMap.org](http://www.OpenWeathe
44

55
If you are looking for an implementation for the [CMS Zikula](http://www.zikula.org), you may want to take a look at [cmfcmf/Weather](https://github.com/cmfcmf/Weather).
66

7-
For example code and how to use this api, please take a look into `Examples.php` or run it in your browser.
7+
For example code and how to use this api, please take a look into `Examples_*.php` files and run them in your browser.
8+
- `Examples_Current.php` Shows how to receive the current weather.
9+
- `Examples_Forecast.php` Shows how to receive weather forecasts.
10+
- `Examples_Cache.php` Shows how to implement a cache.
811

912
**Notice:** This api is not made by OpenWeatherMap, nor their offical php api.
1013

@@ -15,6 +18,7 @@ Example call
1518
use cmfcmf\OpenWeatherMap;
1619
use cmfcmf\OpenWeatherMap\Exception as OWMException;
1720

21+
// Remove this line if you are using composer.
1822
require('cmfcmf/OpenWeatherMap.php');
1923

2024
// Language of data (try your own language here!):
@@ -59,5 +63,5 @@ However if you like this and want to **support** _me_, you may want to **flattr*
5963

6064
Roadmap
6165
=======
62-
- [ ] Add forecast functionality
66+
- [x] Add forecast functionality
6367
- [x] Tell the guys of [OpenWeatherMap.org](http://www.OpenWeatherMap.org) that you made such an api.

0 commit comments

Comments
 (0)