]> git.mxchange.org Git - friendica-addons.git/blob - curweather/vendor/cmfcmf/openweathermap-php-api/Examples/CurrentWeather.php
added composer.json and needed libs
[friendica-addons.git] / curweather / vendor / cmfcmf / openweathermap-php-api / Examples / CurrentWeather.php
1 <?php
2 /**
3  * OpenWeatherMap-PHP-API — A 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
11  * OpenWeatherMap before using this class:
12  *
13  * @see http://www.OpenWeatherMap.org
14  * @see http://www.OpenWeatherMap.org/terms
15  * @see http://openweathermap.org/appid
16  */
17
18 use Cmfcmf\OpenWeatherMap;
19 use Cmfcmf\OpenWeatherMap\Exception as OWMException;
20
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');
24 } else {
25     // Library is part of a project.
26     /** @noinspection PhpIncludeInspection */
27     require('../../../autoload.php');
28 }
29
30 // Language of data (try your own language here!):
31 $lang = 'de';
32
33 // Units (can be 'metric' or 'imperial' [default]):
34 $units = 'metric';
35
36 // Get OpenWeatherMap object. Don't use caching (take a look into Example_Cache.php to see how it works).
37 $owm = new OpenWeatherMap();
38
39 // Example 1: Get current temperature in Berlin.
40 $weather = $owm->getWeather('Berlin', $units, $lang);
41 echo "EXAMPLE 1<hr />\n\n\n";
42
43 // $weather contains all available weather information for Berlin.
44 // Let's get the temperature:
45
46 // Returns it as formatted string (using __toString()):
47 echo $weather->temperature;
48 echo "<br />\n";
49
50 // Returns it as formatted string (using a method):
51 echo $weather->temperature->getFormatted();
52 echo "<br />\n";
53
54 // Returns the value only:
55 echo $weather->temperature->getValue();
56 echo "<br />\n";
57
58 // Returns the unit only:
59 echo $weather->temperature->getUnit();
60 echo "<br />\n";
61
62 /**
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()".
67  */
68
69 // Returns the current temperature:
70 echo "Current: " . $weather->temperature->now;
71 echo "<br />\n";
72
73 // Returns the minimum temperature:
74 echo "Minimum: " . $weather->temperature->min;
75 echo "<br />\n";
76
77 // Returns the maximum temperature:
78 echo "Maximum: " . $weather->temperature->max;
79 echo "<br />\n";
80
81 /**
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:
84  */
85 echo "Last update: " . $weather->lastUpdate->format('r');
86 echo "<br />\n";
87
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";
91
92 /**
93  * You can use the methods above to only get the value or the unit.
94  */
95
96 echo "Pressure: " . $weather->pressure;
97 echo "<br />\n";
98 echo "Humidity: " . $weather->humidity;
99 echo "<br />\n";
100
101 // Example 3: Get today's sunrise and sunset times.
102 echo "<br /><br />\n\n\nEXAMPLE 3<hr />\n\n\n";
103
104 /**
105  * These functions return a DateTime object.
106  */
107
108 echo "Sunrise: " . $weather->sun->rise->format('r');
109 echo "<br />\n";
110 echo "Sunset: " . $weather->sun->set->format('r');
111 echo "<br />\n";
112
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";
116
117 echo "Temperature: " . $weather->temperature;
118 echo "<br />\n";
119
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";
123
124 echo "City: " . $weather->city->name;
125 echo "<br />\n";
126
127 echo "Temperature: " . $weather->temperature;
128 echo "<br />\n";
129
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";
133
134 echo "Id: " . $weather->city->id;
135 echo "<br />\n";
136
137 echo "Name: " . $weather->city->name;
138 echo "<br />\n";
139
140 echo "Lon: " . $weather->city->lon;
141 echo "<br />\n";
142
143 echo "Lat: " . $weather->city->lat;
144 echo "<br />\n";
145
146 echo "Country: " . $weather->city->country;
147 echo "<br />\n";
148
149 // Example 7: Get wind information.
150 echo "<br /><br />\n\n\nEXAMPLE 7<hr />\n\n\n";
151
152 echo "Speed: " . $weather->wind->speed;
153 echo "<br />\n";
154
155 echo "Direction: " . $weather->wind->direction;
156 echo "<br />\n";
157
158 /**
159  * For speed and direction there is a description available, which isn't always translated.
160  */
161
162 echo "Speed: " . $weather->wind->speed->getDescription();
163 echo "<br />\n";
164
165 echo "Direction: " . $weather->wind->direction->getDescription();
166 echo "<br />\n";
167
168 // Example 8: Get information about the clouds.
169 echo "<br /><br />\n\n\nEXAMPLE 8<hr />\n\n\n";
170
171 // The number in braces seems to be an indicator how cloudy the sky is.
172 echo "Clouds: " . $weather->clouds->getDescription() . " (" . $weather->clouds . ")";
173 echo "<br />\n";
174
175 // Example 9: Get information about precipitation.
176 echo "<br /><br />\n\n\nEXAMPLE 9<hr />\n\n\n";
177
178 echo "Precipation: " . $weather->precipitation->getDescription() . " (" . $weather->precipitation . ")";
179 echo "<br />\n";
180
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";
183
184 echo $owm::COPYRIGHT;
185 echo "<br />\n";
186
187 // Example 11: Get raw xml data.
188 echo "<br /><br />\n\n\nEXAMPLE 11<hr />\n\n\n";
189
190 echo "<pre><code>" . htmlspecialchars($owm->getRawWeatherData('Berlin', $units, $lang, null, 'xml')) . "</code></pre>";
191 echo "<br />\n";
192
193 // Example 12: Get raw json data.
194 echo "<br /><br />\n\n\nEXAMPLE 12<hr />\n\n\n";
195
196 echo "<code>" . htmlspecialchars($owm->getRawWeatherData('Berlin', $units, $lang, null, 'json')) . "</code>";
197 echo "<br />\n";
198
199 // Example 13: Get raw html data.
200 echo "<br /><br />\n\n\nEXAMPLE 13<hr />\n\n\n";
201
202 echo $owm->getRawWeatherData('Berlin', $units, $lang, null, 'html');
203 echo "<br />\n";
204
205 // Example 14: Error handling.
206 echo "<br /><br />\n\n\nEXAMPLE 14<hr />\n\n\n";
207
208 // Try wrong city name.
209 try {
210     $weather = $owm->getWeather("ThisCityNameIsNotValidAndDoesNotExist", $units, $lang);
211 } catch (OWMException $e) {
212     echo $e->getMessage() . ' (Code ' . $e->getCode() . ').';
213     echo "<br />\n";
214 }
215
216 // Try invalid $query.
217 try {
218     $weather = $owm->getWeather(new \DateTime('now'), $units, $lang);
219 } catch (\Exception $e) {
220     echo $e->getMessage() . ' (Code ' . $e->getCode() . ').';
221     echo "<br />\n";
222 }
223
224 // Full error handling would look like this:
225 try {
226     $weather = $owm->getWeather(-1, $units, $lang);
227 } catch (OWMException $e) {
228     echo 'OpenWeatherMap exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').';
229     echo "<br />\n";
230 } catch (\Exception $e) {
231     echo 'General exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').';
232     echo "<br />\n";
233 }
234
235 // Example 15: Using an api key:
236 $owm->getWeather('Berlin', $units, $lang, 'Your-Api-Key-Here');