Hack100.Create a Weather Showdown


Hack 100. Create a Weather Showdown

Use Weather.com's web service to build a weather showdown page that will help you decide where you want to be this week (and what to pack).

The Services_Weather PEAR module [Hack #2] makes it easy to integrate weather forecast information into your web application. This hack uses the information from the Weather.comweb service through the Services_ Weather PEAR module to find the weekly temperature range in two different cities. It then compares those temperatures against a desired value and tells you which city is closer to your desired climate.

10.7.1. The Code

Save the code in Example 10-11 as index.php.

Example 10-11. PHP checking the weather forecast
 <?php require_once( "Services/Weather.php" ); $weather = new Services_Weather(); $wdc = $weather->service( 'Weatherdotcom' ); function get_average( $zip ) {   global $wdc;   $fc = $wdc->getForecast( $zip, 7 );   $dayavg = 0;   foreach ( $fc['days'] as $day )   {     $high = $day['temperatureHigh']; $low = $day['temperatureLow']; $dayavg += $high; $dayavg += $low;   }   return floor(($dayavg/14)); } $zipa = isset( $_GET['zipa'] ) ? $_GET['zipa'] : '94587'; $zipb = isset( $_GET['zipb'] ) ? $_GET['zipb'] : '19081'; $desired = isset( $_GET['desired'] ) ? $_GET['desired'] : '65'; $tempa = get_average( $zipa ); $tempb = get_average( $zipb ); $da = abs( $desired - $tempa ); $db = abs( $desired - $tempb ); $victor = ( $da < $db ) ? 1 : 2; $stylea = ( $victor == 1 ) ? "background: #bbb;" : ""; $styleb = ( $victor == 2 ) ? "background: #bbb;" : ""; ?> <html> <head> <title>Average Temperature Showdown</title> <style type="text/css"> td { text-align: center; } </style> </head> <body> <form> <table width="600"> <tr> <th>Desired</th> <th style="<?php echo($stylea); ?>"><input type="text" name="zipa" value="<?php echo( $zipa ); ?>" size="6" /></th> <th style="<?php echo($styleb); ?>"><input type="text" name="zipb" value="<?php echo( $zipb ); ?>" size="6" /></th> </tr> <tr> <td><input type="text" name="desired" value="<?php echo( $desired ); ?>" size="3"   /></td> <td style="<?php echo($stylea); ?>"><?php echo( $tempa ); ?></td> <td style="<?php echo($styleb); ?>"><?php echo( $tempb ); ?></td> </tr> <tr> <td colspan="3"><input type="submit" value="Compare" /></td> <tr> </table> </form> </body> </html> 

The weather service that is exposed through the Services_Weather PEAR module provides a structure of forecast results in response to a Zip Code that is provided. The structure isn't very elaborate. There is an array of days, each day containing a hash table that has the predicted temperature. This page averages the temperature across the days to give a comparison between the two locations.

10.7.2. Running the Hack

Download and install the Services_Weather PEAR module [Hack #2] on your server and then upload the index.php file. Navigate to the page in your web browser; you should see something like Figure 10-19.

Figure 10-19. Comparing the temperature in Union City, California to that in Swarthmore, Pennsylvania


Change the values in the column headers to your local Zip Codes and click the Compare button to see which town is the temperature victor for this coming week. If you want to get fancy, add some Google maps [Hack #95] and really make this a killer application.

10.7.3. See Also

  • "Search Google by Link Graph" [Hack #91]

  • "Create a New Interface for Amazon.com" [Hack #92]



PHP Hacks
PHP Hacks: Tips & Tools For Creating Dynamic Websites
ISBN: 0596101392
EAN: 2147483647
Year: 2006
Pages: 163

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net