I've been trying to put yahoo weather on my site thru a feed (rss)
With NO luck..
Can someone be kind enough to post step-by-step instructions using dreamweaver CS3?
THANKS
I expect ALOT of rep for this..
Step 1
Copy this code, and save it as weather.php
Step 2Code:<? class RSSParser { var $insideitem = false; var $tag = ""; var $title = ""; var $description = ""; var $link = ""; function startElement($parser, $tagName, $attrs) { if ($this->insideitem) { $this->tag = $tagName; } elseif ($tagName == "ITEM") { $this->insideitem = true; } } function endElement($parser, $tagName) { if ($tagName == "ITEM") { $title = $this->title; $desc = $this->description; echo "<div><strong>" . $title . "</strong></div>"; echo "<div>" . $desc . "</div>"; $this->insideitem = false; } } function characterData($parser, $data) { if ($this->insideitem) { switch ($this->tag) { case "TITLE": $this->title .= $data; break; case "DESCRIPTION": $this->description .= $data; break; case "LINK": $this->link .= $data; break; } } } } $location = $_POST['location']; if(!isset($location)) { ?> <div><strong>Enter in your zipcode below</strong></div> <form action="weather.php" method="post"> <div> Zipcode: <input type="text" name="location" size="10" /> <input type="submit" value="GO" /> </div> </form> <? } else { $url = "http://weather.yahooapis.com/forecastrss?p=" . $location; $xml_parser = xml_parser_create(); $rss_parser = new RSSParser(); xml_set_object($xml_parser,&$rss_parser); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); $fp = fopen("$url","r") or die("Error reading RSS data."); while ($data = fread($fp, 4096)) xml_parse($xml_parser, $data, feof($fp)) or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); fclose($fp); xml_parser_free($xml_parser); } ?>
Oh wait, thats it.
This example: www.developmy.mobi/weather.php
Last edited by developmy.mobi; 12-24-2008 at 02:19 PM.
Only want to have a static zipcode?
Use this code:
This example: www.developmy.mobi/weather2.phpCode:<? class RSSParser { var $insideitem = false; var $tag = ""; var $title = ""; var $description = ""; var $link = ""; function startElement($parser, $tagName, $attrs) { if ($this->insideitem) { $this->tag = $tagName; } elseif ($tagName == "ITEM") { $this->insideitem = true; } } function endElement($parser, $tagName) { if ($tagName == "ITEM") { $title = $this->title; $desc = $this->description; echo "<div><strong>" . $title . "</strong></div>"; echo "<div>" . $desc . "</div>"; $this->insideitem = false; } } function characterData($parser, $data) { if ($this->insideitem) { switch ($this->tag) { case "TITLE": $this->title .= $data; break; case "DESCRIPTION": $this->description .= $data; break; case "LINK": $this->link .= $data; break; } } } } $location = "01301"; $url = "http://weather.yahooapis.com/forecastrss?p=" . $location; $xml_parser = xml_parser_create(); $rss_parser = new RSSParser(); xml_set_object($xml_parser,&$rss_parser); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); $fp = fopen("$url","r") or die("Error reading RSS data."); while ($data = fread($fp, 4096)) xml_parse($xml_parser, $data, feof($fp)) or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); fclose($fp); xml_parser_free($xml_parser); ?>
Mods, make this a sticky?
In the second example, just change the $url variable to :
http://www.rss.noaa.gov/noaa-gov.xml
for example.
It doesn't look like they have forecasts for RSS.
Last edited by developmy.mobi; 12-24-2008 at 03:19 PM.
See: www.londonweather.mobi
If you go to http://developer.yahoo.com
And go to the weather section, they have a section that allows you to use international location codes.
Hope this helps,
Dave
Hint: Just one word (or two): metar files!
PS: Now be a good boy and do your homework... and youll gonna have weather in your site!!!![]()
i done this with weather . com API joining with a ip2country database. users don't need to choose location manually now.
Bookmarks