WeatherLib.php 3.5 KB

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