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

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