home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / Ejse.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  15.2 KB  |  403 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: Ejse.php,v 1.9 2004/03/31 12:32:58 eru Exp $
  20.  
  21. require_once "Services/Weather/Common.php";
  22.  
  23. // {{{ class Services_Weather_Ejse
  24. /**
  25. * PEAR::Services_Weather_Ejse
  26. *
  27. * This class acts as an interface to the soap service of EJSE. It retrieves
  28. * current weather data and forecasts based on postal codes (ZIP).
  29. *
  30. * Currently this service is only available for US territory.
  31. *
  32. * For a working example, please take a look at
  33. *     docs/Services_Weather/examples/ejse-basic.php
  34. *
  35. * @author       Alexander Wirtz <alex@pc4p.net>
  36. * @link         http://www.ejse.com/services/weather_xml_web_services.htm
  37. * @example      docs/Services_Weather/examples/ejse-basic.php
  38. * @package      Services_Weather
  39. * @license      http://www.php.net/license/2_02.txt
  40. * @version      1.2
  41. */
  42. class Services_Weather_Ejse extends Services_Weather_Common {
  43.  
  44.     // {{{ properties
  45.     /**
  46.     * WSDL object, provided by EJSE
  47.     *
  48.     * @var      object                      $_wsdl
  49.     * @access   private
  50.     */
  51.     var $_wsdl;
  52.  
  53.     /**
  54.     * SOAP object to access weather data, provided by EJSE
  55.     *
  56.     * @var      object                      $_weaterSoap
  57.     * @access   private
  58.     */
  59.     var $_weatherSoap;
  60.     // }}}
  61.  
  62.     // {{{ constructor
  63.     /**
  64.     * Constructor
  65.     *
  66.     * Requires SOAP to be installed
  67.     *
  68.     * @param    array                       $options
  69.     * @param    mixed                       $error
  70.     * @throws   PEAR_Error
  71.     * @throws   PEAR_Error::SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA
  72.     * @see      Science_Weather::Science_Weather
  73.     * @access   private
  74.     */
  75.     function Services_Weather_Ejse($options, &$error)
  76.     {
  77.         $perror = null;
  78.         $this->Services_Weather_Common($options, $perror);
  79.         if (Services_Weather::isError($perror)) {
  80.             $error = $perror;
  81.             return;
  82.         }
  83.  
  84.         include_once "SOAP/Client.php";
  85.         $this->_wsdl = new SOAP_WSDL("http://www.ejse.com/WeatherService/Service.asmx?WSDL", array("timeout" => $this->_httpTimeout));
  86.         if (isset($this->_wsdl->fault) && Services_Weather::isError($this->_wsdl->fault)) {
  87.             $error = Services_Weather::raiseError(SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA);
  88.             return;
  89.         }
  90.  
  91.         eval($this->_wsdl->generateAllProxies());
  92.         if (!class_exists("WebService_Service_ServiceSoap")) {
  93.             $error = Services_Weather::raiseError(SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA);
  94.             return;
  95.         }
  96.  
  97.         $this->_weatherSoap = &new WebService_Service_ServiceSoap;
  98.     }
  99.     // }}}
  100.  
  101.     // {{{ _checkLocationID()
  102.     /**
  103.     * Checks the id for valid values and thus prevents silly requests to EJSE server
  104.     *
  105.     * @param    string                      $id
  106.     * @return   PEAR_Error|bool
  107.     * @throws   PEAR_Error::SERVICES_WEATHER_ERROR_NO_LOCATION
  108.     * @throws   PEAR_Error::SERVICES_WEATHER_ERROR_INVALID_LOCATION
  109.     * @access   private
  110.     */
  111.     function _checkLocationID($id)
  112.     {
  113.         if (!strlen($id)) {
  114.             return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_NO_LOCATION);
  115.         } elseif (!ctype_digit($id) || (strlen($id) != 5)) {
  116.             return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_INVALID_LOCATION);
  117.         }
  118.  
  119.         return true;
  120.     }
  121.     // }}}
  122.  
  123.     // {{{ searchLocation()
  124.     /**
  125.     * EJSE offers no search function to date, so this function is disabled.
  126.     * Maybe this is the place to interface to some online postcode service... 
  127.     *
  128.     * @param    string                      $location
  129.     * @param    bool                        $useFirst
  130.     * @return   bool
  131.     * @access   public
  132.     * @deprecated
  133.     */
  134.     function searchLocation($location = null, $useFirst = null)
  135.     {
  136.         return $false;
  137.     }
  138.     // }}}
  139.  
  140.     // {{{ searchLocationByCountry()
  141.     /**
  142.     * EJSE offers no search function to date, so this function is disabled.
  143.     * Maybe this is the place to interface to some online postcode service... 
  144.     *
  145.     * @param    string                      $country
  146.     * @return   bool
  147.     * @access   public
  148.     * @deprecated
  149.     */
  150.     function searchLocationByCountry($country = null)
  151.     {
  152.         return $false;
  153.     }
  154.     // }}}
  155.  
  156.     // {{{ getUnits()
  157.     /**
  158.     * Returns the units for the current query
  159.     *
  160.     * @param    string                      $id
  161.     * @param    string                      $unitsFormat
  162.     * @return   array
  163.     * @deprecated
  164.     * @access   public
  165.     */
  166.     function getUnits($id = null, $unitsFormat = "")
  167.     {
  168.         return $this->getUnitsFormat($unitsFormat);
  169.     }
  170.     // }}}
  171.  
  172.     // {{{ getLocation()
  173.     /**
  174.     * Returns the data for the location belonging to the ID
  175.     *
  176.     * @param    string                      $id
  177.     * @return   PEAR_Error|array
  178.     * @throws   PEAR_Error
  179.     * @access   public
  180.     */
  181.     function getLocation($id = "")
  182.     {
  183.         $status = $this->_checkLocationID($id);
  184.  
  185.         if (Services_Weather::isError($status)) {
  186.             return $status;
  187.         }
  188.  
  189.         $locationReturn = array();
  190.  
  191.         if ($this->_cacheEnabled && ($weather = $this->_cache->get($id, "weather"))) {
  192.             // Get data from cache
  193.             $this->_weather = $weather;
  194.             $locationReturn["cache"] = "HIT";
  195.         } else {
  196.             $weather = $this->_weatherSoap->getWeatherInfo($id);
  197.  
  198.             if (Services_Weather::isError($weather)) {
  199.                 return $weather;
  200.             }
  201.  
  202.             $this->_weather = $weather;
  203.  
  204.             if ($this->_cacheEnabled) {
  205.                 // ...and cache it
  206.                 $expire = constant("SERVICES_WEATHER_EXPIRES_WEATHER");
  207.                 $this->_cache->extSave($id, $this->_weather, "", $expire, "weather");
  208.             }
  209.             $locationReturn["cache"] = "MISS";
  210.         }
  211.         $locationReturn["name"] = $this->_weather->Location;
  212.  
  213.         return $locationReturn;
  214.     }
  215.     // }}}
  216.  
  217.     // {{{ getWeather()
  218.     /**
  219.     * Returns the weather-data for the supplied location
  220.     *
  221.     * @param    string                      $id
  222.     * @param    string                      $unitsFormat
  223.     * @return   PEAR_Error|array
  224.     * @throws   PEAR_Error
  225.     * @access   public
  226.     */
  227.     function getWeather($id = "", $unitsFormat = "")
  228.     {
  229.         $status = $this->_checkLocationID($id);
  230.  
  231.         if (Services_Weather::isError($status)) {
  232.             return $status;
  233.         }
  234.  
  235.         // Get other data
  236.         $units    = $this->getUnitsFormat($unitsFormat);
  237.  
  238.         $weatherReturn = array();
  239.         if ($this->_cacheEnabled && ($weather = $this->_cache->get($id, "weather"))) {
  240.             // Same procedure...
  241.             $this->_weather = $weather;
  242.             $weatherReturn["cache"] = "HIT";
  243.         } else {
  244.             // ...as last function
  245.             $weather = $this->_weatherSoap->getWeatherInfo($id);
  246.  
  247.             if (Services_Weather::isError($weather)) {
  248.                 return $weather;
  249.             }
  250.  
  251.             $this->_weather = $weather;
  252.  
  253.             if ($this->_cacheEnabled) {
  254.                 // ...and cache it
  255.                 $expire = constant("SERVICES_WEATHER_EXPIRES_WEATHER");
  256.                 $this->_cache->extSave($id, $this->_weather, "", $expire, "weather");
  257.             }
  258.             $weatherReturn["cache"] = "MISS";
  259.         }
  260.  
  261.         if (!isset($compass)) {
  262.             // Yes, NNE and the likes are multiples of 22.5, but as the other
  263.             // services return integers for this value, these directions are
  264.             // rounded up
  265.             $compass = array(
  266.                 "north"             => array("N",     0),
  267.                 "north northeast"   => array("NNE",  23),
  268.                 "northeast"         => array("NE",   45),
  269.                 "east northeast"    => array("ENE",  68),
  270.                 "east"              => array("E",    90),
  271.                 "east southeast"    => array("ESE", 113),
  272.                 "southeast"         => array("SE",  135),
  273.                 "south southeast"   => array("SSE", 158),
  274.                 "south"             => array("S",   180),
  275.                 "south southwest"   => array("SSW", 203),
  276.                 "southwest"         => array("SW",  225),
  277.                 "west southwest"    => array("WSW", 248),
  278.                 "west"              => array("W",   270),
  279.                 "west northwest"    => array("WNW", 293),
  280.                 "northwest"         => array("NW",  315),
  281.                 "north northwest"   => array("NNW", 338)
  282.             );
  283.         }
  284.  
  285.         preg_match("/(\w+) (\d+), (\d+), at (\d+:\d+ \wM) [^\(]+(\(([^\)]+)\))?/", $this->_weather->LastUpdated, $update);
  286.         if (isset($update[5])) {
  287.             $timestring = $update[6];
  288.         } else {
  289.             $timestring = $update[2]." ".$update[1]." ".$update[3]." ".$update[4]." EST";
  290.         }
  291.         $weatherReturn["update"]            = gmdate(trim($this->_dateFormat." ".$this->_timeFormat), strtotime($timestring));
  292.         $weatherReturn["updateRaw"]            = $this->_weather->LastUpdated;
  293.         $weatherReturn["station"]           = $this->_weather->ReportedAt;
  294.         $weatherReturn["conditionIcon"]     = $this->_weather->IconIndex;
  295.         preg_match("/(-?\d+)\D+/", $this->_weather->Temprature, $temperature);        
  296.         $weatherReturn["temperature"]       = $this->convertTemperature($temperature[1], "f", $units["temp"]);
  297.         preg_match("/(-?\d+)\D+/", $this->_weather->FeelsLike, $feltTemperature);        
  298.         $weatherReturn["feltTemperature"]   = $this->convertTemperature($feltTemperature[1], "f", $units["temp"]);
  299.         $weatherReturn["condition"]         = $this->_weather->Forecast;
  300.         if (preg_match("/([\d\.]+)\D+/", $this->_weather->Visibility, $visibility)) { 
  301.             $weatherReturn["visibility"]    = $this->convertDistance($visibility[1], "sm", $units["vis"]);
  302.         } else {
  303.             $weatherReturn["visibility"]    = trim($this->_weather->Visibility);
  304.         } 
  305.         preg_match("/([\d\.]+) inches and (\w+)/", $this->_weather->Pressure, $pressure);
  306.         $weatherReturn["pressure"]          = $this->convertPressure($pressure[1], "in", $units["pres"]);        
  307.         $weatherReturn["pressureTrend"]     = $pressure[2];
  308.         preg_match("/(-?\d+)\D+/", $this->_weather->DewPoint, $dewPoint);      
  309.         $weatherReturn["dewPoint"]          = $this->convertTemperature($dewPoint[1], "f", $units["temp"]);
  310.         preg_match("/(\d+) (\w+)/", $this->_weather->UVIndex, $uvIndex);
  311.         $weatherReturn["uvIndex"]           = $uvIndex[1];
  312.         $weatherReturn["uvText"]            = $uvIndex[2];
  313.         $weatherReturn["humidity"]          = str_replace("%", "", $this->_weather->Humidity);
  314.         if (preg_match("/From the ([\w\ ]+) at ([\d\.]+) (gusting to ([\d\.]+) )?mph/", $this->_weather->Wind, $wind)) {
  315.             $weatherReturn["wind"]              = $this->convertSpeed($wind[2], "mph", $units["wind"]);
  316.             if (isset($wind[4])) {
  317.                 $weatherReturn["windGust"]      = $this->convertSpeed($wind[4], "mph", $units["wind"]);
  318.             }
  319.             $weatherReturn["windDegrees"]       = $compass[strtolower($wind[1])][1];
  320.             $weatherReturn["windDirection"]     = $compass[strtolower($wind[1])][0];
  321.         } elseif (strtolower($this->_weather->Wind) == "calm") {
  322.             $weatherReturn["wind"]          = 0;
  323.             $weatherReturn["windDegrees"]   = 0;
  324.             $weatherReturn["windDirection"] = "CALM";
  325.         }
  326.  
  327.         return $weatherReturn;
  328.     }
  329.     // }}}
  330.  
  331.     // {{{ getForecast()
  332.     /**
  333.     * Get the forecast for the next days
  334.     *
  335.     * @param    string                      $int
  336.     * @param    int                         $days           Values between 1 and 9
  337.     * @param    string                      $unitsFormat
  338.     * @return   PEAR_Error|array
  339.     * @throws   PEAR_Error
  340.     * @access   public
  341.     */
  342.     function getForecast($id = "", $days = 2, $unitsFormat = "")
  343.     {
  344.         $status = $this->_checkLocationID($id);
  345.  
  346.         if (Services_Weather::isError($status)) {
  347.             return $status;
  348.         }
  349.         if (!in_array($days, range(1, 9))) {
  350.             $days = 2;
  351.         }
  352.  
  353.         // Get other data
  354.         $units    = $this->getUnitsFormat($unitsFormat);
  355.  
  356.         $forecastReturn = array();
  357.         if ($this->_cacheEnabled && ($forecast = $this->_cache->get($id, "forecast"))) {
  358.             // Same procedure...
  359.             $this->_forecast = $forecast;
  360.             $forecastReturn["cache"] = "HIT";
  361.         } else {
  362.             // ...as last function
  363.             $forecast = $this->_weatherSoap->GetNineDayForecastInfo($id);
  364.  
  365.             if (Services_Weather::isError($forecast)) {
  366.                 return $forecast;
  367.             }
  368.  
  369.             $this->_forecast = $forecast;
  370.  
  371.             if ($this->_cacheEnabled) {
  372.                 // ...and cache it
  373.                 $expire = constant("SERVICES_WEATHER_EXPIRES_FORECAST");
  374.                 $this->_cache->extSave($id, $this->_forecast, "", $expire, "forecast");
  375.             }
  376.             $forecastReturn["cache"] = "MISS";
  377.         }
  378.  
  379.         $forecastReturn["days"]   = array();
  380.  
  381.         for ($i = 1; $i <= $days; $i++) {
  382.             preg_match("/(-?\d+)\D+/", $this->_forecast->{"Day".$i}->High, $temperatureHigh);        
  383.             preg_match("/(-?\d+)\D+/", $this->_forecast->{"Day".$i}->Low, $temperatureLow);        
  384.             $day = array(
  385.                 "tempertureHigh" => $this->convertTemperature($temperatureHigh[1], "f", $units["temp"]),
  386.                 "temperatureLow" => $this->convertTemperature($temperatureLow[1], "f", $units["temp"]),
  387.                 "day" => array(
  388.                     "condition"     => $this->_forecast->{"Day".$i}->Forecast,
  389.                     "conditionIcon" => $this->_forecast->{"Day".$i}->IconIndex,
  390.                     "precipitation" => trim(str_replace("%", "", $this->_forecast->{"Day".$i}->PrecipChance))
  391.                 )
  392.             );
  393.  
  394.             $forecastReturn["days"][] = $day;
  395.         }
  396.  
  397.         return $forecastReturn;        
  398.     }
  399.     // }}}
  400. }
  401. // }}}
  402. ?>
  403.