TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

Members
  • Total Members: 3,966
  • Latest: safir45
Stats
  • Total Posts: 195,982
  • Total Topics: 21,320
  • Online today: 1,031
  • Online ever: 8,223 (February 19, 2025, 04:35:35 AM)
Users Online
  • Users: 0
  • Guests: 787
  • Total: 787

Weather on your Site

Started by texasflats, February 22, 2007, 01:02:36 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

texasflats

My first contribution is "Weather on your site". Not really "advanced" but requires a table to be manually inserted.

Edit: I should add that this is more than a block on your site. Users can get an actual weather forecast from within your site.

Demo is a left block on my site.

1. Sign up for Weather.com's free XML service at http://www.weather.com/services/xmloap.html

2. Download the weather.com SDK and upload all image files to your web server, maintaining the zip file structure. This script assumes you have a folder named "images" on your root and a subfolder named "weather" within it. You should copy the weather.com image folders (logos, 32x32, 64x64, and 128x128) to your weather folder. EG. /images/weather/32x32 - 64x64 - 128x128 (you get the picture)

3. Create a table in your database named weather_xml with the following structure:

CREATE TABLE `weather_xml` (
  `xml_url` varchar(150) NOT NULL default '',
  `xml_data` text NOT NULL,
  `last_updated` datetime NOT NULL default '0000-00-00 00:00:00',
  KEY `xml_url` (`xml_url`)
) TYPE=MyISAM;


Create a php file named weather_xml.php
<?
/*
+++++++++++++++++++++++++++++++
+
+ Weather_xml.php written by
+ admin@notonebit.com
+
+ Last changed: December 21, 2004
+ Version 1.3
+
* Converted to TP by J.Dean
* at  www.texasflats.net
+++++++++++++++++++++++++++++++
*/

// Sign up for Weather.com's free XML service at http://www.weather.com/services/xmloap.html

// Setup database connection
$db_host = "YOUR HOST";
$db_user = "DB USER";
$db_pwd = "DB PASSWORD";
$db_name = "DB NAME";
$connection = mysql_connect($db_host, $db_user, $db_pwd) or die("Could not connect");
mysql_select_db($db_name) or die("Could not select database");

// Set Local variables
$partner_ID = "IN EMAIL";
$license_key = "IN EMAIL";
$location = urlencode($_REQUEST['loc']); //search on location (city or zip)
$loc_id = $_REQUEST['id']; // specific town id
$length = $_REQUEST['length']; // Forecast length
$image_size = "64x64"; // 32x32, 64x64, or 128x128 - size of daily weather images

if (!($length >= 1 And $length <= 10)) $length = 10;

// First URL for searching, second for detail.
$search_url = "http://xoap.weather.com/search/search?where=$location";
$forecast_url = "http://xoap.weather.com/weather/local/$loc_id?cc=*&dayf=$length&prod=xoap&par=$partner_ID&key=$license_key";

/*
cc Current Conditions OPTIONAL VALUE IGNORED
dayf Multi-day forecast information for some or all forecast elements OPTIONAL VALUE = [ 1..10 ]
link Links for weather.com pages OPTIONAL VALUE = xoap
par Application developers Id assigned to you REQUIRED VALUE = {partner id}
prod The XML Server product code REQUIRED VALUE = xoap
key The license key assigned to you REQUIRED VALUE = {license key}
unit Set of units. Standard or Metric OPTIONAL VALUES = [ s | m ] DEFAULT = s
*/

function html_header() // Write html header to screen
{
$header = <<< end_of_output
<html>
<head>
<title></title>
<style>
<!--
body, p      { font-size: 10pt; font-family: Verdana }
td           { font-size: 10pt; font-family: Verdana }
-->
</style>
</head>
<body bgcolor=#E9E4C9>
end_of_output;
echo $header;
}

if ($location) // Determine URL to use. If location is passed, we're searching for a city or zip. Elese we're retrieving a forecast.
{
$url = $search_url;
}
else
{
$url = $forecast_url;
}

