Skip to content

Commit 813cfba

Browse files
committed
Improve examples.
1 parent 1862b4e commit 813cfba

File tree

1 file changed

+69
-52
lines changed

1 file changed

+69
-52
lines changed

Examples/CurrentWeather.php

Lines changed: 69 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919

2020
require_once __DIR__ . '/bootstrap.php';
2121

22+
$cli = false;
23+
$lf = '<br>';
24+
if (php_sapi_name() === 'cli') {
25+
$lf = "\n";
26+
$cli = true;
27+
}
28+
2229
// Language of data (try your own language here!):
2330
$lang = 'de';
2431

@@ -31,26 +38,26 @@
3138

3239
// Example 1: Get current temperature in Berlin.
3340
$weather = $owm->getWeather('Berlin', $units, $lang);
34-
echo "EXAMPLE 1<hr />\n\n\n";
41+
echo "EXAMPLE 1$lf";
3542

3643
// $weather contains all available weather information for Berlin.
3744
// Let's get the temperature:
3845

3946
// Returns it as formatted string (using __toString()):
4047
echo $weather->temperature;
41-
echo "<br />\n";
48+
echo $lf;
4249

4350
// Returns it as formatted string (using a method):
4451
echo $weather->temperature->getFormatted();
45-
echo "<br />\n";
52+
echo $lf;
4653

4754
// Returns the value only:
4855
echo $weather->temperature->getValue();
49-
echo "<br />\n";
56+
echo $lf;
5057

5158
// Returns the unit only:
5259
echo $weather->temperature->getUnit();
53-
echo "<br />\n";
60+
echo $lf;
5461

5562
/*
5663
* In the example above we're using a "shortcut". OpenWeatherMap returns the minimum temperature of a day,
@@ -61,173 +68,183 @@
6168

6269
// Returns the current temperature:
6370
echo 'Current: '.$weather->temperature->now;
64-
echo "<br />\n";
71+
echo $lf;
6572

6673
// Returns the minimum temperature:
6774
echo 'Minimum: '.$weather->temperature->min;
68-
echo "<br />\n";
75+
echo $lf;
6976

7077
// Returns the maximum temperature:
7178
echo 'Maximum: '.$weather->temperature->max;
72-
echo "<br />\n";
79+
echo $lf;
7380

7481
/*
7582
* When speaking about "current" and "now", this means when the weather data was last updated. You can get this
7683
* via a DateTime object:
7784
*/
7885
echo 'Last update: '.$weather->lastUpdate->format('r');
79-
echo "<br />\n";
86+
echo $lf;
8087

8188
// Example 2: Get current pressure and humidity in Hongkong.
8289
$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";
8491

8592
/*
8693
* You can use the methods above to only get the value or the unit.
8794
*/
8895

8996
echo 'Pressure: '.$weather->pressure;
90-
echo "<br />\n";
97+
echo $lf;
9198
echo 'Humidity: '.$weather->humidity;
92-
echo "<br />\n";
99+
echo $lf;
93100

94101
// 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";
96103

97104
/*
98105
* These functions return a DateTime object.
99106
*/
100107

101108
echo 'Sunrise: '.$weather->sun->rise->format('r');
102-
echo "<br />\n";
109+
echo $lf;
103110
echo 'Sunset: '.$weather->sun->set->format('r');
104-
echo "<br />\n";
111+
echo $lf;
105112

106113
// Example 4: Get current temperature from coordinates (Greenland :-) ).
107114
$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";
109116

110117
echo 'Temperature: '.$weather->temperature;
111-
echo "<br />\n";
118+
echo $lf;
112119

113120
// Example 5: Get current temperature from city id. The city is an internal id used by OpenWeatherMap. See example 6 too.
114121
$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";
116123

117124
echo 'City: '.$weather->city->name;
118-
echo "<br />\n";
125+
echo $lf;
119126

120127
echo 'Temperature: '.$weather->temperature;
121-
echo "<br />\n";
128+
echo $lf;
122129

123130
// Example 6: Get information about a city.
124131
$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";
126133

127134
echo 'Id: '.$weather->city->id;
128-
echo "<br />\n";
135+
echo $lf;
129136

