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

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