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($locationtrue);
  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"$fupdate("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. <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>
  191. <table style="width: 100%">
  192. <tr>
  193.     <td>
  194.         <table style="border-top: 2px solid #524b98; border-bottom: 2px solid #e0e3ce; border-left: 2px solid #b8b6c1; border-right: 2px solid #8b87a0; width: 100%">
  195.         <tr class="bgkhaki">
  196.             <td style="width: 310px; border-bottom: 2px solid #abada2" colspan="2"><span class="bold"><?=$location["name"]?></span></td>
  197.             <td style="width: 190px; border-bottom: 2px solid #abada2"><span class="bold">Local time:</span> <?=$location["time"]?> (GMT<?=(($location["timezone"< 0"" "+").$location["timezone"]?>)</td>
  198.             <td style="border-bottom: 2px solid #abada2">&nbsp;</td>
  199.         </tr>
  200.         <tr>
  201.             <td><span class="bold">Sunrise:</span> <img style="width: 28px; height: 13px; vertical-align: baseline" alt="Sunrise" src="images/sunrise.gif"> <?=$location["sunrise"]?></td>
  202.             <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>
  203.             <td rowspan="5" valign="middle" align="center">
  204.                 <table style="border-top: 2px solid #524b98; border-bottom: 2px solid #e0e3ce; border-left: 2px solid #b8b6c1; border-right: 2px solid #8b87a0">
  205.                 <tr class="bgkhaki">
  206.                     <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>
  207.                 </tr>
  208. <?php
  209. // Loop through the mandatory links, nothing spectacular
  210. for ($i = 0; $i sizeof($links["promo"])$i++{
  211. ?>
  212.                 <tr class="bggrey">
  213.                     <td><a href="<?=$links["promo"][$i]["link"]?>"><?=$links["promo"][$i]["title"]?></a></td>
  214.                 </tr>
  215. <?php
  216. }
  217. ?>
  218.                 </table>
  219.             </td>
  220.         </tr>
  221.         <tr>
  222.             <td><span class="bold">Temperature:</span> <?=round($weather["temperature"]1).$units["temp"]?></td>
  223.             <td><span class="bold">Dew point:</span> <?=round($weather["dewPoint"]1).$units["temp"]?></td>
  224.             <td><span class="bold">Felt temperature:</span> <?=round($weather["feltTemperature"]1).$units["temp"]?></td>
  225.         </tr>
  226.         <tr>
  227.             <td colspan="2"><span class="bold">Pressure:</span> <?=round($weather["pressure"]1).$units["pres"]?> and <?=$weather["pressureTrend"]?></td>
  228.             <td><span class="bold">Humidity:</span> <?=$weather["humidity"]?>%</td>
  229.         </tr>
  230.         <tr>
  231.             <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>
  232.             <td><span class="bold">Visibility:</span> <?=round($weather["visibility"]1).$units["vis"]?></td>
  233.         </tr>
  234.         <tr>
  235.             <td><span class="bold">Current condition:</span><br><?=$weather["condition"]?></td>
  236.             <td><img style="height: 32px; width: 32px" alt="<?=$weather["condition"]?>" src="images/32x32/<?=$weather["conditionIcon"]?>.png"></td>
  237.             <td valign="top"><span class="bold">UV-Index:</span> <?=$weather["uvIndex"]?> (<?=$weather["uvText"]?>)</td>
  238.         </tr>
  239.         </table>
  240.     </td>
  241. </tr>
  242. <tr>
  243.     <td>
  244.         <table style="border-top: 2px solid #524b98; border-bottom: 2px solid #e0e3ce; border-left: 2px solid #b8b6c1; border-right: 2px solid #8b87a0">
  245.         <tr class="bgkhaki">
  246.             <td align="center" style="border-bottom: 2px solid #abada2" colspan="<?=(1 + $forecastDays)?>"><span class="bold"><?=$forecastDays?>-day forecast</span></td>
  247.         </tr>
  248.         <tr valign="top">
  249.             <td style="width: 10%">
  250.                 <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">
  251.                 <tr>
  252.                     <td align="center" style="height: 15px">&nbsp;</td>
  253.                 <tr>
  254.                     <td align="center" style="height: 45px"><span class="bold">Temperature</span> <span class="redbold">High</span> / <span class="bluebold">Low</span></td>
  255.                 </tr>
  256.                 <tr>
  257.                     <td align="center" style="height: 15px">&nbsp;</td>
  258.                 </tr>
  259.                 <tr>
  260.                     <td align="center" style="height: 75px"><span class="bold">Condition</span></td>
  261.                 </tr>
  262.                 <tr>
  263.                     <td align="center" style="height: 45px"><span class="bold">Precipitation probability</span></td>
  264.                 </tr>            
  265.                 <tr>
  266.                     <td align="center" style="height: 45px"><span class="bold">Wind</span></td>
  267.                 </tr>
  268.                 <tr>
  269.                     <td align="center" style="height: 15px"><span class="bold">Humidity</span></td>
  270.                 </tr>
  271.                 </table>
  272.             </td>
  273. <?php
  274. for ($day = 0; $day $forecastDays$day++{
  275.     // Set name of day
  276.     if ($day == 0{
  277.         $dayname "Today";
  278.     elseif ($day == 1{
  279.         $dayname "Tomorrow";
  280.     else {
  281.         $dayname date("l"$fup $day * 86400);
  282.     }
  283.     // Afternoon is only important for today
  284.     $afternoon ($day == 0$afternoon : false;
  285.  
  286.     // The usual notation for condition icons is numeric. Check for numeric icon or "na" and set to "na" if that's not the case
  287.     if (!(ctype_digit($forecast["days"][$day]["day"]["conditionIcon"]|| $forecast["days"][$day]["day"]["conditionIcon"== "na")) {
  288.         $forecast["days"][$day]["day"]["conditionIcon""na";
  289.     }
  290.     if (!(ctype_digit($forecast["days"][$day]["night"]["conditionIcon"]|| $forecast["days"][$day]["night"]["conditionIcon"== "na")) {
  291.         $forecast["days"][$day]["night"]["conditionIcon""na";
  292.     }
  293. ?>
  294.             <td style="width: <?=(90 / $forecastDays)?>%">
  295.                 <table style="width: 100%"<?=($day % 2' class="bggrey"' ""?>>
  296.                     <tr>
  297.                         <td align="center" colspan="2" style="height: 15px"><span class="bold"><?=$dayname?></span></td>
  298.                     </tr>
  299.                     <tr>
  300.                         <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>
  301.                     </tr>
  302.                     <tr>
  303.                         <td align="center" style="width: 50%; height: 15px"><?=$afternoon "&nbsp;" '<span class="bold">Day</span>'?></td>
  304.                         <td align="center" style="width: 50%; height: 15px"><span class="bold">Night</span></td>
  305.                     <tr>
  306.                     <tr>
  307.                         <td align="center" style="height: 75px" validn="top">
  308.                             <?=$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>
  309.                             <?=$afternoon "&nbsp;" $forecast["days"][$day]["day"]["condition"]?> 
  310.                         </td>
  311.                         <td align="center" style="height: 75px" validn="top">
  312.                             <img style="height: 32px; width: 32px" align="top" alt="<?=$forecast["days"][$day]["night"]["condition"]?>" src="images/32x32/<?=$forecast["days"][$day]["night"]["conditionIcon"]?>.png"><br>
  313.                             <?=$forecast["days"][$day]["night"]["condition"]?> 
  314.                         </td>
  315.                     </tr>
  316.                     <tr>
  317.                         <td align="center" style="height: 45px"><?=$afternoon "&nbsp" $forecast["days"][$day]["day"]["precipitation"]."%"?></td>
  318.                         <td align="center" style="height: 45px"><?=$forecast["days"][$day]["night"]["precipitation"]?>%</td>
  319.                     </tr>
  320.                     <tr>
  321.                         <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>
  322.                         <td align="center" style="height: 45px"><?=round($forecast["days"][$day]["night"]["wind"]0).$units["wind"]?> from&nbsp;<?=$forecast["days"][$day]["night"]["windDirection"]?></td>
  323.                     </tr>
  324.                     <tr>
  325.                         <td align="center" style="height: 15px"><?=$afternoon "" $forecast["days"][$day]["day"]["humidity"]."%"?></td>
  326.                         <td align="center" style="height: 15px"><?=$forecast["days"][$day]["night"]["humidity"]?>%</td>
  327.                     </tr>
  328.                 </table>
  329.             </td>
  330. <?php
  331. }
  332. ?>
  333.         </tr>
  334.         <tr class="bgkhaki">
  335.             <td style="border-top: 2px solid #abada2">&nbsp;</td>
  336.             <td style="border-top: 2px solid #abada2">Updated: (<?=date($timeFormat$wupd)?>&nbsp;/&nbsp;<?=date($timeFormat$fupd)?>)</td>
  337.             <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>
  338.         </tr>
  339.         </table>
  340.     </td>
  341. </tr>
  342. </table>
  343. <a href="javascript:history.back()">back</a>
  344. </body>
  345. </html>

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