130137
echo 'Name: '.$weather->city->name;
131-
echo "<br />\n";
138+
echo $lf;
132139

133140
echo 'Lon: '.$weather->city->lon;
134-
echo "<br />\n";
141+
echo $lf;
135142

136143
echo 'Lat: '.$weather->city->lat;
137-
echo "<br />\n";
144+
echo $lf;
138145

139146
echo 'Country: '.$weather->city->country;
140-
echo "<br />\n";
147+
echo $lf;
141148

142149
// Example 7: Get wind information.
143-
echo "<br /><br />\n\n\nEXAMPLE 7<hr />\n\n\n";
150+
echo "$lf$lf EXAMPLE 7$lf";
144151

145152
echo 'Speed: '.$weather->wind->speed;
146-
echo "<br />\n";
153+
echo $lf;
147154

148155
echo 'Direction: '.$weather->wind->direction;
149-
echo "<br />\n";
156+
echo $lf;
150157

151158
/*
152159
* For speed and direction there is a description available, which isn't always translated.
153160
*/
154161

155162
echo 'Speed: '.$weather->wind->speed->getDescription();
156-
echo "<br />\n";
163+
echo $lf;
157164

158165
echo 'Direction: '.$weather->wind->direction->getDescription();
159-
echo "<br />\n";
166+
echo $lf;
160167

161168
// 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";
163170

164171
// The number in braces seems to be an indicator how cloudy the sky is.
165172
echo 'Clouds: '.$weather->clouds->getDescription().' ('.$weather->clouds.')';
166-
echo "<br />\n";
173+
echo $lf;
167174

168175
// 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";
170177

171178
echo 'Precipation: '.$weather->precipitation->getDescription().' ('.$weather->precipitation.')';
172-
echo "<br />\n";
179+
echo $lf;
173180

174181
// 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";
176183

177184
echo $owm::COPYRIGHT;
178-
echo "<br />\n";
185+
echo $lf;
179186

180187
// Example 11: Retrieve weather icons.
181-
echo "<br /><br />\n\n\nEXAMPLE 11<hr />\n\n\n";
188+
echo "$lf$lf EXAMPLE 11$lf";
182189
$weather = $owm->getWeather('Berlin');
183190
echo $weather->weather->icon;
184-
echo "<br />\n";
191+
echo $lf;
185192
echo $weather->weather->getIconUrl();
186193

187194
// 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";
189196

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;
192204

193205
// 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";
195207

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;
198215

199216
// 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";
201218

202219
echo $owm->getRawWeatherData('Berlin', $units, $lang, null, 'html');
203-
echo "<br />\n";
220+
echo $lf;
204221

205222
// Example 15: Error handling.
206-
echo "<br /><br />\n\n\nEXAMPLE 15<hr />\n\n\n";
223+
echo "$lf$lf EXAMPLE 15$lf";
207224

208225
// Try wrong city name.
209226
try {
210227
$weather = $owm->getWeather('ThisCityNameIsNotValidAndDoesNotExist', $units, $lang);
211228
} catch (OWMException $e) {
212229
echo $e->getMessage().' (Code '.$e->getCode().').';
213-
echo "<br />\n";
230+
echo $lf;
214231
}
215232

216233
// Try invalid $query.
217234
try {
218235
$weather = $owm->getWeather(new \DateTime('now'), $units, $lang);
219236
} catch (\Exception $e) {
220237
echo $e->getMessage().' (Code '.$e->getCode().').';
221-
echo "<br />\n";
238+
echo $lf;
222239
}
223240

224241
// Full error handling would look like this:
225242
try {
226243
$weather = $owm->getWeather(-1, $units, $lang);
227244
} catch (OWMException $e) {
228245
echo 'OpenWeatherMap exception: '.$e->getMessage().' (Code '.$e->getCode().').';
229-
echo "<br />\n";
246+
echo $lf;
230247
} catch (\Exception $e) {
231248
echo 'General exception: '.$e->getMessage().' (Code '.$e->getCode().').';
232-
echo "<br />\n";
249+
echo $lf;
233250
}

0 commit comments

Comments
 (0)