|
19 | 19 |
|
20 | 20 | require_once __DIR__ . '/bootstrap.php';
|
21 | 21 |
|
| 22 | +$cli = false; |
| 23 | +$lf = '<br>'; |
| 24 | +if (php_sapi_name() === 'cli') { |
| 25 | + $lf = "\n"; |
| 26 | + $cli = true; |
| 27 | +} |
| 28 | + |
22 | 29 | // Language of data (try your own language here!):
|
23 | 30 | $lang = 'de';
|
24 | 31 |
|
|
31 | 38 |
|
32 | 39 | // Example 1: Get current temperature in Berlin.
|
33 | 40 | $weather = $owm->getWeather('Berlin', $units, $lang);
|
34 |
| -echo "EXAMPLE 1<hr />\n\n\n"; |
| 41 | +echo "EXAMPLE 1$lf"; |
35 | 42 |
|
36 | 43 | // $weather contains all available weather information for Berlin.
|
37 | 44 | // Let's get the temperature:
|
38 | 45 |
|
39 | 46 | // Returns it as formatted string (using __toString()):
|
40 | 47 | echo $weather->temperature;
|
41 |
| -echo "<br />\n"; |
| 48 | +echo $lf; |
42 | 49 |
|
43 | 50 | // Returns it as formatted string (using a method):
|
44 | 51 | echo $weather->temperature->getFormatted();
|
45 |
| -echo "<br />\n"; |
| 52 | +echo $lf; |
46 | 53 |
|
47 | 54 | // Returns the value only:
|
48 | 55 | echo $weather->temperature->getValue();
|
49 |
| -echo "<br />\n"; |
| 56 | +echo $lf; |
50 | 57 |
|
51 | 58 | // Returns the unit only:
|
52 | 59 | echo $weather->temperature->getUnit();
|
53 |
| -echo "<br />\n"; |
| 60 | +echo $lf; |
54 | 61 |
|
55 | 62 | /*
|
56 | 63 | * In the example above we're using a "shortcut". OpenWeatherMap returns the minimum temperature of a day,
|
|
61 | 68 |
|
62 | 69 | // Returns the current temperature:
|
63 | 70 | echo 'Current: '.$weather->temperature->now;
|
64 |
| -echo "<br />\n"; |
| 71 | +echo $lf; |
65 | 72 |
|
66 | 73 | // Returns the minimum temperature:
|
67 | 74 | echo 'Minimum: '.$weather->temperature->min;
|
68 |
| -echo "<br />\n"; |
| 75 | +echo $lf; |
69 | 76 |
|
70 | 77 | // Returns the maximum temperature:
|
71 | 78 | echo 'Maximum: '.$weather->temperature->max;
|
72 |
| -echo "<br />\n"; |
| 79 | +echo $lf; |
73 | 80 |
|
74 | 81 | /*
|
75 | 82 | * When speaking about "current" and "now", this means when the weather data was last updated. You can get this
|
76 | 83 | * via a DateTime object:
|
77 | 84 | */
|
78 | 85 | echo 'Last update: '.$weather->lastUpdate->format('r');
|
79 |
| -echo "<br />\n"; |
| 86 | +echo $lf; |
80 | 87 |
|
81 | 88 | // Example 2: Get current pressure and humidity in Hongkong.
|
82 | 89 | $weather = $owm->getWeather('Hongkong', $units, $lang);
|
83 |
| -echo "<br /><br />\n\n\nEXAMPLE 2<hr />\n\n\n"; |
| 90 | +echo "$lf$lf EXAMPLE 2$lf"; |
84 | 91 |
|
85 | 92 | /*
|
86 | 93 | * You can use the methods above to only get the value or the unit.
|
87 | 94 | */
|
88 | 95 |
|
89 | 96 | echo 'Pressure: '.$weather->pressure;
|
90 |
| -echo "<br />\n"; |
| 97 | +echo $lf; |
91 | 98 | echo 'Humidity: '.$weather->humidity;
|
92 |
| -echo "<br />\n"; |
| 99 | +echo $lf; |
93 | 100 |
|
94 | 101 | // Example 3: Get today's sunrise and sunset times.
|
95 |
| -echo "<br /><br />\n\n\nEXAMPLE 3<hr />\n\n\n"; |
| 102 | +echo "$lf$lf EXAMPLE 3$lf"; |
96 | 103 |
|
97 | 104 | /*
|
98 | 105 | * These functions return a DateTime object.
|
99 | 106 | */
|
100 | 107 |
|
101 | 108 | echo 'Sunrise: '.$weather->sun->rise->format('r');
|
102 |
| -echo "<br />\n"; |
| 109 | +echo $lf; |
103 | 110 | echo 'Sunset: '.$weather->sun->set->format('r');
|
104 |
| -echo "<br />\n"; |
| 111 | +echo $lf; |
105 | 112 |
|
106 | 113 | // Example 4: Get current temperature from coordinates (Greenland :-) ).
|
107 | 114 | $weather = $owm->getWeather(array('lat' => 77.73038, 'lon' => 41.89604), $units, $lang);
|
108 |
| -echo "<br /><br />\n\n\nEXAMPLE 4<hr />\n\n\n"; |
| 115 | +echo "$lf$lf EXAMPLE 4$lf"; |
109 | 116 |
|
110 | 117 | echo 'Temperature: '.$weather->temperature;
|
111 |
| -echo "<br />\n"; |
| 118 | +echo $lf; |
112 | 119 |
|
113 | 120 | // Example 5: Get current temperature from city id. The city is an internal id used by OpenWeatherMap. See example 6 too.
|
114 | 121 | $weather = $owm->getWeather(2172797, $units, $lang);
|
115 |
| -echo "<br /><br />\n\n\nEXAMPLE 5<hr />\n\n\n"; |
| 122 | +echo "$lf$lf EXAMPLE 5$lf"; |
116 | 123 |
|
117 | 124 | echo 'City: '.$weather->city->name;
|
118 |
| -echo "<br />\n"; |
| 125 | +echo $lf; |
119 | 126 |
|
120 | 127 | echo 'Temperature: '.$weather->temperature;
|
121 |
| -echo "<br />\n"; |
| 128 | +echo $lf; |
122 | 129 |
|
123 | 130 | // Example 6: Get information about a city.
|
124 | 131 | $weather = $owm->getWeather('Paris', $units, $lang);
|
125 |
| -echo "<br /><br />\n\n\nEXAMPLE 6<hr />\n\n\n"; |
| 132 | +echo "$lf$lf EXAMPLE 6$lf"; |
126 | 133 |
|
127 | 134 | echo 'Id: '.$weather->city->id;
|
128 |
| -echo "<br />\n"; |
| 135 | +echo $lf; |
129 | 136 |
|
130 | 137 | echo 'Name: '.$weather->city->name;
|
131 |
| -echo "<br />\n"; |
| 138 | +echo $lf; |
132 | 139 |
|
133 | 140 | echo 'Lon: '.$weather->city->lon;
|
134 |
| -echo "<br />\n"; |
| 141 | +echo $lf; |
135 | 142 |
|
136 | 143 | echo 'Lat: '.$weather->city->lat;
|
137 |
| -echo "<br />\n"; |
| 144 | +echo $lf; |
138 | 145 |
|
139 | 146 | echo 'Country: '.$weather->city->country;
|
140 |
| -echo "<br />\n"; |
| 147 | +echo $lf; |
141 | 148 |
|
142 | 149 | // Example 7: Get wind information.
|
143 |
| -echo "<br /><br />\n\n\nEXAMPLE 7<hr />\n\n\n"; |
| 150 | +echo "$lf$lf EXAMPLE 7$lf"; |
144 | 151 |
|
145 | 152 | echo 'Speed: '.$weather->wind->speed;
|
146 |
| -echo "<br />\n"; |
| 153 | +echo $lf; |
147 | 154 |
|
148 | 155 | echo 'Direction: '.$weather->wind->direction;
|
149 |
| -echo "<br />\n"; |
| 156 | +echo $lf; |
150 | 157 |
|
151 | 158 | /*
|
152 | 159 | * For speed and direction there is a description available, which isn't always translated.
|
153 | 160 | */
|
154 | 161 |
|
155 | 162 | echo 'Speed: '.$weather->wind->speed->getDescription();
|
156 |
| -echo "<br />\n"; |
| 163 | +echo $lf; |
157 | 164 |
|
158 | 165 | echo 'Direction: '.$weather->wind->direction->getDescription();
|
159 |
| -echo "<br />\n"; |
| 166 | +echo $lf; |
160 | 167 |
|
161 | 168 | // Example 8: Get information about the clouds.
|
162 |
| -echo "<br /><br />\n\n\nEXAMPLE 8<hr />\n\n\n"; |
| 169 | +echo "$lf$lf EXAMPLE 8$lf"; |
163 | 170 |
|
164 | 171 | // The number in braces seems to be an indicator how cloudy the sky is.
|
165 | 172 | echo 'Clouds: '.$weather->clouds->getDescription().' ('.$weather->clouds.')';
|
166 |
| -echo "<br />\n"; |
| 173 | +echo $lf; |
167 | 174 |
|
168 | 175 | // Example 9: Get information about precipitation.
|
169 |
| -echo "<br /><br />\n\n\nEXAMPLE 9<hr />\n\n\n"; |
| 176 | +echo "$lf$lf EXAMPLE 9$lf"; |
170 | 177 |
|
171 | 178 | echo 'Precipation: '.$weather->precipitation->getDescription().' ('.$weather->precipitation.')';
|
172 |
| -echo "<br />\n"; |
| 179 | +echo $lf; |
173 | 180 |
|
174 | 181 | // Example 10: Show copyright notice. WARNING: This is no official text. This hint was created by looking at http://www.http://openweathermap.org/copyright .
|
175 |
| -echo "<br /><br />\n\n\nEXAMPLE 10<hr />\n\n\n"; |
| 182 | +echo "$lf$lf EXAMPLE 10$lf"; |
176 | 183 |
|
177 | 184 | echo $owm::COPYRIGHT;
|
178 |
| -echo "<br />\n"; |
| 185 | +echo $lf; |
179 | 186 |
|
180 | 187 | // Example 11: Retrieve weather icons.
|
181 |
| -echo "<br /><br />\n\n\nEXAMPLE 11<hr />\n\n\n"; |
| 188 | +echo "$lf$lf EXAMPLE 11$lf"; |
182 | 189 | $weather = $owm->getWeather('Berlin');
|
183 | 190 | echo $weather->weather->icon;
|
184 |
| -echo "<br />\n"; |
| 191 | +echo $lf; |
185 | 192 | echo $weather->weather->getIconUrl();
|
186 | 193 |
|
187 | 194 | // Example 12: Get raw xml data.
|
188 |
| -echo "<br /><br />\n\n\nEXAMPLE 12<hr />\n\n\n"; |
| 195 | +echo "$lf$lf EXAMPLE 12$lf"; |
189 | 196 |
|
190 |
| -echo '<pre><code>'.htmlspecialchars($owm->getRawWeatherData('Berlin', $units, $lang, null, 'xml')).'</code></pre>'; |
191 |
| -echo "<br />\n"; |
| 197 | +$xml = $owm->getRawWeatherData('Berlin', $units, $lang, null, 'xml'); |
| 198 | +if ($cli) { |
| 199 | + echo $xml; |
| 200 | +} else { |
| 201 | + echo '<pre><code>'.htmlspecialchars($xml).'</code></pre>'; |
| 202 | +} |
| 203 | +echo $lf; |
192 | 204 |
|
193 | 205 | // Example 13: Get raw json data.
|
194 |
| -echo "<br /><br />\n\n\nEXAMPLE 13<hr />\n\n\n"; |
| 206 | +echo "$lf$lf EXAMPLE 13$lf"; |
195 | 207 |
|
196 |
| -echo '<code>'.htmlspecialchars($owm->getRawWeatherData('Berlin', $units, $lang, null, 'json')).'</code>'; |
197 |
| -echo "<br />\n"; |
| 208 | +$json = $owm->getRawWeatherData('Berlin', $units, $lang, null, 'json'); |
| 209 | +if ($cli) { |
| 210 | + echo $json; |
| 211 | +} else { |
| 212 | + echo '<pre><code>'.htmlspecialchars($json).'</code></pre>'; |
| 213 | +} |
| 214 | +echo $lf; |
198 | 215 |
|
199 | 216 | // Example 14: Get raw html data.
|
200 |
| -echo "<br /><br />\n\n\nEXAMPLE 14<hr />\n\n\n"; |
| 217 | +echo "$lf$lf EXAMPLE 14$lf"; |
201 | 218 |
|
202 | 219 | echo $owm->getRawWeatherData('Berlin', $units, $lang, null, 'html');
|
203 |
| -echo "<br />\n"; |
| 220 | +echo $lf; |
204 | 221 |
|
205 | 222 | // Example 15: Error handling.
|
206 |
| -echo "<br /><br />\n\n\nEXAMPLE 15<hr />\n\n\n"; |
| 223 | +echo "$lf$lf EXAMPLE 15$lf"; |
207 | 224 |
|
208 | 225 | // Try wrong city name.
|
209 | 226 | try {
|
210 | 227 | $weather = $owm->getWeather('ThisCityNameIsNotValidAndDoesNotExist', $units, $lang);
|
211 | 228 | } catch (OWMException $e) {
|
212 | 229 | echo $e->getMessage().' (Code '.$e->getCode().').';
|
213 |
| - echo "<br />\n"; |
| 230 | + echo $lf; |
214 | 231 | }
|
215 | 232 |
|
216 | 233 | // Try invalid $query.
|
217 | 234 | try {
|
218 | 235 | $weather = $owm->getWeather(new \DateTime('now'), $units, $lang);
|
219 | 236 | } catch (\Exception $e) {
|
220 | 237 | echo $e->getMessage().' (Code '.$e->getCode().').';
|
221 |
| - echo "<br />\n"; |
| 238 | + echo $lf; |
222 | 239 | }
|
223 | 240 |
|
224 | 241 | // Full error handling would look like this:
|
225 | 242 | try {
|
226 | 243 | $weather = $owm->getWeather(-1, $units, $lang);
|
227 | 244 | } catch (OWMException $e) {
|
228 | 245 | echo 'OpenWeatherMap exception: '.$e->getMessage().' (Code '.$e->getCode().').';
|
229 |
| - echo "<br />\n"; |
| 246 | + echo $lf; |
230 | 247 | } catch (\Exception $e) {
|
231 | 248 | echo 'General exception: '.$e->getMessage().' (Code '.$e->getCode().').';
|
232 |
| - echo "<br />\n"; |
| 249 | + echo $lf; |
233 | 250 | }
|
0 commit comments