Source for file Ejse.php
Documentation is available at Ejse.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
* PEAR::Services_Weather_Ejse
* Copyright (c) 2005-2011, Alexander Wirtz
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* o Neither the name of the software nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* @package Services_Weather
* @author Alexander Wirtz <alex@pc4p.net>
* @copyright 2005-2011 Alexander Wirtz
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Services_Weather
* @link http://www.ejse.com/services/weather_xml_web_services.htm
* @example examples/ejse-basic.php ejse-basic.php
require_once "Services/Weather/Common.php";
// {{{ class Services_Weather_Ejse
* This class acts as an interface to the soap service of EJSE. It retrieves
* current weather data and forecasts based on postal codes (ZIP).
* Currently this service is only available for US territory.
* For a working example, please take a look at
* docs/Services_Weather/examples/ejse-basic.php
* @package Services_Weather
* @author Alexander Wirtz <alex@pc4p.net>
* @copyright 2005-2011 Alexander Wirtz
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version Release: 1.4.7
* @link http://pear.php.net/package/Services_Weather
* @link http://www.ejse.com/services/weather_xml_web_services.htm
* @example examples/ejse-basic.php ejse-basic.php
* Password key at ejse.com
* WSDL object, provided by EJSE
* SOAP object to access weather data, provided by EJSE
* @var object $_weaterSoap
* Requires SOAP to be installed
function Services_Weather_Ejse ($options, &$error)
$this->Services_Weather_Common ($options, $perror);
* Connects to the SOAP server and retrieves the WSDL data
* @return PEAR_Error|bool
* @throws PEAR_Error::SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA
function _connectServer ()
include_once "SOAP/Client.php";
$this->_wsdl = new SOAP_WSDL ("http://www.ejse.com/WeatherService/Service.asmx?WSDL", $this->_httpOptions);
eval ($this->_wsdl->generateAllProxies ());
$this->_weatherSoap = &new WebService_Service_ServiceSoap;
* Sets the neccessary account-information for ejse.com, you'll
* receive them after registering for the service
* @param string $username
* @param string $password
$this->_username = $username;
$this->_password = $password;
// {{{ _checkLocationID()
* Checks the id for valid values and thus prevents silly requests to EJSE server
* @return PEAR_Error|bool
* @throws PEAR_Error::SERVICES_WEATHER_ERROR_NO_LOCATION
* @throws PEAR_Error::SERVICES_WEATHER_ERROR_INVALID_LOCATION
function _checkLocationID ($id)
* EJSE offers no search function to date, so this function is disabled.
* Maybe this is the place to interface to some online postcode service...
* @param string $location
// {{{ searchLocationByCountry()
* EJSE offers no search function to date, so this function is disabled.
* Maybe this is the place to interface to some online postcode service...
* Returns the data for the location belonging to the ID
* @return PEAR_Error|array
$status = $this->_checkLocationID ($id);
$locationReturn = array ();
if ($this->_cacheEnabled && ($weather = $this->_getCache ($id, "weather"))) {
$this->_weather = $weather;
$locationReturn["cache"] = "HIT";
// Check, if the weatherSoap-Object is present. If not, connect to the Server and retrieve the WDSL data
if (!$this->_weatherSoap) {
$status = $this->_connectServer ();
$weather = $this->_weatherSoap->getWeatherInfo2 ($this->_username, $this->_password, $id);
$this->_weather = $weather;
if ($this->_cacheEnabled) {
$this->_saveCache ($id, $this->_weather, "", "weather");
$locationReturn["cache"] = "MISS";
$locationReturn["name"] = $this->_weather->Location;
* Returns the weather-data for the supplied location
* @param string $unitsFormat
* @return PEAR_Error|array
$status = $this->_checkLocationID ($id);
$weatherReturn = array ();
if ($this->_cacheEnabled && ($weather = $this->_getCache ($id, "weather"))) {
$this->_weather = $weather;
$weatherReturn["cache"] = "HIT";
// Check, if the weatherSoap-Object is present. If not, connect to the Server and retrieve the WDSL data
if (!$this->_weatherSoap) {
$status = $this->_connectServer ();
$weather = $this->_weatherSoap->getWeatherInfo2 ($this->_username, $this->_password, $id);
$this->_weather = $weather;
if ($this->_cacheEnabled) {
$this->_saveCache ($id, $this->_weather, "", "weather");
$weatherReturn["cache"] = "MISS";
// Yes, NNE and the likes are multiples of 22.5, but as the other
// services return integers for this value, these directions are
"north" => array ("N", 0 ),
"north northeast" => array ("NNE", 23 ),
"northeast" => array ("NE", 45 ),
"east northeast" => array ("ENE", 68 ),
"east" => array ("E", 90 ),
"east southeast" => array ("ESE", 113 ),
"southeast" => array ("SE", 135 ),
"south southeast" => array ("SSE", 158 ),
"south" => array ("S", 180 ),
"south southwest" => array ("SSW", 203 ),
"southwest" => array ("SW", 225 ),
"west southwest" => array ("WSW", 248 ),
"west" => array ("W", 270 ),
"west northwest" => array ("WNW", 293 ),
"northwest" => array ("NW", 315 ),
"north northwest" => array ("NNW", 338 )
// Initialize some arrays
$feltTemperature = array ();
if (preg_match("/(\w+) (\d+), (\d+), at (\d+:\d+ \wM) [^\(]+(\(([^\)]+)\))?/", $this->_weather->LastUpdated , $update)) {
$timestring = $update[6 ];
$timestring = $update[2 ]. " ". $update[1 ]. " ". $update[3 ]. " ". $update[4 ]. " EST";
$weatherReturn["update"] = gmdate(trim($this->_dateFormat. " ". $this->_timeFormat), strtotime($timestring));
$weatherReturn["update"] = "";
$weatherReturn["updateRaw"] = $this->_weather->LastUpdated;
$weatherReturn["station"] = $this->_weather->ReportedAt;
$weatherReturn["conditionIcon"] = $this->_weather->IconIndex;
preg_match("/(-?\d+)\D+/", $this->_weather->Temprature , $temperature);
$weatherReturn["temperature"] = $this->convertTemperature($temperature[1 ], "f", $units["temp"]);
preg_match("/(-?\d+)\D+/", $this->_weather->FeelsLike , $feltTemperature);
$weatherReturn["feltTemperature"] = $this->convertTemperature($feltTemperature[1 ], "f", $units["temp"]);
$weatherReturn["condition"] = $this->_weather->Forecast;
if (preg_match("/([\d\.]+)\D+/", $this->_weather->Visibility , $visibility)) {
$weatherReturn["visibility"] = $this->convertDistance($visibility[1 ], "sm", $units["vis"]);
$weatherReturn["visibility"] = trim($this->_weather->Visibility );
preg_match("/([\d\.]+) inches and (\w+)/", $this->_weather->Pressure , $pressure);
$weatherReturn["pressure"] = $this->convertPressure($pressure[1 ], "in", $units["pres"]);
$weatherReturn["pressureTrend"] = $pressure[2 ];
preg_match("/(-?\d+)\D+/", $this->_weather->DewPoint , $dewPoint);
$weatherReturn["dewPoint"] = $this->convertTemperature($dewPoint[1 ], "f", $units["temp"]);
preg_match("/(\d+) (\w+)/", $this->_weather->UVIndex , $uvIndex);
$weatherReturn["uvIndex"] = $uvIndex[1 ];
$weatherReturn["uvText"] = $uvIndex[2 ];
$weatherReturn["humidity"] = str_replace("%", "", $this->_weather->Humidity );
if (preg_match("/From the ([\w\ ]+) at ([\d\.]+) (gusting to ([\d\.]+) )?mph/", $this->_weather->Wind , $wind)) {
$weatherReturn["wind"] = $this->convertSpeed($wind[2 ], "mph", $units["wind"]);
$weatherReturn["windGust"] = $this->convertSpeed($wind[4 ], "mph", $units["wind"]);
$weatherReturn["windDegrees"] = $compass[strtolower($wind[1 ])][1 ];
$weatherReturn["windDirection"] = $compass[strtolower($wind[1 ])][0 ];
} elseif (strtolower($this->_weather->Wind ) == "calm") {
$weatherReturn["wind"] = 0;
$weatherReturn["windDegrees"] = 0;
$weatherReturn["windDirection"] = "CALM";
* Get the forecast for the next days
* @param int $days Values between 1 and 9
* @param string $unitsFormat
* @return PEAR_Error|array
function getForecast($id = "", $days = 2 , $unitsFormat = "")
$status = $this->_checkLocationID ($id);
$forecastReturn = array ();
if ($this->_cacheEnabled && ($forecast = $this->_getCache ($id, "forecast"))) {
$this->_forecast = $forecast;
$forecastReturn["cache"] = "HIT";
// Check, if the weatherSoap-Object is present. If not, connect to the Server and retrieve the WDSL data
if (!$this->_weatherSoap) {
$status = $this->_connectServer ();
$forecast = $this->_weatherSoap->GetNineDayForecastInfo2 ($this->_username, $this->_password, $id);
$this->_forecast = $forecast;
if ($this->_cacheEnabled) {
$this->_saveCache ($id, $this->_forecast, "", "forecast");
$forecastReturn["cache"] = "MISS";
$forecastReturn["days"] = array ();
// Initialize some arrays
$temperatureHigh = array ();
$temperatureLow = array ();
for ($i = 1; $i <= $days; $i++ ) {
preg_match("/(-?\d+)\D+/", $this->_forecast->{"Day". $i}->High , $temperatureHigh);
preg_match("/(-?\d+)\D+/", $this->_forecast->{"Day". $i}->Low , $temperatureLow);
"condition" => $this->_forecast ->{"Day". $i}->Forecast ,
"conditionIcon" => $this->_forecast ->{"Day". $i}->IconIndex ,
"precipitation" => trim(str_replace("%", "", $this->_forecast ->{"Day". $i}->PrecipChance ))
$forecastReturn["days"][] = $day;
Documentation generated on Mon, 11 Mar 2019 15:50:58 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|