-
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
640 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
<?php | ||
/** | ||
* OpenWeatherMap-PHP-API — An php api to parse weather data from http://www.OpenWeatherMap.org . | ||
* | ||
* @license MIT | ||
* | ||
* Please see the LICENSE file distributed with this source code for further | ||
* information regarding copyright and licensing. | ||
* | ||
* Please visit the following links to read about the usage policies and the license of OpenWeatherMap before using this class. | ||
* @see http://www.OpenWeatherMap.org | ||
* @see http://www.OpenWeatherMap.org/about | ||
* @see http://www.OpenWeatherMap.org/copyright | ||
* @see http://openweathermap.org/appid | ||
*/ | ||
|
||
use cmfcmf\OpenWeatherMap; | ||
use cmfcmf\OpenWeatherMap\Exception as OWMException; | ||
|
||
require('cmfcmf/OpenWeatherMap.php'); | ||
|
||
// Language of data (try your own language here!): | ||
$lang = 'de'; | ||
|
||
// Units (can be 'metric' or 'imperial' [default]): | ||
$units = 'metric'; | ||
|
||
// Get OpenWeatherMap object. Don't use caching (take a look into Example_Cache.php to see how it works). | ||
$owm = new OpenWeatherMap(); | ||
|
||
// Example 1: Get current temperature in Berlin. | ||
$weather = $owm->getWeather('Berlin', $units, $lang); | ||
echo "EXAMPLE 1<hr />\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 "<br />\n"; | ||
|
||
// Returns it as formatted string (using a method): | ||
echo $weather->temperature->getFormatted(); | ||
echo "<br />\n"; | ||
|
||
// Returns the value only: | ||
echo $weather->temperature->getValue(); | ||
echo "<br />\n"; | ||
|
||
// Returns the unit only: | ||
echo $weather->temperature->getUnit(); | ||
echo "<br />\n"; | ||
|
||
/** | ||
* @notice 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 "<br />\n"; | ||
|
||
// Returns the minimum temperature: | ||
echo "Minimum: " . $weather->temperature->min; | ||
echo "<br />\n"; | ||
|
||
// Returns the maximum temperature: | ||
echo "Maximum: " . $weather->temperature->max; | ||
echo "<br />\n"; | ||
|
||
/** | ||
* @notice 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 "<br />\n"; | ||
|
||
// Example 2: Get current pressure and humidity in Hongkong. | ||
$weather = $owm->getWeather('Hongkong', $units, $lang); | ||
echo "<br /><br />\n\n\nEXAMPLE 2<hr />\n\n\n"; | ||
|
||
/** | ||
* @notice You can use the methods above to only get the value or the unit. | ||
*/ | ||
|
||
echo "Pressure: " . $weather->pressure; | ||
echo "<br />\n"; | ||
echo "Humidity: " . $weather->humidity; | ||
echo "<br />\n"; | ||
|
||
// Example 3: Get today's sunrise and sunset times. | ||
echo "<br /><br />\n\n\nEXAMPLE 3<hr />\n\n\n"; | ||
|
||
/** | ||
* @notice These functions return a DateTime object. | ||
*/ | ||
|
||
echo "Sunrise: " . $weather->sun->rise->format('r'); | ||
echo "<br />\n"; | ||
echo "Sunset: " . $weather->sun->set->format('r'); | ||
echo "<br />\n"; | ||
|
||
// Example 4: Get current temperature from coordinates (Greenland :-) ). | ||
$weather = $owm->getWeather(array('lat' => 77.73038, 'lon' => 41.89604), $units, $lang); | ||
echo "<br /><br />\n\n\nEXAMPLE 4<hr />\n\n\n"; | ||
|
||
echo "Temperature: " . $weather->temperature; | ||
echo "<br />\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 "<br /><br />\n\n\nEXAMPLE 5<hr />\n\n\n"; | ||
|
||
echo "City: " . $weather->city->name; | ||
echo "<br />\n"; | ||
|
||
echo "Temperature: " . $weather->temperature; | ||
echo "<br />\n"; | ||
|
||
// Example 6: Get information about a city. | ||
$weather = $owm->getWeather('Paris', $units, $lang); | ||
echo "<br /><br />\n\n\nEXAMPLE 6<hr />\n\n\n"; | ||
|
||
echo "Id: " . $weather->city->id; | ||
echo "<br />\n"; | ||
|
||
echo "Name: " . $weather->city->name; | ||
echo "<br />\n"; | ||
|
||
echo "Lon: " . $weather->city->lon; | ||
echo "<br />\n"; | ||
|
||
echo "Lat: " . $weather->city->lat; | ||
echo "<br />\n"; | ||
|
||
echo "Country: " . $weather->city->country; | ||
echo "<br />\n"; | ||
|
||
// Example 7: Get wind information. | ||
echo "<br /><br />\n\n\nEXAMPLE 7<hr />\n\n\n"; | ||
|
||
echo "Speed: " . $weather->wind->speed; | ||
echo "<br />\n"; | ||
|
||
echo "Direction: " . $weather->wind->direction; | ||
echo "<br />\n"; | ||
|
||
/** | ||
* @notice For speed and direction there is a description available, which isn't always translated. | ||
*/ | ||
|
||
echo "Speed: " . $weather->wind->speed->getDescription(); | ||
echo "<br />\n"; | ||
|
||
echo "Direction: " . $weather->wind->direction->getDescription(); | ||
echo "<br />\n"; | ||
|
||
// Example 8: Get information about the clouds. | ||
echo "<br /><br />\n\n\nEXAMPLE 8<hr />\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 "<br />\n"; | ||
|
||
// Example 9: Get information about precipitation. | ||
echo "<br /><br />\n\n\nEXAMPLE 9<hr />\n\n\n"; | ||
|
||
echo "Precipation: " . $weather->precipitation->getDescription() . " (" . $weather->precipitation . ")"; | ||
echo "<br />\n"; | ||
|
||
// Example 10: Show copyright notice. WARNING: This is no offical text. This hint was made regarding to http://www.http://openweathermap.org/copyright . | ||
echo "<br /><br />\n\n\nEXAMPLE 10<hr />\n\n\n"; | ||
|
||
echo $weather->copyright; | ||
echo "<br />\n"; | ||
|
||
// Example 11: Get raw xml data. | ||
echo "<br /><br />\n\n\nEXAMPLE 11<hr />\n\n\n"; | ||
|
||
echo "<pre><code>" . htmlspecialchars($owm->getRawData('Berlin', $units, $lang, null, 'xml')) . "</code></pre>"; | ||
echo "<br />\n"; | ||
|
||
// Example 12: Get raw json data. | ||
echo "<br /><br />\n\n\nEXAMPLE 12<hr />\n\n\n"; | ||
|
||
echo "<code>" . htmlspecialchars($owm->getRawData('Berlin', $units, $lang, null, 'json')) . "</code>"; | ||
echo "<br />\n"; | ||
|
||
// Example 13: Get raw html data. | ||
echo "<br /><br />\n\n\nEXAMPLE 13<hr />\n\n\n"; | ||
|
||
echo $owm->getRawData('Berlin', $units, $lang, null, 'html'); | ||
echo "<br />\n"; | ||
|
||
// Example 14: Error handling. | ||
echo "<br /><br />\n\n\nEXAMPLE 14<hr />\n\n\n"; | ||
|
||
// Try wrong city name. | ||
try { | ||
$weather = $owm->getWeather("ThisCityNameIsNotValidAndDoesNotExist", $units, $lang); | ||
} catch(OWMException $e) { | ||
echo $e->getMessage() . ' (Code ' . $e->getCode() . ').'; | ||
echo "<br />\n"; | ||
} | ||
|
||
// Try invalid $query. | ||
try { | ||
$weather = $owm->getWeather(new \DateTime('now'), $units, $lang); | ||
} catch(\Exception $e) { | ||
echo $e->getMessage() . ' (Code ' . $e->getCode() . ').'; | ||
echo "<br />\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 "<br />\n"; | ||
} catch(\Exception $e) { | ||
echo 'General exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').'; | ||
echo "<br />\n"; | ||
} | ||
|
||
// Example 15: Using an api key: | ||
|
||
# $owm->getWeather('Berlin', $units, $lang, 'Your-Api-Key-Here'); | ||
# $owm->getRawData('Berlin', $units, $lang, 'Your-Api-Key-Here', 'json'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
/** | ||
* OpenWeatherMap-PHP-API — An php api to parse weather data from http://www.OpenWeatherMap.org . | ||
* | ||
* @license MIT | ||
* | ||
* Please see the LICENSE file distributed with this source code for further | ||
* information regarding copyright and licensing. | ||
* | ||
* Please visit the following links to read about the usage policies and the license of OpenWeatherMap before using this class. | ||
* @see http://www.OpenWeatherMap.org | ||
* @see http://www.OpenWeatherMap.org/about | ||
* @see http://www.OpenWeatherMap.org/copyright | ||
* @see http://openweathermap.org/appid | ||
*/ | ||
|
||
use cmfcmf\OpenWeatherMap; | ||
use cmfcmf\OpenWeatherMap\Exception as OWMException; | ||
|
||
require('cmfcmf/OpenWeatherMap.php'); | ||
|
||
// Language of data (try your own language here!): | ||
$lang = 'de'; | ||
|
||
// Units (can be 'metric' or 'imperial' [default]): | ||
$units = 'metric'; | ||
|
||
// Get OpenWeatherMap object. Don't use caching (take a look into Example_Cache.php to see how it works). | ||
$owm = new OpenWeatherMap(); | ||
|
||
// Example 1: Get forecast for the next 10 days for Berlin. | ||
$forecast = $owm->getWeatherForecast('Berlin', $units, $lang, '', 10); | ||
echo "EXAMPLE 1<hr />\n\n\n"; | ||
|
||
echo "City: " . $forecast->city->name; | ||
echo "<br />\n"; | ||
echo "LastUpdate: " . $forecast->lastUpdate->format('d.m.Y H:i'); | ||
echo "<br />\n"; | ||
echo "<br />\n"; | ||
|
||
foreach($forecast as $weather) { | ||
// Each $weather contains a cmfcmf\ForecastWeather object which is almost the same as the cmfcmf\Weather object. | ||
// Take a look into 'Examples_Current.php' to see the available options. | ||
echo "Weather forecast at " . $weather->time->day->format('d.m.Y') . " from " . $weather->time->from->format('H:i') . " to " . $weather->time->to->format('H:i'); | ||
echo "<br />\n"; | ||
echo $weather->temperature; | ||
echo "<br />\n"; | ||
echo "---"; | ||
echo "<br />\n"; | ||
} | ||
|
||
// Example 1: Get forecast for the next 3 days for Berlin. | ||
$forecast = $owm->getWeatherForecast('Berlin', $units, $lang, '', 3); | ||
echo "EXAMPLE 2<hr />\n\n\n"; | ||
|
||
foreach($forecast as $weather) { | ||
echo "Weather forecast at " . $weather->time->day->format('d.m.Y') . " from " . $weather->time->from->format('H:i') . " to " . $weather->time->to->format('H:i') . "<br />"; | ||
echo $weather->temperature . "<br />"; | ||
echo "---<br />"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.