• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 412
  • Last Modified:

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?
0
kfranck
Asked:
kfranck
  • 7
  • 6
  • 2
  • +1
1 Solution
 
Ray PaseurCommented:
There are lots of "weather widgets" -- google the term.

0
 
MurfurFull Stack DeveloperCommented:
0
 
MurfurFull Stack DeveloperCommented:
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ray PaseurCommented:
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

0
 
kfranckAuthor Commented:
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
0
 
leakim971PluritechnicianCommented:
0
 
Ray PaseurCommented:
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

0
 
kfranckAuthor Commented:
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

0
 
Ray PaseurCommented:
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

0
 
Ray PaseurCommented:
In this example, the entire script is generated via PHP.  It allows for separation of the logic and the presentation.  We do all the same things we did before but instead of echoing the span string, we assign it to a variable on line 26.  Lines 30-42 create our HTML string.  Take careful note of line 39 - that is where we use the variable we created on line 26.  At the very end, line 45, we write the HTML to the browser and our web page is complete.

There are a lot of reasons to separate the logic and presentation, most notably because it is much easier to debug your scripts this way, and there are things about PHP sessions and error handling that work better if these things are separated.

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"];

// THE FINISHED HTML TAG WE WANT
$w = "<span id=\"weather\">$s and $f&#176; at $d in $c</span>" . PHP_EOL; 


// THE HTML STRING IN HEREDOC NOTATION
$html = <<<ENDHTML
<!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>
<body>
$w
</body>
</html>
ENDHTML;

// WRITE THE STRING TO THE BROWSER
echo $html;

Open in new window

0
 
kfranckAuthor Commented:
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

0
 
Ray PaseurCommented:
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.  
0
 
kfranckAuthor Commented:
1 and 1.com. I will contact them and let you know what happens. Thanks.
0
 
kfranckAuthor Commented:
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.
0
 
kfranckAuthor Commented:
The Expert was very patient, informative and helpful in getting me the information I need.

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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 7
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now