WeatherLib.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 MIT
  25. * @see http://developer.worldweatheronline.com/documentation
  26. * 2013-09-11 ms
  27. */
  28. class WeatherLib {
  29. const API_URL = 'http://api.worldweatheronline.com/premium/v1/';
  30. const API_URL_FREE = 'http://api.worldweatheronline.com/free/v1/';
  31. public $settings = array(
  32. 'format' => 'xml',
  33. 'num_of_days' => 5,
  34. 'q' => '', # e.g. 48.00,11.00
  35. 'key' => '',
  36. 'free' => true,
  37. );
  38. public function __construct() {
  39. $this->settings = array_merge($this->settings, (array)Configure::read('Weather'));
  40. }
  41. /**
  42. * Get weather data - cached.
  43. * Provide cache=>false to prevent/clear the cache
  44. *
  45. * @return array Data or false on failure
  46. */
  47. public function get($q, $options = array()) {
  48. $options = array_merge($this->settings, $options);
  49. $options['q'] = urlencode($q);
  50. $data = $this->_get('weather.ashx', $options);
  51. if (empty($data) || empty($data['data'])) {
  52. return false;
  53. }
  54. return $data['data'];
  55. }
  56. /**
  57. * Get weather conditions - cached.
  58. * Provide cache=>false to prevent/clear the cache
  59. *
  60. * @return array
  61. */
  62. public function conditions() {
  63. $options = array();
  64. $options['format'] = $this->settings['format'];
  65. if ($options['format'] === 'json') {
  66. $options['format'] = 'xml';
  67. }
  68. $conditions = $this->_get('wwoConditionCodes.xml', $options);
  69. if (empty($conditions) || empty($conditions['codes']['condition'])) {
  70. return array();
  71. }
  72. return $conditions['codes']['condition'];
  73. }
  74. //.../feed/weather.ashx?q=Neufahrn&format=json&num_of_days=2&key=598dfbdaeb121715111208
  75. public function _get($url, $options) {
  76. if (isset($options['cache'])) {
  77. $cache = $options['cache'];
  78. unset($options['cache']);
  79. }
  80. $url = $this->_url($url, $options);
  81. if (!empty($cache)) {
  82. if (!Cache::isInitialized('data')) {
  83. Cache::set('duration', $cache, 'data');
  84. }
  85. if ($cacheContent = Cache::read(md5($url), 'data')) {
  86. return $cacheContent;
  87. }
  88. }
  89. $Socket = new HttpSocket(array('timeout' => 5));
  90. $file = $Socket->get($url);
  91. $content = $file->body;
  92. if (empty($content)) {
  93. return false;
  94. }
  95. switch ($options['format']) {
  96. case 'json':
  97. $res = json_decode($content);
  98. break;
  99. case 'xml':
  100. $res = Xml::build($content);
  101. $res = Xml::toArray($res);
  102. break;
  103. case 'csv':
  104. //TODO?
  105. $res = array();
  106. throw new CakeException('Not implemented yet');
  107. }
  108. if (!empty($cache)) {
  109. Cache::write(md5($url), $res, 'data');
  110. }
  111. if (empty($res)) {
  112. return false;
  113. }
  114. return $res;
  115. }
  116. /**
  117. * @return string Url
  118. */
  119. public function _url($url, $options = array()) {
  120. $params = array();
  121. foreach ($options as $key => $option) {
  122. $params[] = $key . '=' . $option;
  123. }
  124. $params = (!empty($params) ? '?' : '') . implode('&', $params);
  125. $domain = $this->settings['free'] ? self::API_URL_FREE : self::API_URL;
  126. return $domain . $url . $params;
  127. }
  128. }