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-2011, 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-2011 Alexander Wirtz
  42.  * @license     http://www.opensource.org/licenses/bsd-license.php  BSD License
  43.  * @version     CVS: $Id$
  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-2011 Alexander Wirtz
  72.  * @license     http://www.opensource.org/licenses/bsd-license.php  BSD License
  73.  * @version     Release: 1.4.7
  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->_getCache("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.                 $this->_saveCache("GW-".$id$this->_location"""location");
  327.             }
  328.             $locationReturn["cache""MISS";
  329.         }
  330.         if (strlen($this->_location->region&& strlen($this->_location->country)) {
  331.             $locname $this->_location->name.", ".$this->_location->region.", ".$this->_location->country;
  332.         elseif (strlen($this->_location->country)) {
  333.             $locname $this->_location->name.", ".$this->_location->country;
  334.         else {
  335.             $locname $this->_location->name;
  336.         }
  337.         $locationReturn["name"]      $locname;
  338.         $locationReturn["latitude"]  $this->_location->latitude;
  339.         $locationReturn["longitude"$this->_location->longitude;
  340.         $locationReturn["sunrise"]   date($this->_timeFormat$this->calculateSunRiseSet(gmmktime()SUNFUNCS_RET_TIMESTAMP$this->_location->latitude$this->_location->longitudeSERVICES_WEATHER_SUNFUNCS_SUNRISE_ZENITH0true));
  341.         $locationReturn["sunset"]    date($this->_timeFormat$this->calculateSunRiseSet(gmmktime()SUNFUNCS_RET_TIMESTAMP$this->_location->latitude$this->_location->longitudeSERVICES_WEATHER_SUNFUNCS_SUNSET_ZENITH,  0false));
  342.         $locationReturn["elevation"$this->_location->elevation;
  343.  
  344.         return $locationReturn;
  345.     }
  346.     // }}}
  347.  
  348.     // {{{ getWeather()
  349.     /**
  350.      * Returns the weather-data for the supplied location
  351.      *
  352.      * @param   string                      $id 
  353.      * @param   string                      $unitsFormat 
  354.      * @return  PEAR_Error|array
  355.      * @throws  PEAR_Error
  356.      * @access  public
  357.      */
  358.     function getWeather($id ""$unitsFormat "")
  359.     {
  360.         static $clouds;
  361.         if (!isset($clouds)) {
  362.             $clouds    = array(
  363.                 "sky clear",
  364.                 "few",
  365.                 "scattered",
  366.                 "broken",
  367.                 "overcast",
  368.             );
  369.         }
  370.  
  371.         $status $this->_checkLocationID($id);
  372.  
  373.         if (Services_Weather::isError($status)) {
  374.             return $status;
  375.         }
  376.  
  377.         // Get other data
  378.         $units    $this->getUnitsFormat($unitsFormat);
  379.  
  380.         $weatherReturn = array();
  381.         if ($this->_cacheEnabled && ($weather $this->_getCache("GW-".$id"weather"))) {
  382.             // Same procedure...
  383.             $this->_weather $weather;
  384.             $weatherReturn["cache""HIT";
  385.         else {
  386.             // Check, if the weatherSoap-Object is present. If not, connect to the Server and retrieve the WDSL data
  387.             if (!$this->_weatherSoap{
  388.                 $status $this->_connectServer();
  389.                 if (Services_Weather::isError($status)) {
  390.                     return $status;
  391.                 }
  392.             }
  393.  
  394.             // ...as last function
  395.             $weather $this->_weatherSoap->getWeatherReport($id);
  396.  
  397.             if (Services_Weather::isError($weather)) {
  398.                 return $weather;
  399.             }
  400.  
  401.             $this->_weather $weather;
  402.  
  403.             if ($this->_cacheEnabled{
  404.                 // ...and cache it
  405.                 $this->_saveCache("GW-".$id$this->_weather"""weather");
  406.             }
  407.             $weatherReturn["cache""MISS";
  408.         }
  409.  
  410.         $update trim(str_replace(array("T""Z")" "$this->_weather->timestamp))." GMT";
  411.  
  412.         $weatherReturn["update"]            gmdate(trim($this->_dateFormat." ".$this->_timeFormat)strtotime($update));
  413.         $weatherReturn["updateRaw"]         $this->_weather->timestamp;
  414.         if (strlen($this->_weather->station->region&& strlen($this->_weather->station->country)) {
  415.             $locname $this->_weather->station->name.", ".$this->_weather->station->region.", ".$this->_weather->station->country;
  416.         elseif (strlen($this->_weather->station->country)) {
  417.             $locname $this->_weather->station->name.", ".$this->_weather->station->country;
  418.         else {
  419.             $locname $this->_weather->station->name;
  420.         }
  421.         $weatherReturn["station"]           $locname;
  422.         $weatherReturn["wind"]              $this->convertSpeed($this->_weather->wind->prevailing_speed"mps"$units["wind"]);
  423.         $weatherReturn["windDegrees"]       $this->_weather->wind->prevailing_direction->degrees;
  424.         $weatherReturn["windDirection"]     $this->_weather->wind->prevailing_direction->compass;
  425.         if ($this->_weather->wind->prevailing_speed != $this->_weather->wind->gust_speed{
  426.             $weatherReturn["windGust"]      $this->convertSpeed($this->_weather->wind->gust_speed"mps"$units["wind"]);
  427.         }
  428.         if ($this->_weather->wind->varying_from_direction != "" && $this->_weather->wind->varying_to_direction != ""{
  429.             $weatherReturn["windVar"]       = array (
  430.                 "from" => $this->_weather->wind->varying_from_direction,
  431.                 "to"   => $this->_weather->wind->varying_to_direction
  432.             );
  433.         }
  434.  
  435.         $weatherReturn["visibility"]        $this->convertDistance($this->_weather->visibility->distance / 1000"km"$units["vis"]);
  436.         $weatherReturn["visQualifier"]      $this->_weather->visibility->qualifier;
  437.  
  438.         $condition = array();
  439.         for ($i = 0; $i sizeof($this->_weather->phenomena)$i++{
  440.             $condition[$this->_weather->phenomena[$i]->string;
  441.         }
  442.         $weatherReturn["condition"]         implode(", "$condition);
  443.  
  444.         if (is_array($this->_weather->sky->layers)) {
  445.             $layers = array();
  446.             for ($i = 0; $i sizeof($this->_weather->sky->layers)$i++{
  447.                 if (strtoupper($this->_weather->sky->layers[$i]->type!= "CLEAR"{
  448.                     $layers[$i]             = array();
  449.                     $layers[$i]["amount"]   $clouds[$this->_weather->sky->layers[$i]->extent];
  450.                     $layers[$i]["height"]   $this->convertDistance($this->_weather->sky->layers[$i]->altitude"m"$units["height"]);
  451.                     if (strtoupper($this->_weather->sky->layers[$i]->type!= "CLOUD"{
  452.                         $layers[$i]["type"ucwords(str_replace("_"""$this->_weather->sky->layers[$i]->type));
  453.                     }
  454.                 }
  455.             }
  456.             if (sizeof($layers)) {
  457.                 $weatherReturn["clouds"]        $layers;
  458.             }
  459.         }
  460.  
  461.         $weatherReturn["temperature"]       $this->convertTemperature($this->_weather->temperature->ambient"c"$units["temp"]);
  462.         $feltTemperature $this->calculateWindChill($this->convertTemperature($weatherReturn["temperature"]$units["temp"]"f")$this->convertSpeed($weatherReturn["wind"]$units["wind"]"mph"));
  463.         $weatherReturn["feltTemperature"]   $this->convertTemperature($feltTemperature"f"$units["temp"]);
  464.         $weatherReturn["dewPoint"]          $this->convertTemperature($this->_weather->temperature->dewpoint"c"$units["temp"]);
  465.         $weatherReturn["humidity"]          $this->_weather->temperature->relative_humidity;
  466.         $weatherReturn["pressure"]          $this->convertPressure($this->_weather->pressure->altimeter"hpa"$units["pres"]);
  467.  
  468.         return $weatherReturn;
  469.     }
  470.     // }}}
  471.  
  472.     // {{{ getForecast()
  473.     /**
  474.      * GlobalWeather has no forecast per se, so this function is just for
  475.      * compatibility purposes.
  476.      *
  477.      * @param   string                      $int 
  478.      * @param   int                         $days 
  479.      * @param   string                      $unitsFormat 
  480.      * @return  bool 
  481.      * @access  public
  482.      * @deprecated
  483.      */
  484.     function getForecast($id = null$days = null$unitsFormat = null)
  485.     {
  486.         return false;
  487.     }
  488.     // }}}
  489. }
  490. // }}}
  491. ?>

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