Navigation Menu

Skip to content

Commit

Permalink
o Created new extensive weather.com example
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.php.net/repository/pear/packages/Services_Weather/trunk@158085 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
Alexander Wirtz committed May 6, 2004
1 parent 7230862 commit 90b7055
Show file tree
Hide file tree
Showing 3 changed files with 299 additions and 0 deletions.
299 changes: 299 additions & 0 deletions examples/weather.com-extensive.php
@@ -0,0 +1,299 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Alexander Wirtz <alex@pc4p.net> |
// +----------------------------------------------------------------------+
//
// $Id $

/*
* Well, this is a more elaborate example how to create a neat little page
* with fetching data from weather.com and putting it into a design.
* I'm not too proud of my design-skills, most of the stuff here is taken
* from the weather-block which can be found withing the Horde Framework,
* courtesy of Rick Emery - creative pixelshoving isn't my domain :-P
* I've used a Firefox for checking the design, so don't be too
* disappointed, if the page looks shabby with with the IE, not that I care
* very much anyway ;-)
* If you want to know where you can obtain the icons, please register at
* http://www.weather.com/services/xmloap.html you'll recive an eMail
* containing the partner id, the license key and a link pointing to the
* SDK. Put the 32x32 and the logos folders into the images/ directory.
* Have fun!
*/

