/ Published in: PHP
With this customizable code you can add google weather showing current temps, conditions, and forecast for any location to your website. User prompts are included to allow the user to change the default location easily then hit the "go" button. Auto refresh of weather conditions at the interval you desire.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<? $Fname = $_GET["Fname"]; { $Fname='New York, NY'; } { $Fname='New York, NY'; } $information = $xml->xpath("/xml_api_reply/weather/forecast_information"); $current = $xml->xpath("/xml_api_reply/weather/current_conditions"); $forecast_list = $xml->xpath("/xml_api_reply/weather/forecast_conditions"); ?> <html> <head> <title>Google Weather API</title> <style type="text/css"> body { background-color: #FFD700; color: #000000; } </style> <script language="JavaScript" type="text/javascript"> //////////////////////////////////////////////////////////////////////////// // CONFIGURATION STARTS HERE // Configure refresh interval (in seconds) var refreshinterval=300 // Shall the coundown be displayed inside your status bar? Say "yes" or "no" below: var displaycountdown="no" // CONFIGURATION ENDS HERE //////////////////////////////////////////////////////////////////////////// // Do not edit the code below var starttime var nowtime var reloadseconds=0 var secondssinceloaded=0 function starttime() { starttime=new Date() starttime=starttime.getTime() countdown() } function countdown() { nowtime= new Date() nowtime=nowtime.getTime() secondssinceloaded=(nowtime-starttime)/1000 reloadseconds=Math.round(refreshinterval-secondssinceloaded) if (refreshinterval>=secondssinceloaded) { var timer=setTimeout("countdown()",1000) if (displaycountdown=="yes") { window.status="Page refreshing in "+reloadseconds+ " seconds" } } else { clearTimeout(timer) window.location=window.location; } } starttime() </script> </head> <body> <form method="get" action="<?php echo $PHP_SELF;?>"> <input type="text" style="position:absolute;left:6px;top:4px;width:140px;height:21px;border:1px #000000 solid;font-family:Arial;font-weight:bold;font-size:12px;z-index:0" name="Fname" value="<?php echo $information[0]->city['data'] ?>" maxlength="30"> <input type="submit" id="Button1" name="Go" value="Go" style="position:absolute;left:146px;top:3px;width:30px;height:23px;font-family:Arial;font-size:13px;z-index:1"> </form><br /> <div id="wb_Image1" style="margin:0;padding:0;position:absolute;left:5px;top:28px;width:44px;height:44px;text-align:left;z-index:0;"> <img src="<?= 'http://www.google.com' . $current[0]->icon['data']?>" id="Image1" alt="" border="0" style="width:44px;height:44px;"></div> <div id="wb_Text2" style="margin:0;padding:0;position:absolute;left:5px;top:74px;width:193px;height:16px;text-align:left;z-index:2;"> <font style="font-size:13px" color="#000000" face="Arial"><?= $current[0]->condition['data'] ?></font></div> <div id="wb_Text3" style="margin:0;padding:0;position:absolute;left:5px;top:92px;width:193px;height:16px;text-align:left;z-index:3;"> <font style="font-size:13px" color="#000000" face="Arial"><?= $current[0]->humidity['data'] ?></font></div> <div id="wb_Text4" style="margin:0;padding:0;position:absolute;left:5px;top:110px;width:193px;height:16px;text-align:left;z-index:4;"> <font style="font-size:13px" color="#000000" face="Arial"><?= $current[0]->wind_condition['data'] ?></font></div> <div id="wb_Text5" style="margin:0;padding:0;position:absolute;left:56px;top:34px;width:131px;height:29px;text-align:left;z-index:5;"> <font style="font-size:24px" color="#000000" face="Arial"><b><?= $current[0]->temp_f['data'] ?>° F</b></font></div> <div id="wb_Text6" style="margin:0;padding:0;position:absolute;left:12px;top:132px;width:30px;height:16px;text-align:left;z-index:6;"> <font style="font-size:13px" color="#000000" face="Arial"><?= $forecast_list[0]->day_of_week['data'] ?></font></div> <div id="wb_Text7" style="margin:0;padding:0;position:absolute;left:55px;top:132px;width:30px;height:16px;text-align:left;z-index:7;"> <font style="font-size:13px" color="#000000" face="Arial"><?= $forecast_list[1]->day_of_week['data'] ?></font></div> <div id="wb_Text8" style="margin:0;padding:0;position:absolute;left:97px;top:132px;width:30px;height:16px;text-align:left;z-index:8;"> <font style="font-size:13px" color="#000000" face="Arial"><?= $forecast_list[2]->day_of_week['data'] ?></font></div> <div id="wb_Text9" style="margin:0;padding:0;position:absolute;left:139px;top:132px;width:30px;height:16px;text-align:left;z-index:9;"> <font style="font-size:13px" color="#000000" face="Arial"><?= $forecast_list[3]->day_of_week['data'] ?></font></div> <div id="wb_Image2" style="margin:0;padding:0;position:absolute;left:10px;top:152px;width:30px;height:29px;text-align:left;z-index:10;"> <img src="<?= 'http://www.google.com' . $forecast_list[0]->icon['data']?>" id="Image2" alt="" border="0" style="width:30px;height:29px;"></div> <div id="wb_Image3" style="margin:0;padding:0;position:absolute;left:51px;top:152px;width:30px;height:29px;text-align:left;z-index:11;"> <img src="<?= 'http://www.google.com' . $forecast_list[1]->icon['data']?>" id="Image3" alt="" border="0" style="width:30px;height:29px;"></div> <div id="wb_Image4" style="margin:0;padding:0;position:absolute;left:92px;top:152px;width:30px;height:29px;text-align:left;z-index:12;"> <img src="<?= 'http://www.google.com' . $forecast_list[2]->icon['data']?>" id="Image4" alt="" border="0" style="width:30px;height:29px;"></div> <div id="wb_Image5" style="margin:0;padding:0;position:absolute;left:134px;top:152px;width:30px;height:29px;text-align:left;z-index:13;"> <img src="<?= 'http://www.google.com' . $forecast_list[3]->icon['data']?>" id="Image5" alt="" border="0" style="width:30px;height:29px;"></div> <div id="wb_Text10" style="margin:0;padding:0;position:absolute;left:8px;top:182px;width:34px;height:14px;text-align:left;z-index:14;"> <font style="font-size:11px" color="#000000" face="Arial"><?= $forecast_list[0]->high['data']?>°/<?= $forecast_list[0]->low['data'] ?>°</font></div> <div id="wb_Text11" style="margin:0;padding:0;position:absolute;left:49px;top:182px;width:34px;height:14px;text-align:left;z-index:15;"> <font style="font-size:11px" color="#000000" face="Arial"><?= $forecast_list[1]->high['data']?>°/<?= $forecast_list[1]->low['data'] ?>°</font></div> <div id="wb_Text12" style="margin:0;padding:0;position:absolute;left:90px;top:182px;width:34px;height:14px;text-align:left;z-index:16;"> <font style="font-size:11px" color="#000000" face="Arial"><?= $forecast_list[2]->high['data']?>°/<?= $forecast_list[2]->low['data'] ?>°</font></div> <div id="wb_Text13" style="margin:0;padding:0;position:absolute;left:132px;top:182px;width:34px;height:14px;text-align:left;z-index:17;"> <font style="font-size:11px" color="#000000" face="Arial"><?= $forecast_list[3]->high['data']?>°/<?= $forecast_list[3]->low['data'] ?>°</font></div> <hr id="Line1" style="color:#00008B;background-color:#00008B;border:0px;margin:0;padding:0;position:absolute;left:0px;top:128px;width:180px;height:5px;z-index:18"> </body> </html>