if ($location Or $loc_id) // If city, zip, or weather.com city id passed, do XML query. $loc_id is a weather.com city code, $location is user entered city or zip
{

/*
query db for md5 of url
if doesn't exist, insert into db
if exists, check date, if under X hours use db content
if older then X hours, pull from weather.com and update db

to delete old data: when querying, delete all records older than X hours
*/

$datetime = date("Y-m-d h:i:s");
$xml_url = md5($url);
$interval = 12; // Hours to keep data in db before being considered old
$expires = $interval*60*60;
$expiredatetime = date("Y-m-d H:i:s", time() - $expires);

// Delete expired records
$query = "DELETE FROM weather_xml WHERE last_updated < '$expiredatetime'";
$result = mysql_query($query) or die('Invalid query: ' . mysql_error());

$query = "SELECT * FROM weather_xml WHERE xml_url = '$xml_url'";
$result = mysql_query($query) or die('Invalid query: ' . mysql_error());
$row = mysql_fetch_array($result);
$time_diff = strtotime($datetime) - strtotime($row['last_updated']);

if (mysql_num_rows($result) < 1) // Data not in table - Add.
{
// Get XML Query Results from Weather.com
$fp = fopen($url,"r");
while (!feof ($fp))
$xml .= fgets($fp, 4096);
fclose ($fp);

// Fire up the built-in XML parser
$parser = xml_parser_create(  );
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);

// Set tag names and values
xml_parse_into_struct($parser,$xml,$values,$index);

// Close down XML parser
xml_parser_free($parser);

$xml = str_replace("'","",$xml); // Added to handle cities with apostrophies in the name like T'Bilisi, Georgia

if ($loc_id) // Only inserts forecast feed, not search results feed, into db
{
$query = "INSERT INTO weather_xml VALUES ('$xml_url', '$xml', '$datetime')";
$result = mysql_query($query) or die('Invalid query: ' . mysql_error());
}

}
else // Data in table, and it is within expiration period - do not load from weather.com and use cached copy instead.
{
$xml = $row['xml_data'];

// Fire up the built-in XML parser
$parser = xml_parser_create(  );
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);

// Set tag names and values
xml_parse_into_struct($parser,$xml,$values,$index);

// Close down XML parser
xml_parser_free($parser);
}

// Debugging output
//echo "<pre>";
//print_r($xml);
//print_r($index);
//print_r($values);
//echo "</pre>";
}

