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: */
  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: Globalweather.php,v 1.19 2004/03/31 12:32:58 eru Exp $
  20.  
  21. require_once "Services/Weather/Common.php";
  22.  
  23. // {{{ class Services_Weather_Globalweather
  24. /**
  25. * PEAR::Services_Weather_Globalweather
  26. *
  27. * This class acts as an interface to the soap service of capescience.com. It searches for given
  28. * locations and retrieves current weather data.
  29. *
  30. * GlobalWeather is a SOAP frontend for METAR data, provided by CapeScience. If you want to
  31. * use METAR, you should try this class first, as it is much more comfortable (and also a bit
  32. * faster) than the native METAR-class provided by this package.
  33. *
  34. * For a working example, please take a look at
  35. *     docs/Services_Weather/examples/globalweather-basic.php
  36. *
  37. @author       Alexander Wirtz <alex@pc4p.net>
  38. @link         http://www.capescience.com/webservices/globalweather/index.shtml
  39. @example      docs/Services_Weather/examples/globalweather-basic.php
  40. @package      Services_Weather
  41. @license      http://www.php.net/license/2_02.txt
  42. @version      1.2
  43. */
  44.  
  45.     // {{{ properties
  46.     /**
  47.     * WSDL object, provided by CapeScience
  48.     *
  49.     * @var      object                      $_wsdl 
  50.     * @access   private
  51.     */
  52.     var $_wsdl;
  53.  
  54.     /**
  55.     * SOAP object to access station data, provided by CapeScience
  56.     *
  57.     * @var      object                      $_stationSoap 
  58.     * @access   private
  59.     */
  60.     var $_stationSoap;
  61.  
  62.     /**
  63.     * SOAP object to access weather data, provided by CapeScience
  64.     *
  65.     * @var      object                      $_weaterSoap 
  66.     * @access   private
  67.     */
  68.     var $_weatherSoap;
  69.     // }}}
  70.  
  71.     // {{{ constructor
  72.     /**
  73.     * Constructor
  74.     *
  75.     * Requires SOAP to be installed
  76.     *
  77.     * @param    array                       $options 
  78.     * @param    mixed                       $error 
  79.     * @throws   PEAR_Error
  80.     * @throws   PEAR_Error::SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA
  81.     * @see      Science_Weather::Science_Weather
  82.     * @access   private
  83.     */
  84.     function Services_Weather_Globalweather($options&$error)
  85.     {
  86.         $perror = null;
  87.         $this->Services_Weather_Common($options$perror);
  88.         if (Services_Weather::isError($perror)) {
  89.             $error $perror;
  90.             return;
  91.         }
  92.  
  93.         include_once "SOAP/Client.php";
  94.         $this->_wsdl = new SOAP_WSDL("http://live.capescience.com/wsdl/GlobalWeather.wsdl"array("timeout" => $this->_httpTimeout));
  95.         if (isset($this->_wsdl->fault&& Services_Weather::isError($this->_wsdl->fault)) {
  96.             $error Services_Weather::raiseError(SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA);
  97.             return;
  98.         }
  99.  
  100.         eval($this->_wsdl->generateAllProxies());
  101.         if (!class_exists("WebService_GlobalWeather_StationInfo"|| !class_exists("WebService_GlobalWeather_GlobalWeather")) {
  102.             $error Services_Weather::raiseError(SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA);
  103.             return;
  104.         }
  105.  
  106.         $this->_stationSoap &new WebService_GlobalWeather_StationInfo;
  107.         $this->_weatherSoap &new WebService_GlobalWeather_GlobalWeather;
  108.     }
  109.     // }}}
  110.  
  111.     // {{{ _checkLocationID()
  112.     /**
  113.     * Checks the id for valid values and thus prevents silly requests to GlobalWeather server
  114.     *
  115.     * @param    string                      $id 
  116.     * @return   PEAR_Error|bool
  117.     * @throws   PEAR_Error::SERVICES_WEATHER_ERROR_NO_LOCATION
  118.     * @throws   PEAR_Error::SERVICES_WEATHER_ERROR_INVALID_LOCATION
  119.     * @access   private
  120.     */
  121.     function _checkLocationID($id)
  122.     {
  123.         if (!strlen($id)) {
  124.             return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_NO_LOCATION);
  125.         elseif ($this->_stationSoap->isValidCode($id=== false{
  126.             return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_INVALID_LOCATION);
  127.         }
  128.  
  129.         return true;
  130.     }
  131.     // }}}
  132.  
  133.     // {{{ searchLocation()
  134.     /**
  135.     * Searches IDs for given location, returns array of possible locations or single ID
  136.     *
  137.     * @param    string                      $location 
  138.     * @param    bool                        $useFirst       If set, first ID of result-array is returned
  139.     * @return   PEAR_Error|array|string
  140.     * @throws   PEAR_Error::SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA
  141.     * @throws   PEAR_Error::SERVICES_WEATHER_ERROR_UNKNOWN_LOCATION
  142.     * @access   public
  143.     */
  144.     function searchLocation($location$useFirst = false)
  145.     {
  146.         // Get search data from server and unserialize
  147.         $search $this->_stationSoap->searchByName($location);
  148.  
  149.         if (Services_Weather::isError($search)) {
  150.             return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA);
  151.         else {
  152.             if (!is_array($search|| !sizeof($search)) {
  153.                 return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_UNKNOWN_LOCATION);
  154.             else {
  155.                 if (!$useFirst && (sizeof($search> 1)) {
  156.                     $searchReturn = array();
  157.                     for ($i = 0; $i sizeof($search)$i++{
  158.                         $searchReturn[$search[$i]->icao$search[$i]->name.", ".$search[$i]->country;
  159.                     }
  160.                 elseif ($useFirst || (sizeof($search== 1)) {
  161.                     $searchReturn $search[0]->icao;
  162.                 }
  163.             }
  164.         }
  165.  
  166.         return $searchReturn;
  167.     }
  168.     // }}}
  169.  
  170.     // {{{ searchLocationByCountry()
  171.     /**
  172.     * Returns IDs with location-name for a given country or all available countries, if no value was given
  173.     *
  174.     * @param    string                      $country 
  175.     * @return   PEAR_Error|array
  176.     * @throws   PEAR_Error::SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA
  177.     * @throws   PEAR_Error::SERVICES_WEATHER_ERROR_UNKNOWN_LOCATION
  178.     * @access   public
  179.     */
  180.     function searchLocationByCountry($country "")
  181.     {
  182.         // Return the available countries as no country was given
  183.         if (!strlen($country)) {
  184.             $countries $this->_stationSoap->listCountries();
  185.             if (Services_Weather::isError($countries)) {
  186.                 return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA);
  187.             }
  188.             return $countries;
  189.         }
  190.  
  191.         // Now for the real search
  192.         $countryLocs $this->_stationSoap->searchByCountry($country);
  193.         // Check result for validity
  194.         if (Services_Weather::isError($countryLocs)) {
  195.             return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA);
  196.         elseif (!is_array($countryLocs)) {
  197.             return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_UNKNOWN_LOCATION);
  198.         }
  199.  
  200.         // Construct the result
  201.         $locations = array();
  202.         foreach ($countryLocs as $location{
  203.             $locations[$location->icao$location->name.", ".$location->country;
  204.         }
  205.         asort($locations);
  206.  
  207.         return $locations;
  208.     }
  209.     // }}}
  210.  
  211.     // {{{ getUnits()
  212.     /**
  213.     * Returns the units for the current query
  214.     *
  215.     * @param    string                      $id 
  216.     * @param    string                      $unitsFormat 
  217.     * @return   array 
  218.     * @deprecated
  219.     * @access   public
  220.     */
  221.     function getUnits($id = null$unitsFormat "")
  222.     {
  223.         return $this->getUnitsFormat($unitsFormat);
  224.     }
  225.     // }}}
  226.  
  227.     // {{{ getLocation()
  228.     /**
  229.     * Returns the data for the location belonging to the ID
  230.     *
  231.     * @param    string                      $id 
  232.     * @return   PEAR_Error|array
  233.     * @throws   PEAR_Error
  234.     * @access   public
  235.     */
  236.     function getLocation($id "")
  237.     {
  238.         $status $this->_checkLocationID($id);
  239.  
  240.         if (Services_Weather::isError($status)) {
  241.             return $status;
  242.         }
  243.  
  244.         $locationReturn = array();
  245.  
  246.         if ($this->_cacheEnabled && ($location $this->_cache->get("GW-".$id"location"))) {
  247.             // Get data from cache
  248.             $this->_location $location;
  249.             $locationReturn["cache""HIT";
  250.         else {
  251.             $location $this->_stationSoap->getStation($id);
  252.  
  253.             if (Services_Weather::isError($location)) {
  254.                 return $location;
  255.             }
  256.  
  257.             $this->_location $location;
  258.  
  259.             if ($this->_cacheEnabled{
  260.                 // ...and cache it
  261.                 $expire constant("SERVICES_WEATHER_EXPIRES_LOCATION");
  262.                 $this->_cache->extSave("GW-".$id$this->_location""$expire"location");
  263.             }
  264.             $locationReturn["cache""MISS";
  265.         }
  266.         if (strlen($this->_location->region&& strlen($this->_location->country)) {
  267.             $locname $this->_location->name.", ".$this->_location->region.", ".$this->_location->country;
  268.         elseif (strlen($this->_location->country)) {
  269.             $locname $this->_location->name.", ".$this->_location->country;
  270.         else {
  271.             $locname $this->_location->name;
  272.         }
  273.         $locationReturn["name"]      $locname;
  274.         $locationReturn["latitude"]  $this->_location->latitude;
  275.         $locationReturn["longitude"$this->_location->longitude;
  276.         $locationReturn["elevation"$this->_location->elevation;
  277.  
  278.         return $locationReturn;
  279.     }
  280.     // }}}
  281.  
  282.     // {{{ getWeather()
  283.     /**
  284.     * Returns the weather-data for the supplied location
  285.     *
  286.     * @param    string                      $id 
  287.     * @param    string                      $unitsFormat 
  288.     * @return   PEAR_Error|array
  289.     * @throws   PEAR_Error
  290.     * @access   public
  291.     */
  292.     function getWeather($id ""$unitsFormat "")
  293.     {
  294.         static $clouds;
  295.         if (!isset($clouds)) {
  296.             $clouds    = array(
  297.                 "sky clear",
  298.                 "few",
  299.                 "scattered",
  300.                 "broken",
  301.                 "overcast",
  302.             );
  303.         }
  304.         
  305.         $status $this->_checkLocationID($id);
  306.  
  307.         if (Services_Weather::isError($status)) {
  308.             return $status;
  309.         }
  310.  
  311.         // Get other data
  312.         $units    $this->getUnitsFormat($unitsFormat);
  313.  
  314.         $weatherReturn = array();
  315.         if ($this->_cacheEnabled && ($weather $this->_cache->get("GW-".$id"weather"))) {
  316.             // Same procedure...
  317.             $this->_weather $weather;
  318.             $weatherReturn["cache""HIT";
  319.         else {
  320.             // ...as last function
  321.             $weather $this->_weatherSoap->getWeatherReport($id);
  322.  
  323.             if (Services_Weather::isError($weather)) {
  324.                 return $weather;
  325.             }
  326.  
  327.             $this->_weather $weather;
  328.  
  329.             if ($this->_cacheEnabled{
  330.                 // ...and cache it
  331.                 $expire constant("SERVICES_WEATHER_EXPIRES_WEATHER");
  332.                 $this->_cache->extSave("GW-".$id$this->_weather""$expire"weather");
  333.             }
  334.             $weatherReturn["cache""MISS";
  335.         }
  336.  
  337.         $update trim(str_replace(array("T""Z")" "$this->_weather->timestamp))." GMT";
  338.  
  339.         $weatherReturn["update"]            gmdate(trim($this->_dateFormat." ".$this->_timeFormat)strtotime($update));
  340.         $weatherReturn["updateRaw"]         $this->_weather->timestamp;
  341.         if (strlen($this->_weather->station->region&& strlen($this->_weather->station->country)) {
  342.             $locname $this->_weather->station->name.", ".$this->_weather->station->region.", ".$this->_weather->station->country;
  343.         elseif (strlen($this->_weather->station->country)) {
  344.             $locname $this->_weather->station->name.", ".$this->_weather->station->country;
  345.         else {
  346.             $locname $this->_weather->station->name;
  347.         }
  348.         $weatherReturn["station"]           $locname;
  349.         $weatherReturn["wind"]              $this->convertSpeed($this->_weather->wind->prevailing_speed"mps"$units["wind"]);
  350.         $weatherReturn["windDegrees"]       $this->_weather->wind->prevailing_direction->degrees;
  351.         $weatherReturn["windDirection"]     $this->_weather->wind->prevailing_direction->compass;
  352.         if ($this->_weather->wind->prevailing_speed != $this->_weather->wind->gust_speed{
  353.             $weatherReturn["windGust"]      $this->convertSpeed($this->_weather->wind->gust_speed"mps"$units["wind"]);
  354.         }
  355.         if ($this->_weather->wind->varying_from_direction != "" && $this->_weather->wind->varying_to_direction != ""{
  356.             $weatherReturn["windVar"]       = array (
  357.                 "from" => $this->_weather->wind->varying_from_direction,
  358.                 "to"   => $this->_weather->wind->varying_to_direction
  359.             );
  360.         }
  361.  
  362.         $weatherReturn["visibility"]        $this->convertDistance($this->_weather->visibility->distance / 1000"km"$units["vis"]);
  363.         $weatherReturn["visQualifier"]      $this->_weather->visibility->qualifier;
  364.  
  365.         $condition = array();
  366.         for ($i = 0; $i sizeof($this->_weather->phenomena)$i++{
  367.             $condition[$this->_weather->phenomena[$i]->string;
  368.         }
  369.         $weatherReturn["condition"]         implode(", "$condition);
  370.  
  371.         if (is_array($this->_weather->sky->layers)) {
  372.             $layers = array();
  373.             for ($i = 0; $i sizeof($this->_weather->sky->layers)$i++{
  374.                 if (strtoupper($this->_weather->sky->layers[$i]->type!= "CLEAR"{
  375.                     $layers[$i]             = array();
  376.                     $layers[$i]["amount"]   $clouds[$this->_weather->sky->layers[$i]->extent];
  377.                     $layers[$i]["height"]   $this->convertDistance($this->_weather->sky->layers[$i]->altitude / 1000"km""ft");
  378.                     if (strtoupper($this->_weather->sky->layers[$i]->type!= "CLOUD"{
  379.                         $layers[$i]["type"ucwords(str_replace("_"""$this->_weather->sky->layers[$i]->type));
  380.                     }
  381.                 }
  382.             }
  383.             if (sizeof($layers)) {
  384.                 $weatherReturn["clouds"]        $layers;
  385.             }
  386.         }
  387.  
  388.         $weatherReturn["temperature"]       $this->convertTemperature($this->_weather->temperature->ambient"c"$units["temp"]);
  389.         $feltTemperature $this->calculateWindChill($this->convertTemperature($weatherReturn["temperature"]$units["temp"]"f")$this->convertSpeed($weatherReturn["wind"]$units["wind"]"mph"));
  390.         $weatherReturn["feltTemperature"]   $this->convertTemperature($feltTemperature"f"$units["temp"]);
  391.         $weatherReturn["dewPoint"]          $this->convertTemperature($this->_weather->temperature->dewpoint"c"$units["temp"]);
  392.         $weatherReturn["humidity"]          $this->_weather->temperature->relative_humidity;
  393.         $weatherReturn["pressure"]          $this->convertPressure($this->_weather->pressure->altimeter"hpa"$units["pres"]);
  394.  
  395.         return $weatherReturn;
  396.     }
  397.     // }}}
  398.  
  399.     // {{{ getForecast()
  400.     /**
  401.     * GlobalWeather has no forecast per se, so this function is just for
  402.     * compatibility purposes.
  403.     *
  404.     * @param    string                      $int 
  405.     * @param    int                         $days 
  406.     * @param    string                      $unitsFormat 
  407.     * @return   bool 
  408.     * @access   public
  409.     * @deprecated
  410.     */
  411.     function getForecast($id = null$days = null$unitsFormat = null)
  412.     {
  413.         return false;
  414.     }
  415.     // }}}
  416. }
  417. // }}}
  418. ?>

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