WeatherLib.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * alternatives:
  4. * http://simplepie.org/wiki/addons/yahoo_weather / http://cam.pl24.de/homepage-wetter.php
  5. * http://www.phpclasses.org/browse/file/11524.html
  6. * example: http://weather.yahooapis.com/forecastrss?p=GMXX0154
  7. * http://www.webmashup.com/API/Weather/Yahoo-Weather-API-l1871.html
  8. * http://developer.yahoo.com/weather/
  9. * http://www.webmashup.com/API/Weather/AccuWeather-API-l1862.html
  10. * http://www2.voegeli.li/no_cache/code-tutorials/php-scripts/class-weather-v2.html?L=1
  11. */
  12. App::uses('Xml', 'Utility');
  13. App::uses('HttpSocket', 'Network/Http');
  14. /**
  15. * WeatherLib to retreive the current weather + forecast
  16. *
  17. * You can use Configure::write('Weather', ...) to adjust settings for it globabally via configs:
  18. * - key (recommended)
  19. * - free (true/false)
  20. * - format (json, csv, xml)
  21. * - num_of_days (defaults to 5)
  22. *
  23. * @author Mark Scherer
  24. * @license http://opensource.org/licenses/mit-license.php MIT
  25. * @see http://developer.worldweatheronline.com/documentation
  26. */
  27. class WeatherLib {
  28. const API_URL = 'http://api.worldweatheronline.com/premium/v1/';
  29. const API_URL_FREE = 'http://api.worldweatheronline.com/free/v1/';
  30. public $settings = [
  31. 'format' => 'xml',
  32. 'num_of_days' => 5,
  33. 'q' => '', # e.g. 48.00,11.00
  34. 'key' => '',
  35. 'free' => true,
  36. ];
  37. public function __construct() {
  38. $this->settings = (array)Configure::read('Weather') + $this->settings;
  39. }
  40. /**
  41. * Get weather data - cached.
  42. * Provide cache=>false to prevent/clear the cache
  43. *
  44. * @return array Data or false on failure
  45. */
  46. public function get($q, array $options = []) {
  47. $options += $this->settings;
  48. $options['q'] = urlencode($q);
  49. $data = $this->_get('weather.ashx', $options);
  50. if (empty($data) || empty($data['data'])) {
  51. return false;
  52. }
  53. return $data['data'];
  54. }
  55. /**
  56. * Get weather conditions - cached.
  57. * Provide cache=>false to prevent/clear the cache
  58. *
  59. * @return array
  60. */
  61. public function conditions() {
  62. $options = [];
  63. $options['format'] = $this->settings['format'];
  64. if ($options['format'] === 'json') {
  65. $options['format'] = 'xml';
  66. }
  67. $conditions = $this->_get('wwoConditionCodes.xml', $options);
  68. if (empty($conditions) || empty($conditions['codes']['condition'])) {
  69. return [];
  70. }
  71. return $conditions['codes']['condition'];
  72. }
  73. //.../feed/weather.ashx?q=Neufahrn&format=json&num_of_days=2&key=598dfbdaeb121715111208
  74. /**
  75. * WeatherLib::_get()
  76. *
  77. * @param mixed $url
  78. * @param mixed $options
  79. * @return array|false
  80. */
  81. protected function _get($url, $options) {
  82. if (isset($options['cache'])) {
  83. $cache = $options['cache'];
  84. unset($options['cache']);
  85. }
  86. $url = $this->_url($url, $options);
  87. if (!empty($cache)) {
  88. if (!Cache::isInitialized('data')) {
  89. Cache::set('duration', $cache, 'data');
  90. }
  91. if ($cacheContent = Cache::read(md5($url), 'data')) {
  92. return $cacheContent;
  93. }
  94. }
  95. $Socket = new HttpSocket(['timeout' => 5]);
  96. $file = $Socket->get($url);
  97. $content = $file->body;
  98. if (empty($content)) {
  99. return false;
  100. }
  101. switch ($options['format']) {
  102. case 'json':
  103. $res = json_decode($content, true);
  104. break;
  105. case 'xml':
  106. $res = Xml::build($content);
  107. $res = Xml::toArray($res);
  108. break;
  109. case 'csv':
  110. default:
  111. //TODO?
  112. $res = [];
  113. throw new CakeException('Not implemented yet');
  114. }
  115. if (!empty($cache)) {
  116. Cache::write(md5($url), $res, 'data');
  117. }
  118. if (empty($res)) {
  119. return false;
  120. }
  121. return $res;
  122. }
  123. /**
  124. * @return string Url
  125. */
  126. protected function _url($url, $options = []) {
  127. $params = [];
  128. foreach ($options as $key => $option) {
  129. $params[] = $key . '=' . $option;
  130. }
  131. $params = (!empty($params) ? '?' : '') . implode('&', $params);
  132. $domain = $this->settings['free'] ? static::API_URL_FREE : static::API_URL;
  133. return $domain . $url . $params;
  134. }
  135. }