3 * OpenWeatherMap-PHP-API — A php api to parse weather data from http://www.OpenWeatherMap.org .
7 * Please see the LICENSE file distributed with this source code for further
8 * information regarding copyright and licensing.
10 * Please visit the following links to read about the usage policies and the license of
11 * OpenWeatherMap before using this class:
13 * @see http://www.OpenWeatherMap.org
14 * @see http://www.OpenWeatherMap.org/terms
15 * @see http://openweathermap.org/appid
18 use Cmfcmf\OpenWeatherMap;
19 use Cmfcmf\OpenWeatherMap\Exception as OWMException;
21 if (file_exists('../vendor/autoload.php')) {
22 // Library is not part of a project. "composer install" was executed directly on this library's composer file.
23 require('../vendor/autoload.php');
25 // Library is part of a project.
26 /** @noinspection PhpIncludeInspection */
27 require('../../../autoload.php');
30 // Language of data (try your own language here!):
33 // Units (can be 'metric' or 'imperial' [default]):
36 // Get OpenWeatherMap object. Don't use caching (take a look into Example_Cache.php to see how it works).
37 $owm = new OpenWeatherMap();
39 // Example 1: Get current temperature in Berlin.
40 $weather = $owm->getWeather('Berlin', $units, $lang);
41 echo "EXAMPLE 1<hr />\n\n\n";
43 // $weather contains all available weather information for Berlin.
44 // Let's get the temperature:
46 // Returns it as formatted string (using __toString()):
47 echo $weather->temperature;
50 // Returns it as formatted string (using a method):
51 echo $weather->temperature->getFormatted();
54 // Returns the value only:
55 echo $weather->temperature->getValue();
58 // Returns the unit only:
59 echo $weather->temperature->getUnit();
63 * In the example above we're using a "shortcut". OpenWeatherMap returns the minimum temperature of a day,
64 * the maximum temperature and the temperature right now. If you don't specify which temperature you want, it will default
65 * to the current temperature. See below how to access the other values. Notice that each of them has implemented the methods
66 * "getFormatted()", "getValue()", "getUnit()".
69 // Returns the current temperature:
70 echo "Current: " . $weather->temperature->now;
73 // Returns the minimum temperature:
74 echo "Minimum: " . $weather->temperature->min;
77 // Returns the maximum temperature:
78 echo "Maximum: " . $weather->temperature->max;
82 * When speaking about "current" and "now", this means when the weather data was last updated. You can get this
83 * via a DateTime object:
85 echo "Last update: " . $weather->lastUpdate->format('r');
88 // Example 2: Get current pressure and humidity in Hongkong.
89 $weather = $owm->getWeather('Hongkong', $units, $lang);
90 echo "<br /><br />\n\n\nEXAMPLE 2<hr />\n\n\n";
93 * You can use the methods above to only get the value or the unit.
96 echo "Pressure: " . $weather->pressure;
98 echo "Humidity: " . $weather->humidity;
101 // Example 3: Get today's sunrise and sunset times.
102 echo "<br /><br />\n\n\nEXAMPLE 3<hr />\n\n\n";
105 * These functions return a DateTime object.
108 echo "Sunrise: " . $weather->sun->rise->format('r');
110 echo "Sunset: " . $weather->sun->set->format('r');
113 // Example 4: Get current temperature from coordinates (Greenland :-) ).
114 $weather = $owm->getWeather(array('lat' => 77.73038, 'lon' => 41.89604), $units, $lang);
115 echo "<br /><br />\n\n\nEXAMPLE 4<hr />\n\n\n";
117 echo "Temperature: " . $weather->temperature;
120 // Example 5: Get current temperature from city id. The city is an internal id used by OpenWeatherMap. See example 6 too.
121 $weather = $owm->getWeather(2172797, $units, $lang);
122 echo "<br /><br />\n\n\nEXAMPLE 5<hr />\n\n\n";
124 echo "City: " . $weather->city->name;
127 echo "Temperature: " . $weather->temperature;
130 // Example 6: Get information about a city.
131 $weather = $owm->getWeather('Paris', $units, $lang);
132 echo "<br /><br />\n\n\nEXAMPLE 6<hr />\n\n\n";
134 echo "Id: " . $weather->city->id;
137 echo "Name: " . $weather->city->name;
140 echo "Lon: " . $weather->city->lon;
143 echo "Lat: " . $weather->city->lat;
146 echo "Country: " . $weather->city->country;
149 // Example 7: Get wind information.
150 echo "<br /><br />\n\n\nEXAMPLE 7<hr />\n\n\n";
152 echo "Speed: " . $weather->wind->speed;
155 echo "Direction: " . $weather->wind->direction;
159 * For speed and direction there is a description available, which isn't always translated.
162 echo "Speed: " . $weather->wind->speed->getDescription();
165 echo "Direction: " . $weather->wind->direction->getDescription();
168 // Example 8: Get information about the clouds.
169 echo "<br /><br />\n\n\nEXAMPLE 8<hr />\n\n\n";
171 // The number in braces seems to be an indicator how cloudy the sky is.
172 echo "Clouds: " . $weather->clouds->getDescription() . " (" . $weather->clouds . ")";
175 // Example 9: Get information about precipitation.
176 echo "<br /><br />\n\n\nEXAMPLE 9<hr />\n\n\n";
178 echo "Precipation: " . $weather->precipitation->getDescription() . " (" . $weather->precipitation . ")";
181 // Example 10: Show copyright notice. WARNING: This is no offical text. This hint was created regarding to http://www.http://openweathermap.org/copyright .
182 echo "<br /><br />\n\n\nEXAMPLE 10<hr />\n\n\n";
184 echo $owm::COPYRIGHT;
187 // Example 11: Get raw xml data.
188 echo "<br /><br />\n\n\nEXAMPLE 11<hr />\n\n\n";
190 echo "<pre><code>" . htmlspecialchars($owm->getRawWeatherData('Berlin', $units, $lang, null, 'xml')) . "</code></pre>";
193 // Example 12: Get raw json data.
194 echo "<br /><br />\n\n\nEXAMPLE 12<hr />\n\n\n";
196 echo "<code>" . htmlspecialchars($owm->getRawWeatherData('Berlin', $units, $lang, null, 'json')) . "</code>";
199 // Example 13: Get raw html data.
200 echo "<br /><br />\n\n\nEXAMPLE 13<hr />\n\n\n";
202 echo $owm->getRawWeatherData('Berlin', $units, $lang, null, 'html');
205 // Example 14: Error handling.
206 echo "<br /><br />\n\n\nEXAMPLE 14<hr />\n\n\n";
208 // Try wrong city name.
210 $weather = $owm->getWeather("ThisCityNameIsNotValidAndDoesNotExist", $units, $lang);
211 } catch (OWMException $e) {
212 echo $e->getMessage() . ' (Code ' . $e->getCode() . ').';
216 // Try invalid $query.
218 $weather = $owm->getWeather(new \DateTime('now'), $units, $lang);
219 } catch (\Exception $e) {
220 echo $e->getMessage() . ' (Code ' . $e->getCode() . ').';
224 // Full error handling would look like this:
226 $weather = $owm->getWeather(-1, $units, $lang);
227 } catch (OWMException $e) {
228 echo 'OpenWeatherMap exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').';
230 } catch (\Exception $e) {
231 echo 'General exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').';
235 // Example 15: Using an api key:
236 $owm->getWeather('Berlin', $units, $lang, 'Your-Api-Key-Here');