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: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at                              |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Alexander Wirtz <alex@pc4p.net>                             |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: weather.com-extensive.php,v 1.11 2004/07/10 12:37:14 eru Exp $
  20.  
  21. /*
  22.  * Well, this is a more elaborate example how to create a neat little page
  23.  * with fetching data from weather.com and putting it into a design.
  24.  * I'm not too proud of my design-skills, most of the stuff here is taken
  25.  * from the weather-block which can be found within the Horde Framework,
  26.  * courtesy of Rick Emery - creative pixelshoving isn't my domain :-P
  27.  * I've used a Firefox for checking the design, so don't be too
  28.  * disappointed, if the page looks shabby with the IE, not that I care
  29.  * very much anyway ;-)
  30.  * If you want to know where you can obtain the icons, please register at
  31.  * http://www.weather.com/services/xmloap.html and you'll receive an eMail
  32.  * containing the partner id, the license key and a link pointing to the
  33.  * SDK. Put the 32x32 and the logos folders into the images/ directory.
  34.  * The design should adhere to the conventions defined by the weather.com
  35.  * SDK-guide, though I won't give any legal certainty for this (of course).
  36.  * Have fun!
  37. */
  38.  
  39. //-------------------------------------------------------------------------
  40. // This is the area, where you can customize the script
  41. //-------------------------------------------------------------------------
  42. $location     "Bonn, Germany"// The city we want to fetch the data for.
  43. $forecastDays = 4;               // This regulates the amount of displayed
  44.                                  // dates in the forecast. (1 <= x <= 10)
  45. $partnerID    "<PartnerID>";   // As provided by weather.com in the
  46. $licenseKey   "<LicenseKey>";  // registration eMail.
  47. $cacheType    "";              // Set a type (file, db, mdb, ...) to
  48.                                  // enable caching.
  49. $cacheOpt     = array();         // Cache needs various options, depending
  50.                                  // on the container-type - please consult
  51.                                  // the Cache manual / sourcecode!
  52. $unitsFormat  "metric";        // The format the units are displayed in -
  53.                                  // metric, standard or some customization.
  54. $dateFormat   "Y-m-d";         // Set the format the date is displayed in
  55.                                  // Changing it will break a few things in
  56.                                  // this script, but usually you can define
  57.                                  // this to your likings.
  58. $timeFormat   "H:i";           // Set the format the time is displayed in
  59. //-------------------------------------------------------------------------
  60.  
  61. // Load the Weather class
  62. require_once "Services/Weather.php";
  63.  
  64. // Object initialization - error checking is important, because of
  65. // handling exceptions such as missing PEAR modules
  66. $weatherDotCom &Services_Weather::service("WeatherDotCom"array("httpTimeout" => 30));
  67. if (Services_Weather::isError($weatherDotCom)) {
  68.     die("Error: ".$weatherDotCom->getMessage()."\n");
  69. }
  70.  
  71. // Set weather.com partner data
  72. $weatherDotCom->setAccountData($partnerID$licenseKey);
  73.  
  74. // Initialize caching
  75. if (strlen($cacheType)) {
  76.     $status $weatherDotCom->setCache($cacheType$cacheOpt);
  77.     if (Services_Weather::isError($status)) {
  78.         echo "Error: ".$status->getMessage()."\n";
  79.     }
  80. }
  81.  
  82. // Define the units format, bring the retrieved format into
  83. // something more common...
  84. $weatherDotCom->setUnitsFormat($unitsFormat);
  85. $units $weatherDotCom->getUnitsFormat();
  86. $units["temp"]   "&deg;".strtoupper($units["temp"]);
  87. $units["wind"]   "&nbsp;".str_replace("kmh""km/h"$units["wind"]);
  88. $units["vis"]    "&nbsp;".$units["vis"];
  89. $units["height""&nbsp;".$units["height"];
  90. $units["pres"]   "&nbsp;".$units["pres"];
  91. $units["rain"]   "&nbsp;".$units["rain"];
  92.  
  93. // Set date-/time-format
  94. $weatherDotCom->setDateTimeFormat($dateFormat$timeFormat);
  95.  
  96. // Search for defined location and fetch the first item found.
  97. // Bail out if something bad happens...
  98. $search $weatherDotCom->searchLocation($locationtrue);
  99. if (Services_Weather::isError($search)) {
  100.     die("Error: ".$search->getMessage()."\n");
  101. }
  102.  
  103. // Retrieve data, store in variables, bail out on error
  104. $fetch = array(
  105.     "links"    => "getLinks",
  106.     "location" => "getLocation",
  107.     "weather"  => "getWeather",
  108.     "forecast" => "getForecast"
  109. );
  110. foreach ($fetch as $variable => $function{
  111.     $$variable $weatherDotCom->$function($search$forecastDays);
  112.     if (Services_Weather::isError($$variable)) {
  113.         echo "Error: ".$$variable->getMessage()."\n";
  114.         continue;
  115.     }
  116. }
  117.  
  118. // We need this for some time-checks and displays later
  119. $wupd strtotime($weather["update"])  date("Z");
  120. $fupd strtotime($forecast["update"]date("Z");
  121. $fup  strtotime($forecast["update"]$location["timezone"* 3600;
  122.  
  123. // Check, if we're in the afternoon and if the forecast was updated yet...
  124. // This triggers if the day-forecast for the current day will get shown.
  125. $afternoon ($location["time""13:59" || date("Ymd"$fupdate("Ymd")) ? true : false;
  126.  
  127. // Now we output all the data, please don't expect extensive comments here, this is basic
  128. // HTML/CSS stuff. Also this isn't a very fancy design, it's just to show you, what
  129. // the script is able to do (and more ;-))...
  130. ?>
  131. <html>
  132. <head>
  133.     <title>Services_Weather::Weatherdotcom</title>
  134.     <style type="text/css">
  135.         .normal     { font-family: Arial, Helvetica, sans-serif; font-size: 11pt; font-weight: normal; font-style: normal }
  136.         .italic     { font-weight: normal; font-style: italic }
  137.         .bold       { font-weight: bold; font-style: normal }
  138.         .bolditalic { font-weight: bold; font-style: italic }
  139.         .redbold    { font-weight: bold; font-style: normal; color: #ff0000 }
  140.         .bluebold   { font-weight: bold; font-style: normal; color: #0000ff }
  141.         .bggrey     { background-color: #e9e9e9 }
  142.         .bgkhaki    { background-color: #d8d8c0 }
  143.         .reg        { font-size: 7pt; vertical-align: super }
  144.         img         { vertical-align: middle; border-style: none; border-width: 0px }
  145.         a           { font-weight: bold; font-style: italic; color: #993300; text-decoration: none }
  146.         a:visited   { font-weight: bold; font-style: italic; color: #993300; text-decoration: none }
  147.         a:hover     { font-weight: bold; font-style: italic; color: #cc3300; text-decoration: underline }
  148.         table       { border: 0px none black; border-spacing: 0px }
  149.         td          { font-family: Arial, Helvetica, sans-serif; font-size: 11pt; font-weight: normal; font-style: normal }
  150.     </style>
  151. </head>
  152. <body class="normal">
  153. <?php
  154. // Debug outputs the raw data fetched by the foreach-loop above, just for checking...
  155. if (isset($_GET["debug"])) {
  156.     echo "<pre>\n";
  157.     var_dump($links$location$weather$forecast);
  158.     echo "</pre>\n";
  159. ?>
  160. <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>
  161. <table style="width: 100%">
  162. <tr>
  163.     <td>
  164.         <table style="border-top: 2px solid #524b98; border-bottom: 2px solid #e0e3ce; border-left: 2px solid #b8b6c1; border-right: 2px solid #8b87a0; width: 100%">
  165.         <tr class="bgkhaki">
  166.             <td style="width: 310px; border-bottom: 2px solid #abada2" colspan="2"><span class="bold"><?=$location["name"]?></span></td>
  167.             <td style="width: 190px; border-bottom: 2px solid #abada2"><span class="bold">Local time:</span> <?=$location["time"]?> (GMT<?=(($location["timezone"< 0"" "+").$location["timezone"]?>)</td>
  168.             <td style="border-bottom: 2px solid #abada2">&nbsp;</td>
  169.         </tr>
  170.         <tr>
  171.             <td><span class="bold">Sunrise:</span> <img style="width: 28px; height: 13px; vertical-align: baseline" alt="Sunrise" src="images/sunrise.gif"> <?=$location["sunrise"]?></td>
  172.             <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>
  173.             <td rowspan="5" valign="middle" align="center">
  174.                 <table style="border-top: 2px solid #524b98; border-bottom: 2px solid #e0e3ce; border-left: 2px solid #b8b6c1; border-right: 2px solid #8b87a0">
  175.                 <tr class="bgkhaki">
  176.                     <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>
  177.                 </tr>
  178. <?php
  179. // Loop through the mandatory links, nothing spectacular
  180. for ($i = 0; $i sizeof($links["promo"])$i++{
  181. ?>
  182.                 <tr class="bggrey">
  183.                     <td><a href="<?=$links["promo"][$i]["link"]?>"><?=$links["promo"][$i]["title"]?></a></td>
  184.                 </tr>
  185. <?php
  186. }
  187. ?>
  188.                 </table>
  189.             </td>
  190.         </tr>
  191.         <tr>
  192.             <td><span class="bold">Temperature:</span> <?=round($weather["temperature"]1).$units["temp"]?></td>
  193.             <td><span class="bold">Dew point:</span> <?=round($weather["dewPoint"]1).$units["temp"]?></td>
  194.             <td><span class="bold">Felt temperature:</span> <?=round($weather["feltTemperature"]1).$units["temp"]?></td>
  195.         </tr>
  196.         <tr>
  197.             <td colspan="2"><span class="bold">Pressure:</span> <?=round($weather["pressure"]1).$units["pres"]?> and <?=$weather["pressureTrend"]?></td>
  198.             <td><span class="bold">Humidity:</span> <?=$weather["humidity"]?>%</td>
  199.         </tr>
  200.         <tr>
  201.             <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>
  202.             <td><span class="bold">Visibility:</span> <?=round($weather["visibility"]1).$units["vis"]?></td>
  203.         </tr>
  204.         <tr>
  205.             <td><span class="bold">Current condition:</span><br><?=$weather["condition"]?></td>
  206.             <td><img style="height: 32px; width: 32px" alt="<?=$weather["condition"]?>" src="images/32x32/<?=$weather["conditionIcon"]?>.png"></td>
  207.             <td valign="top"><span class="bold">UV-Index:</span> <?=$weather["uvIndex"]?> (<?=$weather["uvText"]?>)</td>
  208.         </tr>
  209.         </table>
  210.     </td>
  211. </tr>
  212. <tr>
  213.     <td>
  214.         <table style="border-top: 2px solid #524b98; border-bottom: 2px solid #e0e3ce; border-left: 2px solid #b8b6c1; border-right: 2px solid #8b87a0">
  215.         <tr class="bgkhaki">
  216.             <td align="center" style="border-bottom: 2px solid #abada2" colspan="<?=(1 + $forecastDays)?>"><span class="bold"><?=$forecastDays?>-day forecast</span></td>
  217.         </tr>
  218.         <tr valign="top">
  219.             <td style="width: 10%">
  220.                 <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">
  221.                 <tr>
  222.                     <td align="center" style="height: 15px">&nbsp;</td>
  223.                 <tr>
  224.                     <td align="center" style="height: 45px"><span class="bold">Temperature</span> <span class="redbold">High</span> / <span class="bluebold">Low</span></td>
  225.                 </tr>
  226.                 <tr>
  227.                     <td align="center" style="height: 15px">&nbsp;</td>
  228.                 </tr>
  229.                 <tr>
  230.                     <td align="center" style="height: 75px"><span class="bold">Condition</span></td>
  231.                 </tr>
  232.                 <tr>
  233.                     <td align="center" style="height: 45px"><span class="bold">Precipitation probability</span></td>
  234.                 </tr>            
  235.                 <tr>
  236.                     <td align="center" style="height: 45px"><span class="bold">Wind</span></td>
  237.                 </tr>
  238.                 <tr>
  239.                     <td align="center" style="height: 15px"><span class="bold">Humidity</span></td>
  240.                 </tr>
  241.                 </table>
  242.             </td>
  243. <?php
  244. for ($day = 0; $day $forecastDays$day++{
  245.     // Set name of day
  246.     if ($day == 0{
  247.         $dayname "Today";
  248.     elseif ($day == 1{
  249.         $dayname "Tomorrow";
  250.     else {
  251.         $dayname date("l"$fup $day * 86400);
  252.     }
  253.     // Afternoon is only important for today
  254.     $afternoon ($day == 0$afternoon : false;
  255. ?>
  256.             <td style="width: <?=(90 / $forecastDays)?>%">
  257.                 <table style="width: 100%"<?=($day % 2' class="bggrey"' ""?>>
  258.                     <tr>
  259.                         <td align="center" colspan="2" style="height: 15px"><span class="bold"><?=$dayname?></span></td>
  260.                     </tr>
  261.                     <tr>
  262.                         <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>
  263.                     </tr>
  264.                     <tr>
  265.                         <td align="center" style="width: 50%; height: 15px"><?=$afternoon "&nbsp;" '<span class="bold">Day</span>'?></td>
  266.                         <td align="center" style="width: 50%; height: 15px"><span class="bold">Night</span></td>
  267.                     <tr>
  268.                     <tr>
  269.                         <td align="center" style="height: 75px" validn="top">
  270.                             <?=$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>
  271.                             <?=$afternoon "&nbsp;" $forecast["days"][$day]["day"]["condition"]?> 
  272.                         </td>
  273.                         <td align="center" style="height: 75px" validn="top">
  274.                             <img style="height: 32px; width: 32px" align="top" alt="<?=$forecast["days"][$day]["night"]["condition"]?>" src="images/32x32/<?=$forecast["days"][$day]["night"]["conditionIcon"]?>.png"><br>
  275.                             <?=$forecast["days"][$day]["night"]["condition"]?> 
  276.                         </td>
  277.                     </tr>
  278.                     <tr>
  279.                         <td align="center" style="height: 45px"><?=$afternoon "&nbsp" $forecast["days"][$day]["day"]["precipitation"]."%"?></td>
  280.                         <td align="center" style="height: 45px"><?=$forecast["days"][$day]["night"]["precipitation"]?>%</td>
  281.                     </tr>
  282.                     <tr>
  283.                         <td align="center" style="height: 45px"><?=$afternoon "&nbsp;" round($forecast["days"][$day]["day"]["wind"]0).$units["wind"]." from&nbsp;".$forecast["days"][0]["day"]["windDirection"]?></td>
  284.                         <td align="center" style="height: 45px"><?=round($forecast["days"][$day]["night"]["wind"]0).$units["wind"]?> from&nbsp;<?=$forecast["days"][0]["day"]["windDirection"]?></td>
  285.                     </tr>
  286.                     <tr>
  287.                         <td align="center" style="height: 15px"><?=$afternoon "" $forecast["days"][$day]["day"]["humidity"]."%"?></td>
  288.                         <td align="center" style="height: 15px"><?=$forecast["days"][$day]["night"]["humidity"]?>%</td>
  289.                     </tr>
  290.                 </table>
  291.             </td>
  292. <?php
  293. }
  294. ?>
  295.         </tr>
  296.         <tr class="bgkhaki">
  297.             <td style="border-top: 2px solid #abada2">&nbsp;</td>
  298.             <td style="border-top: 2px solid #abada2">Updated: (<?=date($timeFormat$wupd)?>&nbsp;/&nbsp;<?=date($timeFormat$fupd)?>)</td>
  299.             <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>
  300.         </tr>
  301.         </table>
  302.     </td>
  303. </tr>
  304. </table>
  305. <a href="javascript:history.back()">back</a>
  306. </body>
  307. </html>

Documentation generated on Mon, 11 Mar 2019 14:17:12 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.