//-------------------------------------------------------------------------
// 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 = "<PartnerID>"; // As provided by weather.com in the
$licenseKey = "<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 liking.
$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)) {
$weatherDotCom->setCache($cacheType, $cacheOpt);
if (Services_Weather::isError($weatherDotCom)) {
echo "Error: ".$weatherDotCom->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"]);

// 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
$wup = strtotime($weather["update"]) + $location["timezone"] * 3600;
$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 = (date("G") > 13 || date("Ymd", $fup) < date("Ymd")) ? true : false;

// 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 ;-))...
?>
<html>
<head>
<title>Services_Weather::Weatherdotcom</title>
<style type="text/css">
.normal { font-family: Arial, Helvetica, sans-serif; font-size: 11pt; font-weight: normal; font-style: normal }
.italic { font-weight: normal; font-style: italic }
.bold { font-weight: bold; font-style: normal }
.bolditalic { font-weight: bold; font-style: italic }
.redbold { font-weight: bold; font-style: normal; color: #ff0000 }
.bluebold { font-weight: bold; font-style: normal; color: #0000ff }
.bggrey { background-color: #e9e9e9 }
.bgkhaki { background-color: #d8d8c0 }
.reg { font-size: 7pt; vertical-align: super }
img { vertical-align: middle; border-style: none; border-width: 0px }
a { font-weight: bold; font-style: italic; color: #993300; text-decoration: none }
a:visited { font-weight: bold; font-style: italic; color: #993300; text-decoration: none }
a:hover { font-weight: bold; font-style: italic; color: #cc3300; text-decoration: underline }
table { border: 0px none black; border-spacing: 0px }
td { font-family: Arial, Helvetica, sans-serif; font-size: 11pt; font-weight: normal; font-style: normal }
</style>
</head>
<body class="normal">
<?php
// Debug outputs the raw data fetched by the foreach-loop above, just for checking...
if (isset($_GET["debug"])) {
echo "<pre>\n";
var_dump($links, $location, $weather, $forecast);
echo "</pre>\n";
}
?>
<span class="bluebold" style="font-size: 13pt">Weather Forecast</span> created with <a style="font-size: 13pt" href="http://pear.php.net/">PEARs</a> <a style="font-size: 13pt" href="http://pear.php.net/package/Services_Weather/">Services_Weather</a><br>
<table width="100%">
<tr>
<td>
<table style="border-top: 2px solid #524b98; border-bottom: 2px solid #e0e3ce; border-left: 2px solid #b8b6c1; border-right: 2px solid #8b87a0">
<tr class="bgkhaki">
<td colspan="2" style="border-bottom: 2px solid #abada2"><span class="bold"><?=$location["name"]?></span></td>
<td style="border-bottom: 2px solid #abada2"><span class="bold">Local time:</span> <?=$location["time"]?> (GMT<?=(($location["timezone"] < 0) ? "" : "+").$location["timezone"]?>)</td>
</tr>
<tr>
<td><span class="bold">Sunrise:</span> <img width="28" height="13" style="vertical-align: baseline" alt="Sunrise" src="images/sunrise.gif"> <?=$location["sunrise"]?></td>
<td colspan="2"><span class="bold">Sunset:</span> <img width="30" height="15" style="vertical-align: baseline" alt="Sunset" src="images/sunset.gif"> <?=$location["sunset"]?></td>
</tr>
<tr>
<td><span class="bold">Temperature:</span> <?=$weather["temperature"]?>&deg;<?=$units["temp"]?></td>
<td><span class="bold">Dew point:</span> <?=$weather["dewPoint"]?>&deg;<?=$units["temp"]?></td>
<td><span class="bold">Felt temperature:</span> <?=$weather["feltTemperature"]?>&deg;<?=$units["temp"]?></td>
</tr>
<tr>
<td colspan="2"><span class="bold">Pressure:</span> <?=$weather["pressure"]?> <?=$units["pres"]?> and <?=$weather["pressureTrend"]?></td>
<td><span class="bold">Humidity:</span> <?=$weather["humidity"]?>%</td>
</tr>
<tr>
<td colspan="2"><span class="bold">Wind:</span> From the <?=$weather["windDirection"]?> (<?=$weather["windDegrees"]?>&deg;) at <?=$weather["wind"]?> <?=$units["wind"]?></td>
<td><span class="bold">Visibility:</span> <?=$weather["visibility"]?> <?=$units["vis"]?></td>
</tr>
<tr>
<td colspan="2"><span class="bold">Current condition:</span> <img height="32" width="32" alt="<?=$weather["condition"]?>" src="images/32x32/<?=$weather["conditionIcon"]?>.png"> <?=$weather["condition"]?></td>
<td><span class="bold">UV-Index:</span> <?=$weather["uvIndex"]?> (<?=$weather["uvText"]?>)</td>
</tr>
</table>
</td>
<td valign="top">
<table style="border-top: 2px solid #524b98; border-bottom: 2px solid #e0e3ce; border-left: 2px solid #b8b6c1; border-right: 2px solid #8b87a0">
<tr class="bgkhaki">
<td align="center" style="border-bottom: 2px solid #abada2"><span class="bold">Featured on <span class="bolditalic">weather.com<span class="reg">&reg;</span></span>:</span></td>
</tr>
<?php
// Loop through the mandatory links, nothing spectacular
for ($i = 0; $i < sizeof($links["promo"]); $i++) {
?>
<tr>
<td><a href="<?=$links["promo"][$i]["link"]?>"><?=$links["promo"][$i]["title"]?></a></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
<tr>
<td colspan="2">
<table style="border-top: 2px solid #524b98; border-bottom: 2px solid #e0e3ce; border-left: 2px solid #b8b6c1; border-right: 2px solid #8b87a0">
<tr class="bgkhaki">
<td align="center" style="border-bottom: 2px solid #abada2" colspan="<?=(1 + $forecastDays)?>"><span class="bold"><?=$forecastDays?>-day forecast</span></td>
</tr>
<tr valign="top">
<td width="10%">
<table width="100%" class="bgkhaki" style="border-top: 2px solid #d8d8c0; border-bottom: 2px solid #d8d8c0; border-left: 2px solid #d8d8c0; border-right: 2px solid #8b87a0">
<tr>
<td align="center" style="height:15px">&nbsp;</td>
<tr>
<td align="center" style="height:45px"><span class="bold">Temperature</span> <span class="redbold">High</span> / <span class="bluebold">Low</span></td>
</tr>
<tr>
<td align="center" style="height:15px">&nbsp;</td>
</tr>
<tr>
<td align="center" style="height:75px"><span class="bold">Condition</span></td>
</tr>
<tr>
<td align="center" style="height:45px"><span class="bold">Precipitation probability</span></td>
</tr>
<tr>
<td align="center" style="height:45px"><span class="bold">Wind</span></td>
</tr>
<tr>
<td align="center" style="height:15px"><span class="bold">Humidity</span></td>
</tr>
</table>
</td>
<?php
for ($day = 0; $day < $forecastDays; $day++) {
// Set name of day
if ($day == 0) {
$dayname = "Today";
} elseif ($day == 1) {
$dayname = "Tomorrow";
} else {
$dayname = date("l", $fup + $day * 86400);
}
// Afternoon is only important for today
$afternoon = ($day == 0) ? $afternoon : false;
?>
<td width="<?=(90 / $forecastDays)?>%">
<table width="100%"<?=($day % 2) ? ' class="bggrey"' : ""?>>
<tr>
<td align="center" colspan="2" style="height:15px"><span class="bold"><?=$dayname?></span></td>
</tr>
<tr>
<td align="center" colspan="2" style="height:45px"><?=$afternoon ? "" : '<span class="redbold">'.$forecast["days"][$day]["temperatureHigh"].'&deg;'.$units["temp"].'</span> / '?><span class="bluebold"><?=$forecast["days"][$day]["temperatureLow"]?>&deg;<?=$units["temp"]?></span></td>
</tr>
<tr>
<td align="center" width="50%" style="height:15px"><?=$afternoon ? "&nbsp;" : '<span class="bold">Day</span>'?></td>
<td align="center" width="50%" style="height:15px"><span class="bold">Night</span></td>
<tr>
<tr>
<td align="center" style="height:75px" validn="top">
<?=$afternoon ? "&nbsp;" : '<img height="32" width="32" align="top" alt="'.$forecast["days"][$day]["day"]["condition"].'" src="images/32x32/'.$forecast["days"][$day]["day"]["conditionIcon"].'.png">'?><br>
<?=$afternoon ? "&nbsp;" : $forecast["days"][$day]["day"]["condition"]?>
</td>
<td align="center" style="height:75px" validn="top">
<img height="32" width="32" align="top" alt="<?=$forecast["days"][$day]["night"]["condition"]?>" src="images/32x32/<?=$forecast["days"][$day]["night"]["conditionIcon"]?>.png"><br>
<?=$forecast["days"][$day]["night"]["condition"]?>
</td>
</tr>
<tr>
<td align="center" style="height:45px"><?=$afternoon ? "&nbsp" : $forecast["days"][$day]["day"]["precipitation"]."%"?></td>
<td align="center" style="height:45px"><?=$forecast["days"][$day]["night"]["precipitation"]?>%</td>
</tr>
<tr>
<td align="center" style="height:45px"><?=$afternoon ? "&nbsp;" : $forecast["days"][$day]["day"]["wind"]."&nbsp;".$units["wind"]." from&nbsp;".$forecast["days"][0]["day"]["windDirection"]?></td>
<td align="center" style="height:45px"><?=$forecast["days"][$day]["night"]["wind"]?>&nbsp;<?=$units["wind"]?> from&nbsp;<?=$forecast["days"][0]["day"]["windDirection"]?></td>
</tr>
<tr>
<td align="center" style="height:15px"><?=$afternoon ? "" : $forecast["days"][$day]["day"]["humidity"]."%"?></td>
<td align="center" style="height:15px"><?=$forecast["days"][$day]["night"]["humidity"]?>%</td>
</tr>
</table>
</td>
<?php
}
?>
</tr>
<tr class="bgkhaki">
<td style="border-top: 2px solid #abada2">&nbsp;</td>
<td style="border-top: 2px solid #abada2">Updated: (<?=date($timeFormat, $wup)?>&nbsp;/&nbsp;<?=date($timeFormat, $fup)?>)</td>
<td align="right" style="border-top: 2px solid #abada2" colspan="<?=($forecastDays - 1)?>">Weather data provided by <a href="http://www.weather.com/?prod=xoap&par=<?=$partnerID?>">weather.com<span class="reg">&reg;</span></a><a href="http://www.weather.com/?prod=xoap&par=<?=$partnerID?>"><img height="32" width="43" alt="weather.com(R) logo" src="images/logos/TWClogo_32px.png"></a></td>
</tr>
</table>
</td>
</tr>
</table>
<a href="javascript:history.back()">back</a>
</body>
</html>
Binary file added images/sunrise.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/sunset.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 90b7055

Please sign in to comment.