* Copyright (c) 2005-2011, Alexander Wirtz * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 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. * * * @category Web Services * @package Services_Weather * @author Alexander Wirtz * @copyright 2005-2011 Alexander Wirtz * @license http://www.opensource.org/licenses/bsd-license.php BSD License * @version SVN: $Id$ * @link http://pear.php.net/package/Services_Weather * @filesource */ //------------------------------------------------------------------------- // This is the area, where you can customize the script //------------------------------------------------------------------------- $location = "Bonn, Germany"; // The city we want to fetch the data for. $forecastDays = 4; // This regulates the amount of displayed // dates in the forecast. (1 <= x <= 10) $partnerID = ""; // As provided by weather.com in the $licenseKey = ""; // registration eMail. $cacheType = ""; // Set a type (file, db, mdb, ...) to // enable caching. $cacheOpt = array(); // Cache needs various options, depending // on the container-type - please consult // the Cache manual / sourcecode! $unitsFormat = "metric"; // The format the units are displayed in - // metric, standard or some customization. $dateFormat = "Y-m-d"; // Set the format the date is displayed in // Changing it will break a few things in // this script, but usually you can define // this to your likings. $timeFormat = "H:i"; // Set the format the time is displayed in //------------------------------------------------------------------------- // Load the Weather class require_once "Services/Weather.php"; // Object initialization - error checking is important, because of // handling exceptions such as missing PEAR modules $weatherDotCom = &Services_Weather::service("WeatherDotCom", array("httpTimeout" => 30)); if (Services_Weather::isError($weatherDotCom)) { die("Error: ".$weatherDotCom->getMessage()."\n"); } // Set weather.com partner data $weatherDotCom->setAccountData($partnerID, $licenseKey); // Initialize caching if (strlen($cacheType)) { $status = $weatherDotCom->setCache($cacheType, $cacheOpt); if (Services_Weather::isError($status)) { echo "Error: ".$status->getMessage()."\n"; } } // Define the units format, bring the retrieved format into // something more common... $weatherDotCom->setUnitsFormat($unitsFormat); $units = $weatherDotCom->getUnitsFormat(); $units["temp"] = "°".strtoupper($units["temp"]); $units["wind"] = " ".str_replace("kmh", "km/h", $units["wind"]); $units["vis"] = " ".$units["vis"]; $units["height"] = " ".$units["height"]; $units["pres"] = " ".$units["pres"]; $units["rain"] = " ".$units["rain"]; // Set date-/time-format $weatherDotCom->setDateTimeFormat($dateFormat, $timeFormat); // Search for defined location and fetch the first item found. // Bail out if something bad happens... $search = $weatherDotCom->searchLocation($location, true); if (Services_Weather::isError($search)) { die("Error: ".$search->getMessage()."\n"); } // Retrieve data, store in variables, bail out on error $fetch = array( "links" => "getLinks", "location" => "getLocation", "weather" => "getWeather", "forecast" => "getForecast" ); foreach ($fetch as $variable => $function) { $$variable = $weatherDotCom->$function($search, $forecastDays); if (Services_Weather::isError($$variable)) { echo "Error: ".$$variable->getMessage()."\n"; continue; } } // We need this for some time-checks and displays later $wupd = strtotime($weather["update"]) + date("Z"); $fupd = strtotime($forecast["update"]) + date("Z"); $fup = strtotime($forecast["update"]) + $location["timezone"] * 3600; // Check, if we're in the afternoon and if the forecast was updated yet... // This triggers if the day-forecast for the current day will get shown. $afternoon = ($location["time"] > "13:59" || date("Ymd", $fup) < date("Ymd")) ? true : false; // The usual notation for condition icons is numeric. Check for numeric icon or "na" and set to "na" if that's not the case if (!(ctype_digit($weather["conditionIcon"]) || $weather["conditionIcon"] == "na")) { $weather["conditionIcon"] = "na"; } // Now we output all the data, please don't expect extensive comments here, this is basic // HTML/CSS stuff. Also this isn't a very fancy design, it's just to show you, what // the script is able to do (and more ;-))... ?> Services_Weather::Weatherdotcom \n"; var_dump($links, $location, $weather, $forecast); echo "\n"; } ?> Weather Forecast created with PEARs Services_Weather
Local time: (GMT)  
Sunrise: Sunrise Sunset: Sunset
Featured on weather.com®
">
Temperature: Dew point: Felt temperature:
Pressure: and Humidity: %
Wind: Visibility:
Current condition:
<?=$weather[" src="images/32x32/.png"> UV-Index: ()
-day forecast
 
Temperature High / Low
 
Condition
Precipitation probability
Wind
Humidity
>
'.round($forecast["days"][$day]["temperatureHigh"], 0).$units["temp"].' / '?>
Day'?> Night
'?>
<?=$forecast[" src="images/32x32/.png">
%
from 
%
  Updated: ( / ) Weather data provided by weather.com®weather.com(R) logo
back