CurrentWeather.php 6.8 KB

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