Services_Weather
[ class tree: Services_Weather ] [ index: Services_Weather ] [ all elements ]

Source for file Ejse.php

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

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