Services_Weather
[ class tree: Services_Weather ] [ index: Services_Weather ] [ all elements ]

Source for file Weather.php

Documentation is available at Weather.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
  3.  
  4. /**
  5.  * PEAR::Services_Weather
  6.  *
  7.  * Services_Weather searches for given locations and retrieves current
  8.  * weather data and, dependant on the used service, also forecasts. Up to
  9.  * now, SOAP services from CapeScience and EJSE, XML from weather.com and
  10.  * METAR/TAF from noaa.gov are supported, further services will get
  11.  * included, if they become available and are properly documented.
  12.  *
  13.  * PHP versions 4 and 5
  14.  *
  15.  * <LICENSE>
  16.  * Copyright (c) 2005, Alexander Wirtz
  17.  * All rights reserved.
  18.  *
  19.  * Redistribution and use in source and binary forms, with or without
  20.  * modification, are permitted provided that the following conditions
  21.  * are met:
  22.  * o Redistributions of source code must retain the above copyright notice,
  23.  *   this list of conditions and the following disclaimer.
  24.  * o Redistributions in binary form must reproduce the above copyright notice,
  25.  *   this list of conditions and the following disclaimer in the documentation
  26.  *   and/or other materials provided with the distribution.
  27.  * o Neither the name of the software nor the names of its contributors
  28.  *   may be used to endorse or promote products derived from this software
  29.  *   without specific prior written permission.
  30.  *
  31.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  32.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  35.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  36.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  37.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  38.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  39.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  41.  * POSSIBILITY OF SUCH DAMAGE.
  42.  * </LICENSE>
  43.  *
  44.  * @category    Web Services
  45.  * @package     Services_Weather
  46.  * @author      Alexander Wirtz <alex@pc4p.net>
  47.  * @copyright   2005 Alexander Wirtz
  48.  * @license     http://www.opensource.org/licenses/bsd-license.php  BSD License
  49.  * @version     CVS: $Id: Weather.php,v 1.43 2005/11/16 12:48:34 eru Exp $
  50.  * @link        http://pear.php.net/package/Services_Weather
  51.  * @filesource
  52.  */
  53.  
  54. // {{{ constants
  55. // {{{ cache times
  56. define("SERVICES_WEATHER_EXPIRES_UNITS",        900)// 15M
  57. define("SERVICES_WEATHER_EXPIRES_LOCATION",   43200)// 12H
  58. define("SERVICES_WEATHER_EXPIRES_WEATHER",     1800)// 30M
  59. define("SERVICES_WEATHER_EXPIRES_FORECAST",    7200)//  2H
  60. define("SERVICES_WEATHER_EXPIRES_LINKS",      43200)// 12H
  61. define("SERVICES_WEATHER_EXPIRES_SEARCH",   2419200)// 28D
  62. // }}}
  63.  
  64. // {{{ error codes
  65. define("SERVICES_WEATHER_ERROR_SERVICE_NOT_FOUND",        10);
  66. define("SERVICES_WEATHER_ERROR_UNKNOWN_LOCATION",         11);
  67. define("SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA",        12);
  68. define("SERVICES_WEATHER_ERROR_CACHE_INIT_FAILED",        13);
  69. define("SERVICES_WEATHER_ERROR_DB_NOT_CONNECTED",         14);
  70. define("SERVICES_WEATHER_ERROR_HTTP_PROXY_INVALID",       15);
  71. define("SERVICES_WEATHER_ERROR_SUNFUNCS_DATE_INVALID",    16);
  72. define("SERVICES_WEATHER_ERROR_SUNFUNCS_RETFORM_INVALID"17);
  73. define("SERVICES_WEATHER_ERROR_METAR_SOURCE_INVALID",     18);
  74. // }}}
  75.  
  76. // {{{ error codes defined by weather.com
  77. define("SERVICES_WEATHER_ERROR_UNKNOWN_ERROR",            0);
  78. define("SERVICES_WEATHER_ERROR_NO_LOCATION",              1);
  79. define("SERVICES_WEATHER_ERROR_INVALID_LOCATION",         2);
  80. define("SERVICES_WEATHER_ERROR_INVALID_PARTNER_ID",     100);
  81. define("SERVICES_WEATHER_ERROR_INVALID_PRODUCT_CODE",   101);
  82. define("SERVICES_WEATHER_ERROR_INVALID_LICENSE_KEY",    102);
  83. // }}}
  84. // }}}
  85.  
  86. // {{{ class Services_Weather
  87. /**
  88.  * This class acts as an interface to various online weather-services.
  89.  *
  90.  * @category    Web Services
  91.  * @package     Services_Weather
  92.  * @author      Alexander Wirtz <alex@pc4p.net>
  93.  * @copyright   2005 Alexander Wirtz
  94.  * @license     http://www.opensource.org/licenses/bsd-license.php  BSD License
  95.  * @version     Release: 1.4.0
  96.  * @link        http://pear.php.net/package/Services_Weather
  97.  */
  98.  
  99.     // {{{ &service()
  100.     /**
  101.      * Factory for creating the services-objects
  102.      *
  103.      * Usable keys for the options array are:
  104.      * o debug              enables debugging output
  105.      * --- Common Options
  106.      * o cacheType          defines what type of cache to use
  107.      * o cacheOptions       passes cache options
  108.      * o unitsFormat        use (US)-standard, metric or custom units
  109.      * o customUnitsFormat  defines the customized units format
  110.      * o httpTimeout        sets timeout for HTTP requests
  111.      * o httpProxy          sets proxy for HTTP requests, please use the
  112.      *                      notation http://[user[:pass]@]host[:port]
  113.      * o dateFormat         string to use for date output
  114.      * o timeFormat         string to use for time output
  115.      * --- EJSE Options
  116.      * o none
  117.      * --- GlobalWeather Options
  118.      * o none
  119.      * --- METAR/TAF Options
  120.      * o dsn                String for defining the DB connection
  121.      * o dbOptions          passes DB options
  122.      * o sourceMetar        http, ftp or file - type of data-source for METAR
  123.      * o sourcePathMetar    where to look for the source, URI or filepath,
  124.      *                      of METAR information
  125.      * o sourceTaf          http, ftp or file - type of data-source for TAF
  126.      * o sourcePathTaf      where to look for the source, URI or filepath,
  127.      *                      of TAF information
  128.      * --- weather.com Options
  129.      * o partnerID          You'll receive these keys after registering
  130.      * o licenseKey         with the weather.com XML-service
  131.      * o preFetch           Enables pre-fetching of data in one single request
  132.      *
  133.      * @param    string                     $service 
  134.      * @param    array                      $options 
  135.      * @return   PEAR_Error|object 
  136.      * @throws   PEAR_Error
  137.      * @throws   PEAR_Error::SERVICES_WEATHER_ERROR_SERVICE_NOT_FOUND
  138.      * @access   public
  139.      */
  140.     function &service($service$options = null)
  141.     {
  142.         $service ucfirst(strtolower($service));
  143.         $classname "Services_Weather_".$service;
  144.  
  145.         // Check for debugging-mode and set stuff accordingly
  146.         if (is_array($options&& isset($options["debug"]&& $options["debug">= 2{
  147.             if (!defined("SERVICES_WEATHER_DEBUG")) {
  148.                 define("SERVICES_WEATHER_DEBUG"true);
  149.             }
  150.             include_once("Services/Weather/".$service.".php");
  151.         else {
  152.             if (!defined("SERVICES_WEATHER_DEBUG")) {
  153.                 define("SERVICES_WEATHER_DEBUG"false);
  154.             }
  155.             @include_once("Services/Weather/".$service.".php");
  156.         }
  157.  
  158.         // No such service... bail out
  159.         if (!class_exists($classname)) {
  160.             return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_SERVICE_NOT_FOUND__FILE____LINE__);
  161.         }
  162.  
  163.         // Create service and return
  164.         $error = null;
  165.         @$obj &new $classname($options$error);
  166.  
  167.         if (Services_Weather::isError($error)) {
  168.             return $error;
  169.         else {
  170.             return $obj;
  171.         }
  172.     }
  173.     // }}}
  174.  
  175.     // {{{ apiVersion()
  176.     /**
  177.      * For your convenience, when I come up with changes in the API...
  178.      *
  179.      * @return  string 
  180.      * @access  public
  181.      */
  182.     function apiVersion()
  183.     {
  184.         return "1.4";
  185.     }
  186.     // }}}
  187.  
  188.     // {{{ _errorMessage()
  189.     /**
  190.      * Returns the message for a certain error code
  191.      *
  192.      * @param   PEAR_Error|int             $value 
  193.      * @return  string 
  194.      * @access  private
  195.      */
  196.     function _errorMessage($value)
  197.     {
  198.         static $errorMessages;
  199.         if (!isset($errorMessages)) {
  200.             $errorMessages = array(
  201.                 SERVICES_WEATHER_ERROR_SERVICE_NOT_FOUND         => "Requested service could not be found.",
  202.                 SERVICES_WEATHER_ERROR_UNKNOWN_LOCATION          => "Unknown location provided.",
  203.                 SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA         => "Server data wrong or not available.",
  204.                 SERVICES_WEATHER_ERROR_CACHE_INIT_FAILED         => "Cache init was not completed.",
  205.                 SERVICES_WEATHER_ERROR_DB_NOT_CONNECTED          => "MetarDB is not connected.",
  206.                 SERVICES_WEATHER_ERROR_HTTP_PROXY_INVALID        => "The given proxy is not valid, please use the notation http://[user[:pass]@]host[:port].",
  207.                 SERVICES_WEATHER_ERROR_SUNFUNCS_DATE_INVALID     => "The date you've provided for calculation of sunrise/sunset is not a timestamp.",
  208.                 SERVICES_WEATHER_ERROR_SUNFUNCS_RETFORM_INVALID  => "The return format you've provided for calculation of sunrise/sunset is not valid.",
  209.                 SERVICES_WEATHER_ERROR_METAR_SOURCE_INVALID      => "The METAR/TAF source you've provided has an invalid type or path.",
  210.                 SERVICES_WEATHER_ERROR_UNKNOWN_ERROR             => "An unknown error has occured.",
  211.                 SERVICES_WEATHER_ERROR_NO_LOCATION               => "No location provided.",
  212.                 SERVICES_WEATHER_ERROR_INVALID_LOCATION          => "Invalid location provided.",
  213.                 SERVICES_WEATHER_ERROR_INVALID_PARTNER_ID        => "Invalid partner id.",
  214.                 SERVICES_WEATHER_ERROR_INVALID_PRODUCT_CODE      => "Invalid product code.",
  215.                 SERVICES_WEATHER_ERROR_INVALID_LICENSE_KEY       => "Invalid license key."
  216.             );
  217.         }
  218.  
  219.         if (Services_Weather::isError($value)) {
  220.             $value $value->getCode();
  221.         }
  222.  
  223.         return isset($errorMessages[$value]$errorMessages[$value$errorMessages[SERVICES_WEATHER_ERROR_UNKNOWN_ERROR];
  224.     }
  225.     // }}}
  226.  
  227.     // {{{ isError()
  228.     /**
  229.      * Checks for an error object, same as in PEAR
  230.      *
  231.      * @param   PEAR_Error|mixed           $value 
  232.      * @return  bool 
  233.      * @access  public
  234.      */
  235.     function isError($value)
  236.     {
  237.         return (is_object($value&& (strtolower(get_class($value)) == "pear_error" || is_subclass_of($value"pear_error")));
  238.     }
  239.     // }}}
  240.  
  241.     // {{{ &raiseError()
  242.     /**
  243.      * Creates error, same as in PEAR with a customized flavor
  244.      *
  245.      * @param   int                         $code 
  246.      * @param   string                      $file 
  247.      * @param   int                         $line 
  248.      * @return  PEAR_Error 
  249.      * @access  private
  250.      */
  251.     function &raiseError($code = SERVICES_WEATHER_ERROR_UNKNOWN_ERROR$file ""$line = 0)
  252.     {
  253.         // This should improve the performance of the script, as PEAR is only included, when
  254.         // really needed.
  255.         include_once "PEAR.php";
  256.  
  257.         $message "Services_Weather";
  258.         if ($file != "" && $line > 0{
  259.             $message .= " (".basename($file).":".$line.")";
  260.         }
  261.         $message .= ": ".Services_Weather::_errorMessage($code);
  262.  
  263.         $error = PEAR::raiseError($message$codePEAR_ERROR_RETURNE_USER_NOTICE"Services_Weather_Error"nullfalse);
  264.         return $error;
  265.     }
  266.     // }}}
  267. }
  268. // }}}
  269. ?>

Documentation generated on Mon, 11 Mar 2019 14:37:52 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.