WeatherForecast.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. use Cmfcmf\OpenWeatherMap;
  18. require_once __DIR__ . '/bootstrap.php';
  19. // Language of data (try your own language here!):
  20. $lang = 'de';
  21. // Units (can be 'metric' or 'imperial' [default]):
  22. $units = 'metric';
  23. // Get OpenWeatherMap object. Don't use caching (take a look into Example_Cache.php to see how it works).
  24. $owm = new OpenWeatherMap($myApiKey);
  25. // Example 1: Get forecast for the next 5 days for Berlin.
  26. $forecast = $owm->getWeatherForecast('Berlin', $units, $lang, '', 5);
  27. echo "EXAMPLE 1<hr />\n\n\n";
  28. echo "City: " . $forecast->city->name;
  29. echo "<br />\n";
  30. echo "LastUpdate: " . $forecast->lastUpdate->format('d.m.Y H:i');
  31. echo "<br />\n";
  32. echo "Sunrise : " . $forecast->sun->rise->format("H:i:s (e)") . " Sunset : " . $forecast->sun->set->format("H:i:s (e)");
  33. echo "<br />\n";
  34. echo "<br />\n";
  35. foreach ($forecast as $weather) {
  36. // Each $weather contains a Cmfcmf\ForecastWeather object which is almost the same as the Cmfcmf\Weather object.
  37. // Take a look into 'Examples_Current.php' to see the available options.
  38. 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');
  39. echo "<br />\n";
  40. echo $weather->temperature;
  41. echo "<br />\n";
  42. echo "Sun rise: " . $weather->sun->rise->format('d.m.Y H:i (e)');
  43. echo "<br />\n";
  44. echo "---";
  45. echo "<br />\n";
  46. }
  47. // Example 2: Get forecast for the next 3 days for Berlin.
  48. $forecast = $owm->getWeatherForecast('Berlin', $units, $lang, '', 3);
  49. echo "EXAMPLE 2<hr />\n\n\n";
  50. foreach ($forecast as $weather) {
  51. 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 />";
  52. echo $weather->temperature . "<br />\n";
  53. echo "<br />\n";
  54. echo "Sun rise: " . $weather->sun->rise->format('d.m.Y H:i (e)');
  55. echo "<br />\n";
  56. echo "---<br />\n";
  57. }