home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / Globalweather.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  16.5 KB  |  420 lines

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