if ($loc_id) // Location code selected - Display detail info. A specific city has been selected from the drop down menu. Get forecast.
{
$city = htmlspecialchars($values[$index[dnam][0]][value]);
$unit_temp = $values[$index[ut][0]][value];
$unit_speed = $values[$index[us][0]][value];
$unit_precip = $values[$index[up][0]][value];
$unit_pressure = $values[$index[ur][0]][value];
$sunrise = $values[$index[sunr][0]][value];
$sunset = $values[$index[suns][0]][value];
$timezone = $values[$index[tzone][0]][value];
$last_update = $values[$index[lsup][0]][value];
$curr_temp = $values[$index[tmp][0]][value];
$curr_flik = $values[$index[flik][0]][value];
$curr_text = $values[$index[t][0]][value];
$curr_icon = $values[$index[icon][0]][value];
$counter = 0;
$row_counter = 2;

html_header(); // Output header

echo "<p align=center><img src=/images/cl.gif>";
echo "<b><p align=center><font size=3>Weather report for $city</font></b><br /><font size=1>(Last updated $last_update).</font><br />\n";
echo "<p align=center><table cellpadding=10><tr><td valign=top><img border=\"1\" src=\"/images/weather/64x64/$curr_icon.png\" alt=\"$curr_text\"></td><td>";
echo "<font size=3>Currently: <b>$curr_temp˚ $unit_temp</b></font><br />\n";
echo "Feels Like: $curr_flik˚ $unit_temp<br />Current conditions: $curr_text<br />\n";
echo "Sunrise: $sunrise.<br />Sunset: $sunset.<br />\n";
echo "</td></tr></table></p>";

$color_bar = <<< end_of_output
<table border="1" align="center" cellspacing="0" cellpadding="4" style="border-collapse: collapse" bordercolor="#000000">
  <tr>
    <td width="9%" bgcolor="#CC99CC" align="center">
    <font face="Verdana" size="1">&lt;10</font></td>
    <td width="9%" bgcolor="#9966FF" align="center">
    <font face="Verdana" size="1">10's</font></td>
    <td width="9%" bgcolor="#3399FF" align="center">
    <font face="Verdana" size="1">20's</font></td>
    <td width="9%" bgcolor="#99CCFF" align="center">
    <font face="Verdana" size="1">30's</font></td>
    <td width="9%" bgcolor="#66CC66" align="center">
    <font face="Verdana" size="1">40's</font></td>
    <td width="9%" bgcolor="#FFFF99" align="center">
    <font face="Verdana" size="1">50's</font></td>
    <td width="9%" bgcolor="#FFCC33" align="center">
    <font face="Verdana" size="1">60's</font></td>
    <td width="9%" bgcolor="#FF9933" align="center">
    <font face="Verdana" size="1">70's</font></td>
    <td width="9%" bgcolor="#FF6600" align="center">
    <font face="Verdana" size="1">80's</font></td>
    <td width="9%" bgcolor="#FF0000" align="center">
    <font face="Verdana" size="1">90's</font></td>
    <td width="10%" bgcolor="#990000" align="center">
    <font face="Verdana" size="1" color="#FFFFFF">100+</font></td>
  </tr>
</table>
end_of_output;
echo $color_bar;

echo "<table border=\"0\" align=\"center\"cellpadding=\"4\" cellspacing=\"1\" bgcolor=\"#C0C0C0\"><tr><th>Date</th><th>High/Low</th><th>Day</th><th>Night</th></tr>";
foreach ($index[day] as $day)
{
if ($values[$day][attributes][t] != "")
{
// ($row_counter%2==0) ? $row_color =  "#CCE6FF": $row_color = "#CCCDFF";
$row_color = "#EEEECC";
$img_day = ($counter + 1) * 2;
$img_night = (($counter + 1) * 2) + 1;

$day_text = (($counter + 1) * 3) + $counter + 2;
$day_wind = ((($counter + 1) * 3) + $counter) + 1;
$day_windspeed = (($counter + 1) * 2) - 1;
$day_windgust = (($counter + 1) * 2) - 1;
$day_humidity = (($counter + 1) * 2) - 1;
$day_precip = $counter * 2;

$night_text = ((($counter + 1) * 3) + $counter) + 4;
$night_wind = ((($counter + 1) * 3) + $counter) + 3;
$night_windspeed = ($counter + 1) * 2;
$night_windgust = ($counter + 1) * 2;
$night_humidity = ($counter + 1) * 2;
$night_precip = ($counter * 2) + 1;

if ($values[$index[hi][$counter]][value] >= 0) $heat_color = "#CC99CC";
if ($values[$index[hi][$counter]][value] >= 10) $heat_color = "#9966FF";
if ($values[$index[hi][$counter]][value] >= 20) $heat_color = "#3399FF";
if ($values[$index[hi][$counter]][value] >= 30) $heat_color = "#99CCFF";
if ($values[$index[hi][$counter]][value] >= 40) $heat_color = "#66CC66";
if ($values[$index[hi][$counter]][value] >= 50) $heat_color = "#FFFF99";
if ($values[$index[hi][$counter]][value] >= 60) $heat_color = "#FFCC33";
if ($values[$index[hi][$counter]][value] >= 70) $heat_color = "#FF9933";
if ($values[$index[hi][$counter]][value] >= 80) $heat_color = "#FF6600";
if ($values[$index[hi][$counter]][value] >= 90) $heat_color = "#FF0000";
if ($values[$index[hi][$counter]][value] >= 100) $heat_color = "#990000";
if ($values[$index[hi][$counter]][value] == "N/A") $heat_color = "#EEEECC";

echo "<tr><td bgcolor=\"$heat_color\"><b>" . $values[$day][attributes][t] . ", " . $values[$day][attributes][dt] . "</b></td>";
echo "<td bgcolor=\"$row_color\"><b>Hi: " . $values[$index[hi][$counter]][value] . "˚ $unit_temp\n";
echo "<hr noshade height=\"1\">Lo: " . $values[$index[low][$counter]][value] . "˚ $unit_temp</b></td>\n";

echo "<td bgcolor=\"$row_color\" nowrap><table width=\"100%\" cellpadding=\"2\" cellspacing=\"0\"><tr><td><b>\n";
echo $values[$index[t][$day_wind]][value] . "</b><br /> ";
echo "<font size=1>Sunrise: " . $values[$index[sunr][$counter]][value] . "<br />";
echo "Wind: " . $values[$index[t][$day_text]][value] . " " . $values[$index[s][$day_windspeed]][value] . " $unit_speed";
echo "<br />Humidity: " . $values[$index[hmid][$day_humidity]][value] . "%<br />Precip: " . $values[$index[ppcp][$day_precip]][value] . "%</font></td>";
echo "<td bgcolor=\"$row_color\" align=\"right\"><img border=\"1\" src=\"/images/weather/$image_size/" . $values[$index[icon][$img_day]][value] . ".png\" alt=\"" . $values[$index[t][$day_text]][value] . "\"></td></tr></table></td>";

echo "<td bgcolor=\"$row_color\" nowrap><table width=\"100%\" cellpadding=\"2\" cellspacing=\"0\"><tr><td><b>\n";
echo $values[$index[t][$night_wind]][value] . "</b><br />";
echo "<font size=1>Sunset: " . $values[$index[suns][$counter]][value] . "<br />";
echo "Wind: " . $values[$index[t][$night_text]][value] . " " . $values[$index[s][$night_windspeed]][value] . " $unit_speed";
echo "<br />Humidity: " . $values[$index[hmid][$night_humidity]][value] . "%<br />Precip: " . $values[$index[ppcp][$night_precip]][value] . "%</font></td>\n";
echo "<td bgcolor=\"$row_color\" align=\"right\"><img border=\"1\" src=\"/images/weather/$image_size/" . $values[$index[icon][$img_night]][value] .".png\" alt=\"" . $values[$index[t][$night_text]][value] . "\"></tr></table></td>";
echo "</tr>";

$counter++;
$row_counter++;
}
}
echo "</table>";
}

