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

Source for file Globalweather.php

Documentation is available at Globalweather.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
  3.  
  4. /**
  5.  * PEAR::Services_Weather_Globalweather
  6.  *
  7.  * PHP versions 4 and 5
  8.  *
  9.  * <LICENSE>
  10.  * Copyright (c) 2005-2007, Alexander Wirtz
  11.  * All rights reserved.
  12.  *
  13.  * Redistribution and use in source and binary forms, with or without
  14.  * modification, are permitted provided that the following conditions
  15.  * are met:
  16.  * o Redistributions of source code must retain the above copyright notice,
  17.  *   this list of conditions and the following disclaimer.
  18.  * o Redistributions in binary form must reproduce the above copyright notice,
  19.  *   this list of conditions and the following disclaimer in the documentation
  20.  *   and/or other materials provided with the distribution.
  21.  * o Neither the name of the software nor the names of its contributors
  22.  *   may be used to endorse or promote products derived from this software
  23.  *   without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35.  * POSSIBILITY OF SUCH DAMAGE.
  36.  * </LICENSE>
  37.  *
  38.  * @category    Web Services
  39.  * @package     Services_Weather
  40.  * @author      Alexander Wirtz <alex@pc4p.net>
  41.  * @copyright   2005-2007 Alexander Wirtz
  42.  * @license     http://www.opensource.org/licenses/bsd-license.php  BSD License
  43.  * @version     CVS: $Id: Globalweather.php,v 1.39 2007/04/16 19:35:31 eru Exp $
  44.  * @link        http://pear.php.net/package/Services_Weather
  45.  * @link        http://www.capescience.com/webservices/globalweather/index.shtml
  46.  * @example     examples/globalweather-basic.php    globalweather-basic.php
  47.  * @filesource
  48.  */
  49.  
  50. require_once "Services/Weather/Common.php";
  51.  
  52. // {{{ class Services_Weather_Globalweather
  53. /**
  54.  * This class acts as an interface to the soap service of capescience.com.
  55.  * It searches for given locations and retrieves current weather data.
  56.  *
  57.  * GlobalWeather is a SOAP frontend for METAR data, provided by CapeScience.
  58.  * If you want to use METAR, you should try this class first, as it is much
  59.  * more comfortable (and also a bit faster) than the native METAR-class
  60.  * provided by this package. On the other hand, this service won't supply
  61.  * TAFs, the forecast system accompanying METAR, so you have to make
  62.  * the call here...
  63.  *
  64.  * For a working example, please take a look at
  65.  *     docs/Services_Weather/examples/globalweather-basic.php
  66.  *
  67.  *
  68.  * @category    Web Services
  69.  * @package     Services_Weather
  70.  * @author      Alexander Wirtz <alex@pc4p.net>
  71.  * @copyright   2005-2007 Alexander Wirtz
  72.  * @license     http://www.opensource.org/licenses/bsd-license.php  BSD License
  73.  * @version     Release: 1.4.3
  74.  * @link        http://pear.php.net/package/Services_Weather
  75.  * @link        http://www.capescience.com/webservices/globalweather/index.shtml
  76.  * @example     examples/globalweather-basic.php    globalweather-basic.php
  77.  */
  78.  
  79.     // {{{ properties
  80.     /**
  81.      * WSDL object, provided by CapeScience
  82.      *
  83.      * @var     object                      $_wsdl 
  84.      * @access  private
  85.      */
  86.     var $_wsdl;
  87.  
  88.     /**
  89.      * SOAP object to access station data, provided by CapeScience
  90.      *
  91.      * @var     object                      $_stationSoap 
  92.      * @access  private
  93.      */
  94.     var $_stationSoap;
  95.  
  96.     /**
  97.      * SOAP object to access weather data, provided by CapeScience
  98.      *
  99.      * @var      object                      $_weatherSoap 
  100.      * @access   private
  101.      */
  102.     var $_weatherSoap;
  103.     // }}}
  104.  
  105.     // {{{ constructor
  106.     /**
  107.      * Constructor
  108.      *
  109.      * Requires SOAP to be installed
  110.      *
  111.      * @param   array                       $options 
  112.      * @param   mixed                       $error 
  113.      * @throws  PEAR_Error
  114.      * @throws  PEAR_Error::SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA
  115.      * @access  private
  116.      */
  117.     function Services_Weather_Globalweather($options&$error)
  118.     {
  119.         $perror = null;
  120.         $this->Services_Weather_Common($options$perror);
  121.         if (Services_Weather::isError($perror)) {
  122.             $error $perror;
  123.         }
  124.     }
  125.     // }}}
  126.  
  127.     // {{{ _connectServer()
  128.     /**
  129.      * Connects to the SOAP server and retrieves the WSDL data
  130.      *
  131.      * @return  PEAR_Error|bool
  132.      * @throws  PEAR_Error::SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA
  133.      * @access  private
  134.      */
  135.     function _connectServer()
  136.     {
  137.         include_once "SOAP/Client.php";
  138.         $this->_wsdl = new SOAP_WSDL("http://live.capeclear.com/ccx/GlobalWeather?wsdl"$this->_httpOptions);
  139.         if (isset($this->_wsdl->fault&& Services_Weather::isError($this->_wsdl->fault)) {
  140.             $error Services_Weather::raiseError(SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA__FILE____LINE__);
  141.             return $error;
  142.         }
  143.  
  144.         eval($this->_wsdl->generateAllProxies());
  145.         if (!class_exists("WebService_GlobalWeather_StationInfo"|| !class_exists("WebService_GlobalWeather_GlobalWeather")) {
  146.             $error Services_Weather::raiseError(SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA__FILE____LINE__);
  147.             return $error;
  148.         }
  149.  
  150.         $this->_stationSoap &new WebService_GlobalWeather_StationInfo;
  151.         $this->_weatherSoap &new WebService_GlobalWeather_GlobalWeather;
  152.  
  153.         return true;
  154.     }
  155.     // }}}
  156.  
  157.     // {{{ _checkLocationID()
  158.     /**
  159.      * Checks the id for valid values and thus prevents silly requests to
  160.      * GlobalWeather server
  161.      *
  162.      * @param   string                      $id 
  163.      * @return  PEAR_Error|bool
  164.      * @throws  PEAR_Error::SERVICES_WEATHER_ERROR_NO_LOCATION
  165.      * @throws  PEAR_Error::SERVICES_WEATHER_ERROR_INVALID_LOCATION
  166.      * @access  private
  167.      */
  168.     function _checkLocationID($id)
  169.     {
  170.         // Check, if the stationSoap-Object is present. If not, connect to the Server and retrieve the WDSL data
  171.         if (!$this->_stationSoap{
  172.             $status $this->_connectServer();
  173.             if (Services_Weather::isError($status)) {
  174.                 return $status;
  175.             }
  176.         }
  177.  
  178.         if (is_array($id|| is_object($id|| !strlen($id)) {
  179.             return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_NO_LOCATION__FILE____LINE__);
  180.         elseif ($this->_stationSoap->isValidCode($id=== false{
  181.             return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_INVALID_LOCATION__FILE____LINE__);
  182.         }
  183.  
  184.         return true;
  185.     }
  186.     // }}}
  187.  
  188.     // {{{ searchLocation()
  189.     /**
  190.      * Searches IDs for given location, returns array of possible locations
  191.      * or single ID
  192.      *
  193.      * @param   string                      $location 
  194.      * @param   bool                        $useFirst       If set, first ID of result-array is returned
  195.      * @return  PEAR_Error|array|string
  196.      * @throws  PEAR_Error::SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA
  197.      * @throws  PEAR_Error::SERVICES_WEATHER_ERROR_UNKNOWN_LOCATION
  198.      * @access  public
  199.      */
  200.     function searchLocation($location$useFirst = false)
  201.     {
  202.         // Check, if the stationSoap-Object is present. If not, connect to the Server and retrieve the WDSL data
  203.         if (!$this->_stationSoap{
  204.             $status $this->_connectServer();
  205.             if (Services_Weather::isError($status)) {
  206.                 return $status;
  207.             }
  208.         }
  209.  
  210.         // Get search data from server and unserialize
  211.         $search $this->_stationSoap->searchByName($location);
  212.  
  213.         if (Services_Weather::isError($search)) {
  214.             return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA__FILE____LINE__);
  215.         else {
  216.             if (!is_array($search|| !sizeof($search)) {
  217.                 return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_UNKNOWN_LOCATION__FILE____LINE__);
  218.             else {
  219.                 if (!$useFirst && (sizeof($search> 1)) {
  220.                     $searchReturn = array();
  221.                     for ($i = 0; $i sizeof($search)$i++{
  222.                         $searchReturn[$search[$i]->icao$search[$i]->name.", ".$search[$i]->country;
  223.                     }
  224.                 elseif ($useFirst || (sizeof($search== 1)) {
  225.                     $searchReturn $search[0]->icao;
  226.                 }
  227.             }
  228.         }
  229.  
  230.         return $searchReturn;
  231.     }
  232.     // }}}
  233.  
  234.     // {{{ searchLocationByCountry()
  235.     /**
  236.      * Returns IDs with location-name for a given country or all available
  237.      * countries, if no value was given
  238.      *
  239.      * @param   string                      $country 
  240.      * @return  PEAR_Error|array
  241.      * @throws  PEAR_Error::SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA
  242.      * @throws  PEAR_Error::SERVICES_WEATHER_ERROR_UNKNOWN_LOCATION
  243.      * @access  public
  244.      */
  245.     function searchLocationByCountry($country "")
  246.     {
  247.         // Check, if the stationSoap-Object is present. If not, connect to the Server and retrieve the WDSL data
  248.         if (!$this->_stationSoap{
  249.             $status $this->_connectServer();
  250.             if (Services_Weather::isError($status)) {
  251.                 return $status;
  252.             }
  253.         }
  254.  
  255.         // Return the available countries as no country was given
  256.         if (!strlen($country)) {
  257.             $countries $this->_stationSoap->listCountries("");
  258.             if (Services_Weather::isError($countries)) {
  259.                 return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA__FILE____LINE__);
  260.             }
  261.             return $countries;
  262.         }
  263.  
  264.         // Now for the real search
  265.         $countryLocs $this->_stationSoap->searchByCountry($country);
  266.         // Check result for validity
  267.         if (Services_Weather::isError($countryLocs)) {
  268.             return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA__FILE____LINE__);
  269.         elseif (!is_array($countryLocs)) {
  270.             return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_UNKNOWN_LOCATION__FILE____LINE__);
  271.         }
  272.  
  273.         // Construct the result
  274.         $locations = array();
  275.         foreach ($countryLocs as $location{
  276.             $locations[$location->icao$location->name.", ".$location->country;
  277.         }
  278.         asort($locations);
  279.  
  280.         return $locations;
  281.     }
  282.     // }}}
  283.  
  284.     // {{{ getLocation()
  285.     /**
  286.      * Returns the data for the location belonging to the ID
  287.      *
  288.      * @param   string                      $id 
  289.      * @return  PEAR_Error|array
  290.      * @throws  PEAR_Error
  291.      * @access  public
  292.      */
  293.     function getLocation($id "")
  294.     {
  295.         $status $this->_checkLocationID($id);
  296.  
  297.         if (Services_Weather::isError($status)) {
  298.             return $status;
  299.         }
  300.  
  301.         $locationReturn = array();
  302.  
  303.         if ($this->_cacheEnabled && ($location $this->_cache->get("GW-".$id"location"))) {
  304.             // Get data from cache
  305.             $this->_location $location;
  306.             $locationReturn["cache""HIT";
  307.         else {
  308.             // Check, if the stationSoap-Object is present. If not, connect to the Server and retrieve the WDSL data
  309.             if (!$this->_stationSoap{
  310.                 $status $this->_connectServer();
  311.                 if (Services_Weather::isError($status)) {
  312.                     return $status;
  313.                 }
  314.             }
  315.  
  316.             $location $this->_stationSoap->getStation($id);
  317.  
  318.             if (Services_Weather::isError($location)) {
  319.                 return $location;
  320.             }
  321.  
  322.             $this->_location $location;
  323.  
  324.             if ($this->_cacheEnabled{
  325.                 // ...and cache it
  326.                 $expire constant("SERVICES_WEATHER_EXPIRES_LOCATION");
  327.                 $this->_cache->extSave("GW-".$id$this->_location""$expire"location");
  328.             }
  329.             $locationReturn["cache""MISS";
  330.         }
  331.         if (strlen($this->_location->region&& strlen($this->_location->country)) {
  332.             $locname $this->_location->name.", ".$this->_location->region.", ".$this->_location->country;
  333.         elseif (strlen($this->_location->country)) {
  334.             $locname $this->_location->name.", ".$this->_location->country;
  335.         else {
  336.             $locname $this->_location->name;
  337.         }
  338.         $locationReturn["name"]      $locname;
  339.         $locationReturn["latitude"]  $this->_location->latitude;
  340.         $locationReturn["longitude"$this->_location->longitude;
  341.         $locationReturn["sunrise"]   date($this->_timeFormat$this->calculateSunRiseSet(gmmktime()SUNFUNCS_RET_TIMESTAMP$this->_location->latitude$this->_location->longitudeSERVICES_WEATHER_SUNFUNCS_SUNRISE_ZENITH0true));
  342.         $locationReturn["sunset"]    date($this->_timeFormat$this->calculateSunRiseSet(gmmktime()SUNFUNCS_RET_TIMESTAMP$this->_location->latitude$this->_location->longitudeSERVICES_WEATHER_SUNFUNCS_SUNSET_ZENITH,  0false));
  343.         $locationReturn["elevation"$this->_location->elevation;
  344.  
  345.         return $locationReturn;
  346.     }
  347.     // }}}
  348.  
  349.     // {{{ getWeather()
  350.     /**
  351.      * Returns the weather-data for the supplied location
  352.      *
  353.      * @param   string                      $id 
  354.      * @param   string                      $unitsFormat 
  355.      * @return  PEAR_Error|array
  356.      * @throws  PEAR_Error
  357.      * @access  public
  358.      */
  359.     function getWeather($id ""$unitsFormat "")
  360.     {
  361.         static $clouds;
  362.         if (!isset($clouds)) {
  363.             $clouds    = array(
  364.                 "sky clear",
  365.                 "few",
  366.                 "scattered",
  367.                 "broken",
  368.                 "overcast",
  369.             );
  370.         }
  371.  
  372.         $status $this->_checkLocationID($id);
  373.  
  374.         if (Services_Weather::isError($status)) {
  375.             return $status;
  376.         }
  377.  
  378.         // Get other data
  379.         $units    $this->getUnitsFormat($unitsFormat);
  380.  
  381.         $weatherReturn = array();
  382.         if ($this->_cacheEnabled && ($weather $this->_cache->get("GW-".$id"weather"))) {
  383.             // Same procedure...
  384.             $this->_weather $weather;
  385.             $weatherReturn["cache""HIT";
  386.         else {
  387.             // Check, if the weatherSoap-Object is present. If not, connect to the Server and retrieve the WDSL data
  388.             if (!$this->_weatherSoap{
  389.                 $status $this->_connectServer();
  390.                 if (Services_Weather::isError($status)) {
  391.                     return $status;
  392.                 }
  393.             }
  394.  
  395.             // ...as last function
  396.             $weather $this->_weatherSoap->getWeatherReport($id);
  397.  
  398.             if (Services_Weather::isError($weather)) {
  399.                 return $weather;
  400.             }
  401.  
  402.             $this->_weather $weather;
  403.  
  404.             if ($this->_cacheEnabled{
  405.                 // ...and cache it
  406.                 $expire constant("SERVICES_WEATHER_EXPIRES_WEATHER");
  407.                 $this->_cache->extSave("GW-".$id$this->_weather""$expire"weather");
  408.             }
  409.             $weatherReturn["cache""MISS";
  410.         }
  411.  
  412.         $update trim(str_replace(array("T""Z")" "$this->_weather->timestamp))." GMT";
  413.  
  414.         $weatherReturn["update"]            gmdate(trim($this->_dateFormat." ".$this->_timeFormat)strtotime($update));
  415.         $weatherReturn["updateRaw"]         $this->_weather->timestamp;
  416.         if (strlen($this->_weather->station->region&& strlen($this->_weather->station->country)) {
  417.             $locname $this->_weather->station->name.", ".$this->_weather->station->region.", ".$this->_weather->station->country;
  418.         elseif (strlen($this->_weather->station->country)) {
  419.             $locname $this->_weather->station->name.", ".$this->_weather->station->country;
  420.         else {
  421.             $locname $this->_weather->station->name;
  422.         }
  423.         $weatherReturn["station"]           $locname;
  424.         $weatherReturn["wind"]              $this->convertSpeed($this->_weather->wind->prevailing_speed"mps"$units["wind"]);
  425.         $weatherReturn["windDegrees"]       $this->_weather->wind->prevailing_direction->degrees;
  426.         $weatherReturn["windDirection"]     $this->_weather->wind->prevailing_direction->compass;
  427.         if ($this->_weather->wind->prevailing_speed != $this->_weather->wind->gust_speed{
  428.             $weatherReturn["windGust"]      $this->convertSpeed($this->_weather->wind->gust_speed"mps"$units["wind"]);
  429.         }
  430.         if ($this->_weather->wind->varying_from_direction != "" && $this->_weather->wind->varying_to_direction != ""{
  431.             $weatherReturn["windVar"]       = array (
  432.                 "from" => $this->_weather->wind->varying_from_direction,
  433.                 "to"   => $this->_weather->wind->varying_to_direction
  434.             );
  435.         }
  436.  
  437.         $weatherReturn["visibility"]        $this->convertDistance($this->_weather->visibility->distance / 1000"km"$units["vis"]);
  438.         $weatherReturn["visQualifier"]      $this->_weather->visibility->qualifier;
  439.  
  440.         $condition = array();
  441.         for ($i = 0; $i sizeof($this->_weather->phenomena)$i++{
  442.             $condition[$this->_weather->phenomena[$i]->string;
  443.         }
  444.         $weatherReturn["condition"]         implode(", "$condition);
  445.  
  446.         if (is_array($this->_weather->sky->layers)) {
  447.             $layers = array();
  448.             for ($i = 0; $i sizeof($this->_weather->sky->layers)$i++{
  449.                 if (strtoupper($this->_weather->sky->layers[$i]->type!= "CLEAR"{
  450.                     $layers[$i]             = array();
  451.                     $layers[$i]["amount"]   $clouds[$this->_weather->sky->layers[$i]->extent];
  452.                     $layers[$i]["height"]   $this->convertDistance($this->_weather->sky->layers[$i]->altitude"m"$units["height"]);
  453.                     if (strtoupper($this->_weather->sky->layers[$i]->type!= "CLOUD"{
  454.                         $layers[$i]["type"ucwords(str_replace("_"""$this->_weather->sky->layers[$i]->type));
  455.                     }
  456.                 }
  457.             }
  458.             if (sizeof($layers)) {
  459.                 $weatherReturn["clouds"]        $layers;
  460.             }
  461.         }
  462.  
  463.         $weatherReturn["temperature"]       $this->convertTemperature($this->_weather->temperature->ambient"c"$units["temp"]);
  464.         $feltTemperature $this->calculateWindChill($this->convertTemperature($weatherReturn["temperature"]$units["temp"]"f")$this->convertSpeed($weatherReturn["wind"]$units["wind"]"mph"));
  465.         $weatherReturn["feltTemperature"]   $this->convertTemperature($feltTemperature"f"$units["temp"]);
  466.         $weatherReturn["dewPoint"]          $this->convertTemperature($this->_weather->temperature->dewpoint"c"$units["temp"]);
  467.         $weatherReturn["humidity"]          $this->_weather->temperature->relative_humidity;
  468.         $weatherReturn["pressure"]          $this->convertPressure($this->_weather->pressure->altimeter"hpa"$units["pres"]);
  469.  
  470.         return $weatherReturn;
  471.     }
  472.     // }}}
  473.  
  474.     // {{{ getForecast()
  475.     /**
  476.      * GlobalWeather has no forecast per se, so this function is just for
  477.      * compatibility purposes.
  478.      *
  479.      * @param   string                      $int 
  480.      * @param   int                         $days 
  481.      * @param   string                      $unitsFormat 
  482.      * @return  bool 
  483.      * @access  public
  484.      * @deprecated
  485.      */
  486.     function getForecast($id = null$days = null$unitsFormat = null)
  487.     {
  488.         return false;
  489.     }
  490.     // }}}
  491. }
  492. // }}}
  493. ?>

Documentation generated on Thu, 10 Dec 2015 05:49:40 +0000 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.