Link to home
Start Free TrialLog in
Avatar of kfranck
kfranckFlag for United States of America

asked on

How can I code weather for my site?

I want to add current temperature (Fahrenheit) and weather condition to my Web site. I know NOAA weather code for my area (Toledo, OH) is KTDZ. I want to know how to code, using ASP, PHP or another script. I want small, with or without an icon, but it needs to run in a small space.

Suggestions?
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

There are lots of "weather widgets" -- google the term.

Avatar of Justin Pilditch
Actually, even better and a lot less work is a Google weather API - returns XML that you can parse:

http://www.google.com/ig/api?weather=toledo,oh
The iGoogle API seems to work nicely!

http://www.laprbass.com/RAY_temp_kfranck.php
Outputs something like:
Cloudy and 55° at 7:31am in Toledo, OH

HTH, ~Ray
<?php // RAY_temp_kfranck.php
error_reporting(E_ALL);


// DEMONSTRATE A WEATHER WIDGET USING IGOOGLE API


date_default_timezone_set('America/New_York');
$url = 'http://www.google.com/ig/api?weather=toledo,oh';
$xml = file_get_contents($url);
$obj = SimpleXML_Load_String($xml);

// ACTIVATE THIS TO SEE THE OBJECT
// echo "<pre>";
// var_dump($obj);
// echo "</pre>";

// EXTRACT AND FORMAT THE DATA
$c = $obj->weather->forecast_information->city["data"];
$d = $obj->weather->forecast_information->current_date_time["data"];
$d = date('g:ia', strtotime($d));
$s = $obj->weather->current_conditions->condition["data"];
$f = $obj->weather->current_conditions->temp_f["data"];

echo "<span>$s and $f&#176; at $d in $c</span>" . PHP_EOL;

Open in new window

Avatar of kfranck

ASKER

Ray:
This is exactly what I need. Do I take the code you sent and place on a PHP server with a php extensiohn?

I work with ASP and am just exploring APIs.
Thanks,
Kfranck
Yes, you can do it that way.  You can call this script as if it is an API and will return the <span> string as if it is browser output.  You can add a class or id to the span tag in line 25 if you want to style it.  Maybe something like the code snippet here (note the escapes needed for the double quotes).
echo "<span id=\"weather\">$s and $f&#176; at $d in $c</span>" . PHP_EOL;

Open in new window

Avatar of kfranck

ASKER

Thanks. Below is the code. Obviously, I have done something wrong because I am getting an error.

Parse error: syntax error, unexpected '<' in /homepages/36/d261004055/htdocs/weatherapi.php on line 35

Suggestions?



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>

<?php // RAY_temp_kfranck.php
error_reporting(E_ALL);


// DEMONSTRATE A WEATHER WIDGET USING IGOOGLE API


date_default_timezone_set('America/New_York');
$url = 'http://www.google.com/ig/api?weather=toledo,oh';
$xml = file_get_contents($url);
$obj = SimpleXML_Load_String($xml);

// ACTIVATE THIS TO SEE THE OBJECT
// echo "<pre>";
// var_dump($obj);
// echo "</pre>";

// EXTRACT AND FORMAT THE DATA
$c = $obj->weather->forecast_information->city["data"];
$d = $obj->weather->forecast_information->current_date_time["data"];
$d = date('g:ia', strtotime($d));
$s = $obj->weather->current_conditions->condition["data"];
$f = $obj->weather->current_conditions->temp_f["data"];

echo "<span id=\"weather\">$s and $f&#176; at $d in $c</span>" . PHP_EOL; 

<body>

</body>

</html>

Open in new window

Right, you have to close off PHP and drop back into HTML.

Make this change at line 34.  But that said, there is a better way to do this thing.  I'll show you in a moment.
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kfranck

ASKER

Ray:

Please check out the error code below.
Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in /homepages/36/d261004055/htdocs/weatherapi.php on line 10

Warning: file_get_contents(http://www.google.com/ig/api?weather=toledo,oh) [function.file-get-contents]: failed to open stream: no suitable wrapper could be found in /homepages/36/d261004055/htdocs/weatherapi.php on line 10

Notice: Trying to get property of non-object in /homepages/36/d261004055/htdocs/weatherapi.php on line 19

Notice: Trying to get property of non-object in /homepages/36/d261004055/htdocs/weatherapi.php on line 19

Notice: Trying to get property of non-object in /homepages/36/d261004055/htdocs/weatherapi.php on line 19

Notice: Trying to get property of non-object in /homepages/36/d261004055/htdocs/weatherapi.php on line 20

Notice: Trying to get property of non-object in /homepages/36/d261004055/htdocs/weatherapi.php on line 20

Notice: Trying to get property of non-object in /homepages/36/d261004055/htdocs/weatherapi.php on line 20

Notice: Trying to get property of non-object in /homepages/36/d261004055/htdocs/weatherapi.php on line 22

Notice: Trying to get property of non-object in /homepages/36/d261004055/htdocs/weatherapi.php on line 22

Notice: Trying to get property of non-object in /homepages/36/d261004055/htdocs/weatherapi.php on line 22

Notice: Trying to get property of non-object in /homepages/36/d261004055/htdocs/weatherapi.php on line 23

Notice: Trying to get property of non-object in /homepages/36/d261004055/htdocs/weatherapi.php on line 23

Notice: Trying to get property of non-object in /homepages/36/d261004055/htdocs/weatherapi.php on line 23
and ° at 7:00pm in

Open in new window

Too bad about this: URL file-access is disabled in the server configuration

Who is your PHP hosting company?  Ask them to enable this feature.  
Avatar of kfranck

ASKER

1 and 1.com. I will contact them and let you know what happens. Thanks.
Avatar of kfranck

ASKER

Good news! I called 1 and 1.com and the adjustment was made. If you go to
toledobladeserver.com/weatherapi.php, you will get this response.

Cloudy and 60° at 10:28am in Toledo, OH.

Thanks so much for your help. So you get credit, I am going to open another post.
Avatar of kfranck

ASKER

The Expert was very patient, informative and helpful in getting me the information I need.

Much appreciated!
kfranck
Thanks for the points!  It's a great question, ~Ray