if ($location And is_array($index[loc])) // A city name has been entered and data returned from weather.com, draw drop down menu of matches
{

if (count($index[loc]) == 1) // If just one match returned, send to detail screen - no need to draw option box for one option.
{
$location_code = $values[$index[loc][0]][attributes][id];
header("Location: weather_xml.php?id=$location_code&length=10"); // Nees html_header because of this redirect.
exit();
}

html_header();

echo "<p align=center><img src=/images/cl.gif><br>";
echo "<form action=\"weather_xml.php\" method=\"POST\">";
echo "<p align=\"center\">Select a city: <select size=\"1\" name=\"id\">\n";
// Loop through the XML, setting values
foreach ($index[loc] as $key=>$val)
{
echo "<option value=\"";
echo $values[$val][attributes][id]; // City code
echo "\">";
echo $values[$val][value]; // City name
echo "</option>\n";
}
echo '</select>';
?>

Length of forecast
<select size="1" name="length">
<option selected>10</option>
<option>9</option>
<option>8</option>
<option>7</option>
<option>6</option>
<option>5</option>
<option>4</option>
<option>3</option>
<option>2</option>
<option>1</option>
</select>
<input type="submit" value="Get Weather">
</form>
<?

}
elseif ($location) // City or zip entered but no match returned from weather.com
{
html_header();
echo "<p align=center><img src=/images/cl.gif>";
echo "<p align=center><b>No city found. Please enter another city or zip code.</b><br />\n";
}

if (empty($location) And empty($loc_id))
{
html_header();
}
?>

<form action="weather_xml.php" method="POST">
<p align=center>Enter a city or zip code: <input type="text" name="loc" size="20"><input type="submit" value="Search"></p>
</form>
<p align=center>Weather data provided by <a href="http://www.weather.com/?prod=xoap&par=<?=$partner_ID?>">weather.com</p>

<p align=center><a href="http://www.texasflats.net">Converted to TP by J.Dean</p>

</body>

</html>


4. Configure the following variables in weather_xml.php and upload to your web servers root directory:

$db_host = "";      Your database host (e.g. localhost)
$db_user = "";      Your database username
$db_pwd = "";      Your database password
$db_name = "";      Your database name
$partner_ID = "";   Your weather.com partner ID you received when signing up for their XML service
$license_key = "";   Your weather.com license key
$image_size = "64x64";   Select the size of the forecast images to use

5. Modify the image codes in weather_xml.php as desired.

6. Create a scriptbox with the following code:

<form action="weather_xml.php" method="POST" target="_blank">
<p>City or zip code: <input type="text" name="loc" size="20"><input type="submit" value="Search"></p>
</form>


Thats it, if someone can make it open in a popup window, or within an article, I'd be very grateful! :)

RoarinRow

Great contribution!  I like weather blocks.  I use this one right now.  It's from weather.com.

<center><table cellpadding="0" cellspacing="0">
<tr>
    <td align="center" width="150">
        <a href="http://www.weather.com?par=internal&code=1025899396&site=logo&promo=english" target="_blank"><img border="0" src="http://image.weather.com/pics/oap/slogo.gif"></a>
    </td>
</tr>
<tr>
    <td align="center" width="150">
        <form target="_blank" method="post" action="http://www.weather.com/cgi-bin/uncgi/zip_city_search.cgi?par=internal&code=1025899396&site=logo&promo=english">
        <table cellpadding="0" cellspacing="0">
            <tr>
                <td valign="center" width="150" colspan="2">
                    <font face="Arial, Helvetica, Chicago, Sans Serif" color="#004371" size="2">  Enter city or US Zip </font>
                </td>
            </tr>
            <tr>
                <td width="40" align="left" valign="bottom">
                    <input name="destination" type="text" size="8" value="">
                </td>
                <td width="20" align="left" valign="bottom">
                    <input type="image" src="http://image.weather.com/pics/button_go.gif" width="26" height="23" border="0" alt="go!" value="Go City">
                </td>
            </tr>
        </table>
        </form>
    </td>
</tr>
</table></center>

SMF 2.0 RC3
TP 1.0 beta 5-1
Wordpress 3.0

brianjw

I have joined the weather except they havent sent me that email :(
I entered the correct email too :-\

RoarinRow

Quote from: brianjw on February 22, 2007, 11:31:46 PM
I have joined the weather except they havent sent me that email :(
I entered the correct email too :-\

no sure how long i had to wait   :o

SMF 2.0 RC3
TP 1.0 beta 5-1
Wordpress 3.0

brianjw


RoarinRow


SMF 2.0 RC3
TP 1.0 beta 5-1
Wordpress 3.0


texasflats

Mine was almost instant, not more than 15 minutes. You might try again?

brianjw


This website is proudly hosted on Crocweb Cloud Website Hosting.