getWeather('Berlin', $units, $lang);
echo "EXAMPLE 1
\n\n\n";
// $weather contains all available weather information for Berlin.
// Let's get the temperature:
// Returns it as formatted string (using __toString()):
echo $weather->temperature;
echo "
\n";
// Returns it as formatted string (using a method):
echo $weather->temperature->getFormatted();
echo "
\n";
// Returns the value only:
echo $weather->temperature->getValue();
echo "
\n";
// Returns the unit only:
echo $weather->temperature->getUnit();
echo "
\n";
/**
* In the example above we're using a "shortcut". OpenWeatherMap returns the minimum temperature of a day,
* the maximum temperature and the temperature right now. If you don't specify which temperature you want, it will default
* to the current temperature. See below how to access the other values. Notice that each of them has implemented the methods
* "getFormatted()", "getValue()", "getUnit()".
*/
// Returns the current temperature:
echo "Current: " . $weather->temperature->now;
echo "
\n";
// Returns the minimum temperature:
echo "Minimum: " . $weather->temperature->min;
echo "
\n";
// Returns the maximum temperature:
echo "Maximum: " . $weather->temperature->max;
echo "
\n";
/**
* When speaking about "current" and "now", this means when the weather data was last updated. You can get this
* via a DateTime object:
*/
echo "Last update: " . $weather->lastUpdate->format('r');
echo "
\n";
// Example 2: Get current pressure and humidity in Hongkong.
$weather = $owm->getWeather('Hongkong', $units, $lang);
echo "
\n\n\nEXAMPLE 2
\n\n\n";
/**
* You can use the methods above to only get the value or the unit.
*/
echo "Pressure: " . $weather->pressure;
echo "
\n";
echo "Humidity: " . $weather->humidity;
echo "
\n";
// Example 3: Get today's sunrise and sunset times.
echo "
\n\n\nEXAMPLE 3
\n\n\n";
/**
* These functions return a DateTime object.
*/
echo "Sunrise: " . $weather->sun->rise->format('r');
echo "
\n";
echo "Sunset: " . $weather->sun->set->format('r');
echo "
\n";
// Example 4: Get current temperature from coordinates (Greenland :-) ).
$weather = $owm->getWeather(array('lat' => 77.73038, 'lon' => 41.89604), $units, $lang);
echo "
\n\n\nEXAMPLE 4
\n\n\n";
echo "Temperature: " . $weather->temperature;
echo "
\n";
// Example 5: Get current temperature from city id. The city is an internal id used by OpenWeatherMap. See example 6 too.
$weather = $owm->getWeather(2172797, $units, $lang);
echo "
\n\n\nEXAMPLE 5
\n\n\n";
echo "City: " . $weather->city->name;
echo "
\n";
echo "Temperature: " . $weather->temperature;
echo "
\n";
// Example 6: Get information about a city.
$weather = $owm->getWeather('Paris', $units, $lang);
echo "
\n\n\nEXAMPLE 6
\n\n\n";
echo "Id: " . $weather->city->id;
echo "
\n";
echo "Name: " . $weather->city->name;
echo "
\n";
echo "Lon: " . $weather->city->lon;
echo "
\n";
echo "Lat: " . $weather->city->lat;
echo "
\n";
echo "Country: " . $weather->city->country;
echo "
\n";
// Example 7: Get wind information.
echo "
\n\n\nEXAMPLE 7
\n\n\n";
echo "Speed: " . $weather->wind->speed;
echo "
\n";
echo "Direction: " . $weather->wind->direction;
echo "
\n";
/**
* For speed and direction there is a description available, which isn't always translated.
*/
echo "Speed: " . $weather->wind->speed->getDescription();
echo "
\n";
echo "Direction: " . $weather->wind->direction->getDescription();
echo "
\n";
// Example 8: Get information about the clouds.
echo "
\n\n\nEXAMPLE 8
\n\n\n";
// The number in braces seems to be an indicator how cloudy the sky is.
echo "Clouds: " . $weather->clouds->getDescription() . " (" . $weather->clouds . ")";
echo "
\n";
// Example 9: Get information about precipitation.
echo "
\n\n\nEXAMPLE 9
\n\n\n";
echo "Precipation: " . $weather->precipitation->getDescription() . " (" . $weather->precipitation . ")";
echo "
\n";
// Example 10: Show copyright notice. WARNING: This is no offical text. This hint was created regarding to http://www.http://openweathermap.org/copyright .
echo "
\n\n\nEXAMPLE 10
\n\n\n";
echo $owm::COPYRIGHT;
echo "
\n";
// Example 11: Get raw xml data.
echo "
\n\n\nEXAMPLE 11
\n\n\n";
echo "" . htmlspecialchars($owm->getRawWeatherData('Berlin', $units, $lang, null, 'xml')) . "
";
echo "
\n";
// Example 12: Get raw json data.
echo "
\n\n\nEXAMPLE 12
\n\n\n";
echo "" . htmlspecialchars($owm->getRawWeatherData('Berlin', $units, $lang, null, 'json')) . "
";
echo "
\n";
// Example 13: Get raw html data.
echo "
\n\n\nEXAMPLE 13
\n\n\n";
echo $owm->getRawWeatherData('Berlin', $units, $lang, null, 'html');
echo "
\n";
// Example 14: Error handling.
echo "
\n\n\nEXAMPLE 14
\n\n\n";
// Try wrong city name.
try {
$weather = $owm->getWeather("ThisCityNameIsNotValidAndDoesNotExist", $units, $lang);
} catch (OWMException $e) {
echo $e->getMessage() . ' (Code ' . $e->getCode() . ').';
echo "
\n";
}
// Try invalid $query.
try {
$weather = $owm->getWeather(new \DateTime('now'), $units, $lang);
} catch (\Exception $e) {
echo $e->getMessage() . ' (Code ' . $e->getCode() . ').';
echo "
\n";
}
// Full error handling would look like this:
try {
$weather = $owm->getWeather(-1, $units, $lang);
} catch (OWMException $e) {
echo 'OpenWeatherMap exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').';
echo "
\n";
} catch (\Exception $e) {
echo 'General exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').';
echo "
\n";
}
// Example 15: Using an api key:
$owm->getWeather('Berlin', $units, $lang, 'Your-Api-Key-Here');