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-2011, 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-2011 Alexander Wirtz
  48.  * @license     http://www.opensource.org/licenses/bsd-license.php  BSD License
  49.  * @version     CVS: $Id$
  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. define("SERVICES_WEATHER_ERROR_MOONFUNCS_DATE_INVALID",   19);
  75. // }}}
  76.  
  77. // {{{ error codes defined by weather.com
  78. define("SERVICES_WEATHER_ERROR_UNKNOWN_ERROR",            0);
  79. define("SERVICES_WEATHER_ERROR_NO_LOCATION",              1);
  80. define("SERVICES_WEATHER_ERROR_INVALID_LOCATION",         2);
  81. define("SERVICES_WEATHER_ERROR_INVALID_PARTNER_ID",     100);
  82. define("SERVICES_WEATHER_ERROR_INVALID_PRODUCT_CODE",   101);
  83. define("SERVICES_WEATHER_ERROR_INVALID_LICENSE_KEY",    102);
  84. // }}}
  85. // }}}
  86.  
  87. // {{{ class Services_Weather
  88. /**
  89.  * This class acts as an interface to various online weather-services.
  90.  *
  91.  * @category    Web Services
  92.  * @package     Services_Weather
  93.  * @author      Alexander Wirtz <alex@pc4p.net>
  94.  * @copyright   2005-2011 Alexander Wirtz
  95.  * @license     http://www.opensource.org/licenses/bsd-license.php  BSD License
  96.  * @version     Release: 1.4.7
  97.  * @link        http://pear.php.net/package/Services_Weather
  98.  */
  99.  
  100.     // {{{ &service()
  101.     /**
  102.      * Factory for creating the services-objects
  103.      *
  104.      * Usable keys for the options array are:
  105.      * o debug              enables debugging output
  106.      * --- Common Options
  107.      * o cacheType          defines what type of cache to use
  108.      * o cacheOptions       passes cache options
  109.      * o unitsFormat        use (US)-standard, metric or custom units
  110.      * o customUnitsFormat  defines the customized units format
  111.      * o httpTimeout        sets timeout for HTTP requests
  112.      * o httpProxy          sets proxy for HTTP requests, please use the
  113.      *                      notation http://[user[:pass]@]host[:port]
  114.      * o dateFormat         string to use for date output
  115.      * o timeFormat         string to use for time output
  116.      * --- EJSE Options
  117.      * o none
  118.      * --- GlobalWeather Options
  119.      * o none
  120.      * --- METAR/TAF Options
  121.      * o dsn                String for defining the DB connection
  122.      * o dbOptions          passes DB options
  123.      * o sourceMetar        http, ftp or file - type of data-source for METAR
  124.      * o sourcePathMetar    where to look for the source, URI or filepath,
  125.      *                      of METAR information
  126.      * o sourceTaf          http, ftp or file - type of data-source for TAF
  127.      * o sourcePathTaf      where to look for the source, URI or filepath,
  128.      *                      of TAF information
  129.      * --- weather.com Options
  130.      * o partnerID          You'll receive these keys after registering
  131.      * o licenseKey         with the weather.com XML-service
  132.      * o preFetch           Enables pre-fetching of data in one single request
  133.      *
  134.      * @param    string                     $service 
  135.      * @param    array                      $options 
  136.      * @return   PEAR_Error|object 
  137.      * @throws   PEAR_Error
  138.      * @throws   PEAR_Error::SERVICES_WEATHER_ERROR_SERVICE_NOT_FOUND
  139.      * @access   public
  140.      */
  141.     function &service($service$options = null)
  142.     {
  143.         $service ucfirst(strtolower($service));
  144.         $classname "Services_Weather_".$service;
  145.  
  146.         // Check for debugging-mode and set stuff accordingly
  147.         if (is_array($options&& isset($options["debug"]&& $options["debug">= 2{
  148.             if (!defined("SERVICES_WEATHER_DEBUG")) {
  149.                 define("SERVICES_WEATHER_DEBUG"true);
  150.             }
  151.             include_once("Services/Weather/".$service.".php");
  152.         else {
  153.             if (!defined("SERVICES_WEATHER_DEBUG")) {
  154.                 define("SERVICES_WEATHER_DEBUG"false);
  155.             }
  156.             @include_once("Services/Weather/".$service.".php");
  157.         }
  158.  
  159.         // No such service... bail out
  160.         if (!class_exists($classname)) {
  161.             return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_SERVICE_NOT_FOUND__FILE____LINE__);
  162.         }
  163.  
  164.         // Create service and return
  165.         $error = null;
  166.         @$obj &new $classname($options$error);
  167.  
  168.         if (Services_Weather::isError($error)) {
  169.             return $error;
  170.         else {
  171.             return $obj;
  172.         }
  173.     }
  174.     // }}}
  175.  
  176.     // {{{ apiVersion()
  177.     /**
  178.      * For your convenience, when I come up with changes in the API...
  179.      *
  180.      * @return  string 
  181.      * @access  public
  182.      */
  183.     function apiVersion()
  184.     {
  185.         return "1.4";
  186.     }
  187.     // }}}
  188.  
  189.     // {{{ _errorMessage()
  190.     /**
  191.      * Returns the message for a certain error code
  192.      *
  193.      * @param   PEAR_Error|int             $value 
  194.      * @return  string 
  195.      * @access  private
  196.      */
  197.     function _errorMessage($value)
  198.     {
  199.         static $errorMessages;
  200.         if (!isset($errorMessages)) {
  201.             $errorMessages = array(
  202.                 SERVICES_WEATHER_ERROR_SERVICE_NOT_FOUND         => "Requested service could not be found.",
  203.                 SERVICES_WEATHER_ERROR_UNKNOWN_LOCATION          => "Unknown location provided.",
  204.                 SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA         => "Server data wrong or not available.",
  205.                 SERVICES_WEATHER_ERROR_CACHE_INIT_FAILED         => "Cache init was not completed.",
  206.                 SERVICES_WEATHER_ERROR_DB_NOT_CONNECTED          => "MetarDB is not connected.",
  207.                 SERVICES_WEATHER_ERROR_HTTP_PROXY_INVALID        => "The given proxy is not valid, please use the notation http://[user[:pass]@]host[:port].",
  208.                 SERVICES_WEATHER_ERROR_SUNFUNCS_DATE_INVALID     => "The date you've provided for calculation of sunrise/sunset is not a timestamp.",
  209.                 SERVICES_WEATHER_ERROR_SUNFUNCS_RETFORM_INVALID  => "The return format you've provided for calculation of sunrise/sunset is not valid.",
  210.                 SERVICES_WEATHER_ERROR_METAR_SOURCE_INVALID      => "The METAR/TAF source you've provided has an invalid type or path.",
  211.                 SERVICES_WEATHER_ERROR_UNKNOWN_ERROR             => "An unknown error has occured.",
  212.                 SERVICES_WEATHER_ERROR_NO_LOCATION               => "No location provided.",
  213.                 SERVICES_WEATHER_ERROR_INVALID_LOCATION          => "Invalid location provided.",
  214.                 SERVICES_WEATHER_ERROR_INVALID_PARTNER_ID        => "Invalid partner id.",
  215.                 SERVICES_WEATHER_ERROR_INVALID_PRODUCT_CODE      => "Invalid product code.",
  216.                 SERVICES_WEATHER_ERROR_INVALID_LICENSE_KEY       => "Invalid license key."
  217.             );
  218.         }
  219.  
  220.         if (Services_Weather::isError($value)) {
  221.             $value $value->getCode();
  222.         }
  223.  
  224.         return isset($errorMessages[$value]$errorMessages[$value$errorMessages[SERVICES_WEATHER_ERROR_UNKNOWN_ERROR];
  225.     }
  226.     // }}}
  227.  
  228.     // {{{ isError()
  229.     /**
  230.      * Checks for an error object, same as in PEAR
  231.      *
  232.      * @param   PEAR_Error|mixed           $value 
  233.      * @return  bool 
  234.      * @access  public
  235.      */
  236.     function isError($value)
  237.     {
  238.         return (is_object($value&& (strtolower(get_class($value)) == "pear_error" || is_subclass_of($value"pear_error")));
  239.     }
  240.     // }}}
  241.  
  242.     // {{{ &raiseError()
  243.     /**
  244.      * Creates error, same as in PEAR with a customized flavor
  245.      *
  246.      * @param   int                         $code 
  247.      * @param   string                      $file 
  248.      * @param   int                         $line 
  249.      * @return  PEAR_Error 
  250.      * @access  private
  251.      */
  252.     function &raiseError($code = SERVICES_WEATHER_ERROR_UNKNOWN_ERROR$file ""$line = 0)
  253.     {
  254.         // This should improve the performance of the script, as PEAR is only included, when
  255.         // really needed.
  256.         include_once "PEAR.php";
  257.  
  258.         $message "Services_Weather";
  259.         if ($file != "" && $line > 0{
  260.             $message .= " (".basename($file).":".$line.")";
  261.         }
  262.         $message .= ": ".Services_Weather::_errorMessage($code);
  263.  
  264.         $error = PEAR::raiseError($message$codePEAR_ERROR_RETURNE_USER_NOTICE"Services_Weather_Error"nullfalse);
  265.         return $error;
  266.     }
  267.     // }}}
  268. }
  269. // }}}
  270. ?>

Documentation generated on Mon, 11 Mar 2019 15:50:59 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.