I have tried to get this code to work on my site. If I use it once it is fine, but a second instance of the same code (with variables renamed for new location) causes the hole page to fail. Each set of code works individually, but not on the same page.
Code: Select all
<?
/*
Author: Paul KH Kim
Email: paul.kim@onlinesolution.co.nz
Date: 21/02/2009
Script: Basic Weather Forecast using Google Weather API
Description: Get Google's weather XML file and parse it to HTML format
*/
function writeln($string)
{
echo "{$string}\r\n";
}
function convert($temp)
{
// Converting Fahrenheit To Celsius, vice versa
global $config;
$temperature = $temp;
if( strtoupper($config['base-temp-unit']) == 'F' && strtoupper($config['display-temp-unit']) == 'C' )
{
// Converting Fahrenheit To Celsius
$temperature = round((5/9)*($temp-32));
}
if( strtoupper($config['base-temp-unit']) == 'C' && strtoupper($config['display-temp-unit']) == 'F' )
{
// Converting Celsius to Fahrenheit
$temperature = round((9/5)*$temp+32);
}
return $temperature;
}
$url = "http://www.google.com";
$location = "dublin,ie"; // <city>,<country code>
$weather_url = "{$url}/ig/api?weather={$location}";
$config['base-temp-unit'] = 'F'; // F=Fahrenheit, C=Celsius
$config['display-temp-unit'] = 'C'; // F=Fahrenheit, C=Celsius
if( $xmlData = file_get_contents($weather_url) )
{
$xml = new SimpleXMLElement($xmlData);
$eol = "\r\n";
// Display basic information
writeln(" <p><b>Dublin</b>");
// Display current information
writeln("<div id='current' class='illustration left'>");
writeln(" <h3>Today</h3>");
writeln(" <p><img src='{$url}{$xml->weather->current_conditions->icon->attributes()}'><br/>");
writeln(" {$xml->weather->current_conditions->condition->attributes()}<br/>");
writeln(" Temp: {$xml->weather->current_conditions->temp_c->attributes()} C<br/>");
writeln(" {$xml->weather->current_conditions->humidity->attributes()}<br/>");
writeln(" {$xml->weather->current_conditions->wind_condition->attributes()}<br/>");
writeln(" </p></div>");
writeln(" <div style='clear:both'></div>");
foreach( $xml->weather->forecast_conditions as $i => $result )
{
// Display forecasts (next 4 days)
writeln("<div id='forecast_{$i}' class='weather'>");
writeln(" <h3>{$result->day_of_week->attributes()}</h3>");
writeln(" <p><img src='{$url}{$result->icon->attributes()}'><br/>");
writeln(" {$result->condition->attributes()}<br/>");
writeln(" Low: ".convert($result->low->attributes())." ".strtoupper($config['display-temp-unit'])."<br/>");
writeln(" Hi: ".convert($result->high->attributes())." ".strtoupper($config['display-temp-unit'])."<br/>");
writeln(" </p></div>");
}
}
?>Code: Select all
<?
/*
Author: Paul KH Kim
Email: paul.kim@onlinesolution.co.nz
Date: 21/02/2009
Script: Basic Weather Forecast using Google Weather API
Description: Get Google's weather XML file and parse it to HTML format
*/
function writeln($stringcork)
{
echo "{$stringcork}\r\n";
}
function convert($tempcork)
{
// Converting Fahrenheit To Celsius, vice versa
global $configcork;
$temperaturecork = $tempcork;
if( strtoupper($configcork['base-temp-unit']) == 'F' && strtoupper($configcork['display-temp-unit']) == 'C' )
{
// Converting Fahrenheit To Celsius
$temperaturecork = round((5/9)*($tempcork-32));
}
if( strtoupper($configcork['base-temp-unit']) == 'C' && strtoupper($configcork['display-temp-unit']) == 'F' )
{
// Converting Celsius to Fahrenheit
$temperaturecork = round((9/5)*$tempcork+32);
}
return $temperaturecork;
}
$urlcork = "http://www.google.com";
$locationcork = "cork,ie"; // <city>,<country code>
$weather_urlcork = "{$urlcork}/ig/api?weather={$locationcork}";
$configcork['base-temp-unit'] = 'F'; // F=Fahrenheit, C=Celsius
$configcork['display-temp-unit'] = 'C'; // F=Fahrenheit, C=Celsius
if( $xmlDatacork = file_get_contents($weather_urlcork) )
{
$xmlcork = new SimpleXMLElement($xmlDatacork);
$eolcork = "\r\n";
// Display basic information
writeln(" <p><b>Dublin</b>");
// Display current information
writeln("<div id='current' class='illustration left'>");
writeln(" <h3>Today</h3>");
writeln(" <p><img src='{$urlcork}{$xmlcork->weather->current_conditions->icon->attributes()}'><br/>");
writeln(" {$xmlcork->weather->current_conditions->condition->attributes()}<br/>");
writeln(" Temp: {$xmlcork->weather->current_conditions->temp_c->attributes()} C<br/>");
writeln(" {$xmlcork->weather->current_conditions->humidity->attributes()}<br/>");
writeln(" {$xmlcork->weather->current_conditions->wind_condition->attributes()}<br/>");
writeln(" </p></div>");
writeln(" <div style='clear:both'></div>");
foreach( $xmlcork->weather->forecast_conditions as $icork => $resultcork )
{
// Display forecasts (next 4 days)
writeln("<div id='forecast_{$icork}' class='weather'>");
writeln(" <h3>{$resultcork->day_of_week->attributes()}</h3>");
writeln(" <p><img src='{$urlcork}{$resultcork->icon->attributes()}'><br/>");
writeln(" {$resultcork->condition->attributes()}<br/>");
writeln(" Low: ".convert($resultcork->low->attributes())." ".strtoupper($configcork['display-temp-unit'])."<br/>");
writeln(" Hi: ".convert($resultcork->high->attributes())." ".strtoupper($configcork['display-temp-unit'])."<br/>");
writeln(" </p></div>");
}
}
?>You help is much appreciated - I've spend a few hours on this already!
MB