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

Source for file weather.com-extensive.php

Documentation is available at weather.com-extensive.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
  3.  
  4. /**
  5.  * Elaborate example for the GlobalWeather-service
  6.  *
  7.  * Well, this is a more elaborate example how to create a neat little page
  8.  * with fetching data from weather.com and putting it into a design.
  9.  * I'm not too proud of my design-skills, most of the stuff here is taken
  10.  * from the weather-block which can be found within the Horde Framework,
  11.  * courtesy of Rick Emery - creative pixelshoving isn't my domain :-P
  12.  * I've used a Firefox for checking the design, so don't be too
  13.  * disappointed, if the page looks shabby with the IE, not that I care
  14.  * very much anyway ;-)
  15.  * If you want to know where you can obtain the icons, please register at
  16.  * http://www.weather.com/services/xmloap.html and you'll receive an eMail
  17.  * containing the partner id, the license key and a link pointing to the
  18.  * SDK. Put the 32x32 and the logos folders into the images/ directory.
  19.  * The design should adhere to the conventions defined by the weather.com
  20.  * SDK-guide, though I won't give any legal certainty for this (of course).
  21.  * Have fun!
  22.  * 
  23.  * PHP versions 4 and 5
  24.  *
  25.  * <LICENSE>
  26.  * Copyright (c) 2005-2011, Alexander Wirtz
  27.  * All rights reserved.
  28.  * 
  29.  * Redistribution and use in source and binary forms, with or without
  30.  * modification, are permitted provided that the following conditions
  31.  * are met:
  32.  * o Redistributions of source code must retain the above copyright notice,
  33.  *   this list of conditions and the following disclaimer.
  34.  * o Redistributions in binary form must reproduce the above copyright notice,
  35.  *   this list of conditions and the following disclaimer in the documentation
  36.  *   and/or other materials provided with the distribution.
  37.  * o Neither the name of the software nor the names of its contributors
  38.  *   may be used to endorse or promote products derived from this software
  39.  *   without specific prior written permission.
  40.  * 
  41.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  42.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  45.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  46.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  47.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  48.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  49.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  51.  * POSSIBILITY OF SUCH DAMAGE.
  52.  * </LICENSE>
  53.  * 
  54.  * @category    Web Services
  55.  * @package     Services_Weather
  56.  * @author      Alexander Wirtz <eru@php.net>
  57.  * @copyright   2005-2011 Alexander Wirtz
  58.  * @license     http://www.opensource.org/licenses/bsd-license.php  BSD License
  59.  * @version     SVN: $Id$
  60.  * @link        http://pear.php.net/package/Services_Weather
  61.  * @filesource
  62.  */
  63.  
  64. //-------------------------------------------------------------------------
  65. // This is the area, where you can customize the script
  66. //-------------------------------------------------------------------------
  67. $location     = "Bonn, Germany"; // The city we want to fetch the data for.
  68. $forecastDays = 4;               // This regulates the amount of displayed
  69.                                  // dates in the forecast. (1 <= x <= 10)
  70. $partnerID    = "<PartnerID>";   // As provided by weather.com in the
  71. $licenseKey   = "<LicenseKey>";  // registration eMail.
  72. $cacheType    = "";              // Set a type (file, db, mdb, ...) to
  73.                                  // enable caching.
  74. $cacheOpt     = array();         // Cache needs various options, depending
  75.                                  // on the container-type - please consult
  76.                                  // the Cache manual / sourcecode!
  77. $unitsFormat  = "metric";        // The format the units are displayed in -
  78.                                  // metric, standard or some customization.
  79. $dateFormat   = "Y-m-d";         // Set the format the date is displayed in
  80.                                  // Changing it will break a few things in
  81.                                  // this script, but usually you can define
  82.                                  // this to your likings.
  83. $timeFormat   = "H:i";           // Set the format the time is displayed in
  84. //-------------------------------------------------------------------------
  85.  
  86. // Load the Weather class
  87. require_once "Services/Weather.php";
  88.  
  89. // Object initialization - error checking is important, because of
  90. // handling exceptions such as missing PEAR modules
  91. $weatherDotCom = &Services_Weather::service("WeatherDotCom", array("httpTimeout" => 30));
  92. if (Services_Weather::isError($weatherDotCom)) {
  93.     die("Error: ".$weatherDotCom->getMessage()."\n");
  94. }
  95.  
  96. // Set weather.com partner data
  97. $weatherDotCom->setAccountData($partnerID, $licenseKey);
  98.  
  99. // Initialize caching
  100. if (strlen($cacheType)) {
  101.     $status = $weatherDotCom->setCache($cacheType, $cacheOpt);
  102.     if (Services_Weather::isError($status)) {
  103.         echo "Error: ".$status->getMessage()."\n";
  104.     }
  105. }
  106.  
  107. // Define the units format, bring the retrieved format into
  108. // something more common...
  109. $weatherDotCom->setUnitsFormat($unitsFormat);
  110. $units = $weatherDotCom->getUnitsFormat();
  111. $units["temp"]   = "&deg;".strtoupper($units["temp"]);
  112. $units["wind"]   = "&nbsp;".str_replace("kmh", "km/h", $units["wind"]);
  113. $units["vis"]    = "&nbsp;".$units["vis"];
  114. $units["height"] = "&nbsp;".$units["height"];
  115. $units["pres"]   = "&nbsp;".$units["pres"];
  116. $units["rain"]   = "&nbsp;".$units["rain"];
  117.  
  118. // Set date-/time-format
  119. $weatherDotCom->setDateTimeFormat($dateFormat, $timeFormat);
  120.  
  121. // Search for defined location and fetch the first item found.
  122. // Bail out if something bad happens...
  123. $search = $weatherDotCom->searchLocation($location, true);
  124. if (Services_Weather::isError($search)) {
  125.     die("Error: ".$search->getMessage()."\n");
  126. }
  127.  
  128. // Retrieve data, store in variables, bail out on error
  129. $fetch = array(
  130.     "links"    => "getLinks",
  131.     "location" => "getLocation",
  132.     "weather"  => "getWeather",
  133.     "forecast" => "getForecast"
  134. );
  135. foreach ($fetch as $variable => $function) {
  136.     $$variable = $weatherDotCom->$function($search, $forecastDays);
  137.     if (Services_Weather::isError($$variable)) {
  138.         echo "Error: ".$$variable->getMessage()."\n";
  139.         continue;
  140.     }
  141. }
  142.  
  143. // We need this for some time-checks and displays later
  144. $wupd = strtotime($weather["update"])  + date("Z");
  145. $fupd = strtotime($forecast["update"]) + date("Z");
  146. $fup  = strtotime($forecast["update"]) + $location["timezone"] * 3600;
  147.  
  148. // Check, if we're in the afternoon and if the forecast was updated yet...
  149. // This triggers if the day-forecast for the current day will get shown.
  150. $afternoon = ($location["time"] > "13:59" || date("Ymd", $fup) < date("Ymd")) ? true : false;
  151.  
  152. // The usual notation for condition icons is numeric. Check for numeric icon or "na" and set to "na" if that's not the case
  153. if (!(ctype_digit($weather["conditionIcon"]) || $weather["conditionIcon"] == "na")) {
  154.     $weather["conditionIcon"] = "na";
  155. }
  156.  
  157. // Now we output all the data, please don't expect extensive comments here, this is basic
  158. // HTML/CSS stuff. Also this isn't a very fancy design, it's just to show you, what
  159. // the script is able to do (and more ;-))...
  160. ?>
  161. <html>
  162. <head>
  163.     <title>Services_Weather::Weatherdotcom</title>
  164.     <style type="text/css">
  165.         .normal     { font-family: Arial, Helvetica, sans-serif; font-size: 11pt; font-weight: normal; font-style: normal }
  166.         .italic     { font-weight: normal; font-style: italic }
  167.         .bold       { font-weight: bold; font-style: normal }
  168.         .bolditalic { font-weight: bold; font-style: italic }
  169.         .redbold    { font-weight: bold; font-style: normal; color: #ff0000 }
  170.         .bluebold   { font-weight: bold; font-style: normal; color: #0000ff }
  171.         .bggrey     { background-color: #e9e9e9 }
  172.         .bgkhaki    { background-color: #d8d8c0 }
  173.         .reg        { font-size: 7pt; vertical-align: super }
  174.         img         { vertical-align: middle; border-style: none; border-width: 0px }
  175.         a           { font-weight: bold; font-style: italic; color: #993300; text-decoration: none }
  176.         a:visited   { font-weight: bold; font-style: italic; color: #993300; text-decoration: none }
  177.         a:hover     { font-weight: bold; font-style: italic; color: #cc3300; text-decoration: underline }
  178.         table       { border: 0px none black; border-spacing: 0px }
  179.         td          { font-family: Arial, Helvetica, sans-serif; font-size: 11pt; font-weight: normal; font-style: normal }
  180.     </style>
  181. </head>
  182. <body class="normal">
  183. <?php
  184. // Debug outputs the raw data fetched by the foreach-loop above, just for checking...
  185. if (isset($_GET["debug"])) {
  186.     echo "<pre>\n";
  187.     var_dump($links, $location, $weather, $forecast);
  188.     echo "</pre>\n";
  189. } 
  190. ?>
  191. <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>
  192. <table style="width: 100%">
  193. <tr>
  194.     <td>
  195.         <table style="border-top: 2px solid #524b98; border-bottom: 2px solid #e0e3ce; border-left: 2px solid #b8b6c1; border-right: 2px solid #8b87a0; width: 100%">
  196.         <tr class="bgkhaki">
  197.             <td style="width: 310px; border-bottom: 2px solid #abada2" colspan="2"><span class="bold"><?=$location["name"]?></span></td>
  198.             <td style="width: 190px; border-bottom: 2px solid #abada2"><span class="bold">Local time:</span> <?=$location["time"]?> (GMT<?=(($location["timezone"] < 0) ? "" : "+").$location["timezone"]?>)</td>
  199.             <td style="border-bottom: 2px solid #abada2">&nbsp;</td>
  200.         </tr>
  201.         <tr>
  202.             <td><span class="bold">Sunrise:</span> <img style="width: 28px; height: 13px; vertical-align: baseline" alt="Sunrise" src="images/sunrise.gif"> <?=$location["sunrise"]?></td>
  203.             <td colspan="2"><span class="bold">Sunset:</span> <img style="width: 30px; height: 15px; vertical-align: baseline" alt="Sunset" src="images/sunset.gif"> <?=$location["sunset"]?></td>
  204.             <td rowspan="5" valign="middle" align="center">
  205.                 <table style="border-top: 2px solid #524b98; border-bottom: 2px solid #e0e3ce; border-left: 2px solid #b8b6c1; border-right: 2px solid #8b87a0">
  206.                 <tr class="bgkhaki">
  207.                     <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>
  208.                 </tr>
  209. <?php
  210. // Loop through the mandatory links, nothing spectacular
  211. for ($i = 0; $i < sizeof($links["promo"]); $i++) {
  212. ?>
  213.                 <tr class="bggrey">
  214.                     <td><a href="<?=$links["promo"][$i]["link"]?>"><?=$links["promo"][$i]["title"]?></a></td>
  215.                 </tr>
  216. <?php
  217. }
  218. ?>
  219.                 </table>
  220.             </td>
  221.         </tr>
  222.         <tr>
  223.             <td><span class="bold">Temperature:</span> <?=round($weather["temperature"], 1).$units["temp"]?></td>
  224.             <td><span class="bold">Dew point:</span> <?=round($weather["dewPoint"], 1).$units["temp"]?></td>
  225.             <td><span class="bold">Felt temperature:</span> <?=round($weather["feltTemperature"], 1).$units["temp"]?></td>
  226.         </tr>
  227.         <tr>
  228.             <td colspan="2"><span class="bold">Pressure:</span> <?=round($weather["pressure"], 1).$units["pres"]?> and <?=$weather["pressureTrend"]?></td>
  229.             <td><span class="bold">Humidity:</span> <?=$weather["humidity"]?>%</td>
  230.         </tr>
  231.         <tr>
  232.             <td colspan="2"><span class="bold">Wind:</span> <?=strtolower($weather["windDirection"]) == "calm" ? "Calm" : "From the ".$weather["windDirection"]." (".$weather["windDegrees"]."&deg;) at ".round($weather["wind"], 1).$units["wind"]?></td>
  233.             <td><span class="bold">Visibility:</span> <?=round($weather["visibility"], 1).$units["vis"]?></td>
  234.         </tr>
  235.         <tr>
  236.             <td><span class="bold">Current condition:</span><br><?=$weather["condition"]?></td>
  237.             <td><img style="height: 32px; width: 32px" alt="<?=$weather["condition"]?>" src="images/32x32/<?=$weather["conditionIcon"]?>.png"></td>
  238.             <td valign="top"><span class="bold">UV-Index:</span> <?=$weather["uvIndex"]?> (<?=$weather["uvText"]?>)</td>
  239.         </tr>
  240.         </table>
  241.     </td>
  242. </tr>
  243. <tr>
  244.     <td>
  245.         <table style="border-top: 2px solid #524b98; border-bottom: 2px solid #e0e3ce; border-left: 2px solid #b8b6c1; border-right: 2px solid #8b87a0">
  246.         <tr class="bgkhaki">
  247.             <td align="center" style="border-bottom: 2px solid #abada2" colspan="<?=(1 + $forecastDays)?>"><span class="bold"><?=$forecastDays?>-day forecast</span></td>
  248.         </tr>
  249.         <tr valign="top">
  250.             <td style="width: 10%">
  251.                 <table class="bgkhaki" style="width: 100%; border-top: 2px solid #d8d8c0; border-bottom: 2px solid #d8d8c0; border-left: 2px solid #d8d8c0; border-right: 2px solid #8b87a0">
  252.                 <tr>
  253.                     <td align="center" style="height: 15px">&nbsp;</td>
  254.                 <tr>
  255.                     <td align="center" style="height: 45px"><span class="bold">Temperature</span> <span class="redbold">High</span> / <span class="bluebold">Low</span></td>
  256.                 </tr>
  257.                 <tr>
  258.                     <td align="center" style="height: 15px">&nbsp;</td>
  259.                 </tr>
  260.                 <tr>
  261.                     <td align="center" style="height: 75px"><span class="bold">Condition</span></td>
  262.                 </tr>
  263.                 <tr>
  264.                     <td align="center" style="height: 45px"><span class="bold">Precipitation probability</span></td>
  265.                 </tr>            
  266.                 <tr>
  267.                     <td align="center" style="height: 45px"><span class="bold">Wind</span></td>
  268.                 </tr>
  269.                 <tr>
  270.                     <td align="center" style="height: 15px"><span class="bold">Humidity</span></td>
  271.                 </tr>
  272.                 </table>
  273.             </td>
  274. <?php
  275. for ($day = 0; $day < $forecastDays; $day++) {
  276.     // Set name of day
  277.     if ($day == 0) {
  278.         $dayname = "Today";
  279.     } elseif ($day == 1) {
  280.         $dayname = "Tomorrow";
  281.     } else {
  282.         $dayname = date("l", $fup + $day * 86400);
  283.     }
  284.     // Afternoon is only important for today
  285.     $afternoon = ($day == 0) ? $afternoon : false;
  286.  
  287.     // The usual notation for condition icons is numeric. Check for numeric icon or "na" and set to "na" if that's not the case
  288.     if (!(ctype_digit($forecast["days"][$day]["day"]["conditionIcon"]) || $forecast["days"][$day]["day"]["conditionIcon"] == "na")) {
  289.         $forecast["days"][$day]["day"]["conditionIcon"] = "na";
  290.     }
  291.     if (!(ctype_digit($forecast["days"][$day]["night"]["conditionIcon"]) || $forecast["days"][$day]["night"]["conditionIcon"] == "na")) {
  292.         $forecast["days"][$day]["night"]["conditionIcon"] = "na";
  293.     }
  294. ?>
  295.             <td style="width: <?=(90 / $forecastDays)?>%">
  296.                 <table style="width: 100%"<?=($day % 2) ? ' class="bggrey"' : ""?>>
  297.                     <tr>
  298.                         <td align="center" colspan="2" style="height: 15px"><span class="bold"><?=$dayname?></span></td>
  299.                     </tr>
  300.                     <tr>
  301.                         <td align="center" colspan="2" style="height: 45px"><?=$afternoon ? "" : '<span class="redbold">'.round($forecast["days"][$day]["temperatureHigh"], 0).$units["temp"].'</span> / '?><span class="bluebold"><?=round($forecast["days"][$day]["temperatureLow"], 0).$units["temp"]?></span></td>
  302.                     </tr>
  303.                     <tr>
  304.                         <td align="center" style="width: 50%; height: 15px"><?=$afternoon ? "&nbsp;" : '<span class="bold">Day</span>'?></td>
  305.                         <td align="center" style="width: 50%; height: 15px"><span class="bold">Night</span></td>
  306.                     <tr>
  307.                     <tr>
  308.                         <td align="center" style="height: 75px" validn="top">
  309.                             <?=$afternoon ? "&nbsp;" : '<img style="height: 32px; width: 32px" align="top" alt="'.$forecast["days"][$day]["day"]["condition"].'" src="images/32x32/'.$forecast["days"][$day]["day"]["conditionIcon"].'.png">'?><br>
  310.                             <?=$afternoon ? "&nbsp;" : $forecast["days"][$day]["day"]["condition"]?> 
  311.                         </td>
  312.                         <td align="center" style="height: 75px" validn="top">
  313.                             <img style="height: 32px; width: 32px" align="top" alt="<?=$forecast["days"][$day]["night"]["condition"]?>" src="images/32x32/<?=$forecast["days"][$day]["night"]["conditionIcon"]?>.png"><br>
  314.                             <?=$forecast["days"][$day]["night"]["condition"]?> 
  315.                         </td>
  316.                     </tr>
  317.                     <tr>
  318.                         <td align="center" style="height: 45px"><?=$afternoon ? "&nbsp" : $forecast["days"][$day]["day"]["precipitation"]."%"?></td>
  319.                         <td align="center" style="height: 45px"><?=$forecast["days"][$day]["night"]["precipitation"]?>%</td>
  320.                     </tr>
  321.                     <tr>
  322.                         <td align="center" style="height: 45px"><?=$afternoon ? "&nbsp;" : round($forecast["days"][$day]["day"]["wind"], 0).$units["wind"]." from&nbsp;".$forecast["days"][$day]["day"]["windDirection"]?></td>
  323.                         <td align="center" style="height: 45px"><?=round($forecast["days"][$day]["night"]["wind"], 0).$units["wind"]?> from&nbsp;<?=$forecast["days"][$day]["night"]["windDirection"]?></td>
  324.                     </tr>
  325.                     <tr>
  326.                         <td align="center" style="height: 15px"><?=$afternoon ? "" : $forecast["days"][$day]["day"]["humidity"]."%"?></td>
  327.                         <td align="center" style="height: 15px"><?=$forecast["days"][$day]["night"]["humidity"]?>%</td>
  328.                     </tr>
  329.                 </table>
  330.             </td>
  331. <?php
  332. }
  333. ?>
  334.         </tr>
  335.         <tr class="bgkhaki">
  336.             <td style="border-top: 2px solid #abada2">&nbsp;</td>
  337.             <td style="border-top: 2px solid #abada2">Updated: (<?=date($timeFormat, $wupd)?>&nbsp;/&nbsp;<?=date($timeFormat, $fupd)?>)</td>
  338.             <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 style="height: 32px; width: 43px" alt="weather.com(R) logo" src="images/logos/TWClogo_32px.png"></a></td>
  339.         </tr>
  340.         </table>
  341.     </td>
  342. </tr>
  343. </table>
  344. <a href="javascript:history.back()">back</a>
  345. </body>
  346. </html>

Documentation generated on Mon, 11 Mar 2019 15:50:59 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.