Weather.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Alexander Wirtz <alex@pc4p.net> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Weather.php,v 1.22 2004/01/06 19:36:28 eru Exp $
  20. // {{{ constants
  21. // {{{ cache times
  22. define("SERVICES_WEATHER_EXPIRES_UNITS", 900);
  23. define("SERVICES_WEATHER_EXPIRES_LOCATION", 900);
  24. define("SERVICES_WEATHER_EXPIRES_WEATHER", 1800);
  25. define("SERVICES_WEATHER_EXPIRES_FORECAST", 7200);
  26. define("SERVICES_WEATHER_EXPIRES_LINKS", 43200);
  27. // }}}
  28. // {{{ error codes
  29. define("SERVICES_WEATHER_ERROR_SERVICE_NOT_FOUND", 10);
  30. define("SERVICES_WEATHER_ERROR_UNKNOWN_LOCATION", 11);
  31. define("SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA", 12);
  32. define("SERVICES_WEATHER_ERROR_CACHE_INIT_FAILED", 13);
  33. define("SERVICES_WEATHER_ERROR_DB_NOT_CONNECTED", 14);
  34. // }}}
  35. // {{{ error codes defined by weather.com
  36. define("SERVICES_WEATHER_ERROR_UNKNOWN_ERROR", 0);
  37. define("SERVICES_WEATHER_ERROR_NO_LOCATION", 1);
  38. define("SERVICES_WEATHER_ERROR_INVALID_LOCATION", 2);
  39. define("SERVICES_WEATHER_ERROR_INVALID_PARTNER_ID", 100);
  40. define("SERVICES_WEATHER_ERROR_INVALID_PRODUCT_CODE", 101);
  41. define("SERVICES_WEATHER_ERROR_INVALID_LICENSE_KEY", 102);
  42. // }}}
  43. // }}}
  44. // {{{ class Services_Weather
  45. /**
  46. * PEAR::Services_Weather
  47. *
  48. * This class acts as an interface to various online weather-services.
  49. *
  50. * Services_Weather searches for given locations and retrieves current weather data
  51. * and, dependant on the used service, also forecasts. Up to now, SOAP services from
  52. * CapeScience and EJSE, XML from weather.com and METAR from noaa.gov are supported,
  53. * further services will get included, if they become available and are
  54. * properly documented.
  55. *
  56. * @author Alexander Wirtz <alex@pc4p.net>
  57. * @package Services_Weather
  58. * @license http://www.php.net/license/2_02.txt
  59. * @version 1.2
  60. */
  61. class Services_Weather {
  62. // {{{ &service()
  63. /**
  64. * Factory for creating the services-objects
  65. *
  66. * Usable keys for the options array are:
  67. * o debug enables debugging output
  68. * --- Common Options
  69. * o cacheType defines what type of cache to use
  70. * o cacheOptions passes cache options
  71. * o unitsFormat use (US)-standard, metric or custom units
  72. * o customUnitsFormat defines the customized units format
  73. * o dateFormat string to use for date output
  74. * o timeFormat string to use for time output
  75. * --- EJSE Options
  76. * none
  77. * --- GlobalWeather Options
  78. * none
  79. * --- METAR Options
  80. * o dsn String for defining the DB connection
  81. * o dbOptions passes DB options
  82. * o source http, ftp or file - type of data-source
  83. * o sourcePath where to look for the source, URI or filepath
  84. * --- weather.com Options
  85. * o partnerID You'll receive these keys after registering
  86. * o licenseKey with the weather.com XML-service
  87. *
  88. * @param string $service
  89. * @param array $options
  90. * @return PEAR_Error|object
  91. * @throws PEAR_Error
  92. * @throws PEAR_Error::SERVICES_WEATHER_ERROR_SERVICE_NOT_FOUND
  93. * @access public
  94. */
  95. function &service($service, $options = null)
  96. {
  97. $service = ucfirst(strtolower($service));
  98. $classname = "Services_Weather_".$service;
  99. // Check for debugging-mode and set stuff accordingly
  100. if (is_array($options) && isset($options["debug"]) && $options["debug"] >= 2) {
  101. if (!defined("SERVICES_WEATHER_DEBUG")) {
  102. define("SERVICES_WEATHER_DEBUG", true);
  103. }
  104. include_once("Services/Weather/".$service.".php");
  105. } else {
  106. if (!defined("SERVICES_WEATHER_DEBUG")) {
  107. define("SERVICES_WEATHER_DEBUG", false);
  108. }
  109. @include_once("Services/Weather/".$service.".php");
  110. }
  111. // No such service... bail out
  112. if (!class_exists($classname)) {
  113. return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_SERVICE_NOT_FOUND);
  114. }
  115. // Create service and return
  116. $error = null;
  117. @$obj = &new $classname($options, $error);
  118. if (Services_Weather::isError($error)) {
  119. return $error;
  120. } else {
  121. return $obj;
  122. }
  123. }
  124. // }}}
  125. // {{{ apiVersion()
  126. /**
  127. * For your convenience, when I come up with changes in the API...
  128. *
  129. * @return string
  130. * @access public
  131. */
  132. function apiVersion()
  133. {
  134. return "1.2";
  135. }
  136. // }}}
  137. // {{{ _errorMessage()
  138. /**
  139. * Returns the message for a certain error code
  140. *
  141. * @param PEAR_Error|int $value
  142. * @return string
  143. * @access private
  144. */
  145. function _errorMessage($value)
  146. {
  147. static $errorMessages;
  148. if (!isset($errorMessages)) {
  149. $errorMessages = array(
  150. SERVICES_WEATHER_ERROR_SERVICE_NOT_FOUND => "Requested service could not be found.",
  151. SERVICES_WEATHER_ERROR_UNKNOWN_LOCATION => "Unknown location provided.",
  152. SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA => "Server data wrong or not available.",
  153. SERVICES_WEATHER_ERROR_CACHE_INIT_FAILED => "Cache init was not completed.",
  154. SERVICES_WEATHER_ERROR_DB_NOT_CONNECTED => "MetarDB is not connected.",
  155. SERVICES_WEATHER_ERROR_UNKNOWN_ERROR => "An unknown error has occured.",
  156. SERVICES_WEATHER_ERROR_NO_LOCATION => "No location provided.",
  157. SERVICES_WEATHER_ERROR_INVALID_LOCATION => "Invalid location provided.",
  158. SERVICES_WEATHER_ERROR_INVALID_PARTNER_ID => "Invalid partner id.",
  159. SERVICES_WEATHER_ERROR_INVALID_PRODUCT_CODE => "Invalid product code.",
  160. SERVICES_WEATHER_ERROR_INVALID_LICENSE_KEY => "Invalid license key."
  161. );
  162. }
  163. if (Services_Weather::isError($value)) {
  164. $value = $value->getCode();
  165. }
  166. return isset($errorMessages[$value]) ? $errorMessages[$value] : $errorMessages[SERVICES_WEATHER_ERROR_UNKNOWN_ERROR];
  167. }
  168. // }}}
  169. // {{{ isError()
  170. /**
  171. * Checks for an error object, same as in PEAR
  172. *
  173. * @param PEAR_Error|mixed $value
  174. * @return bool
  175. * @access public
  176. */
  177. function isError($value)
  178. {
  179. return (is_object($value) && (get_class($value) == "pear_error" || is_subclass_of($value, "pear_error")));
  180. }
  181. // }}}
  182. // {{{ &raiseError()
  183. /**
  184. * Creates error, same as in PEAR with a customized flavor
  185. *
  186. * @param int $code
  187. * @return PEAR_Error
  188. * @access private
  189. */
  190. function &raiseError($code = SERVICES_WEATHER_ERROR_UNKNOWN_ERROR)
  191. {
  192. // This should improve the performance of the script, as PEAR is only included, when
  193. // really needed.
  194. include_once "PEAR.php";
  195. $message = "Services_Weather: ".Services_Weather::_errorMessage($code);
  196. return PEAR::raiseError($message, $code, PEAR_ERROR_RETURN, E_USER_NOTICE, "Services_Weather_Error", null, false);
  197. }
  198. // }}}
  199. }
  200. // }}}
  201. ?>