Forum rules

Please read our Guide to Making Ozzu Tutorials if you would like to submit your own tutorials.

TUTORIAL: Read weather from Yahoo! weather with PHP

Post May 12th, 2008, 4:34 am

Introduction


Many times you see websites that read weather from an RSS feed on another site, here I'll show you how to read weather onto your site from Yahoo! weather's RSS feed with php.

Where to get the XML from


To read the weather from Yahoo! weather, you send a request to Yahoo! that looks as follows:
Code: [ Download ] [ Select ]
http://weather.yahooapis.com/forecastrss?p=SFXX0044&u=c

where p is the location code ... In this tutorial I am using "Pretoria, South-Africa" (where I am from), and as you can see the location code is SFXX0044, I got this from http://weather.yahoo.com and I just browsed for my location, and the code is then in the URL ... alternatively, if you live in the USA you can just use the ZIP code where you are. Next, 'u' is the unit in which the weather must be displayed, I chose 'c' for Celsius, but you can also choose 'f' for Fahrenheit.

What the XML looks like


I have created this layut of what the XML response looks like that gets returned from Yahoo! weather:
Code: [ Download ] [ Select ]
<channel>
    <title>
    <link></link>
    <description></description>
    <language></language>
    <lastBuildDate></lastBuildDate>
    <ttl></ttl>
    <yweather:location city region country />
    <yweather:units temperature distance pressure speed />
    <yweather:wind chill direction speed />
    <yweather:atmosphere humidity visibility pressure rising />
    <yweather:astronomy sunrise sunset />
    <image>
        <title></title>
        <width></width>
        <height></height>
        <link></link>
        <url></url>
    </image>
    <item>
        <title></title>
        <geo:lat></geo:lat>
        <geo:long></geo:long>
        <link></link>
        <pubDate></pubDate>
        <yweather:condition text code temp date>
        <description></description>
        <yweather:forecast day date low high text code />
        <yweather:forecast day date low high text code />
        <guid Ispermalink></guid>
    </item>
</channel>
  1. <channel>
  2.     <title>
  3.     <link></link>
  4.     <description></description>
  5.     <language></language>
  6.     <lastBuildDate></lastBuildDate>
  7.     <ttl></ttl>
  8.     <yweather:location city region country />
  9.     <yweather:units temperature distance pressure speed />
  10.     <yweather:wind chill direction speed />
  11.     <yweather:atmosphere humidity visibility pressure rising />
  12.     <yweather:astronomy sunrise sunset />
  13.     <image>
  14.         <title></title>
  15.         <width></width>
  16.         <height></height>
  17.         <link></link>
  18.         <url></url>
  19.     </image>
  20.     <item>
  21.         <title></title>
  22.         <geo:lat></geo:lat>
  23.         <geo:long></geo:long>
  24.         <link></link>
  25.         <pubDate></pubDate>
  26.         <yweather:condition text code temp date>
  27.         <description></description>
  28.         <yweather:forecast day date low high text code />
  29.         <yweather:forecast day date low high text code />
  30.         <guid Ispermalink></guid>
  31.     </item>
  32. </channel>

as you can see, our "root" element is called "channel", and beneath that we have other elements aswell, the only element we eant to get from here is the following one:
    channel->item->description
so let's have a look at the code we are going to use:

The code


I am going to show the code with comments in between so you can see what I am doing
PHP Code: [ Download ] [ Select ]
<?php
//I am using the DOM(Document Object Model) library to read the entire XML document into memory first.
$doc = new DOMDocument();
$doc->load('http://weather.yahooapis.com/forecastrss?p=SFXX0044&u=c');
//now I get all elements inside this document with the following name "channel", this is the 'root'
$channel = $doc->getElementsByTagName("channel");
//now I go through each item withing $channel
foreach($channel as $chnl)
{
//I then find the 'item' element inside that loop
$item = $chnl->getElementsByTagName("item");
foreach($item as $itemgotten)
{
//now I search within '$item' for the element "description"
$describe = $itemgotten->getElementsByTagName("description");
//once I find it I create a variable named "$description" and assign the value of the Element to it
$description = $describe->item(0)->nodeValue;
//and display it on-screen
echo $description;
}
}
?>
  1. <?php
  2. //I am using the DOM(Document Object Model) library to read the entire XML document into memory first.
  3. $doc = new DOMDocument();
  4. $doc->load('http://weather.yahooapis.com/forecastrss?p=SFXX0044&u=c');
  5. //now I get all elements inside this document with the following name "channel", this is the 'root'
  6. $channel = $doc->getElementsByTagName("channel");
  7. //now I go through each item withing $channel
  8. foreach($channel as $chnl)
  9. {
  10. //I then find the 'item' element inside that loop
  11. $item = $chnl->getElementsByTagName("item");
  12. foreach($item as $itemgotten)
  13. {
  14. //now I search within '$item' for the element "description"
  15. $describe = $itemgotten->getElementsByTagName("description");
  16. //once I find it I create a variable named "$description" and assign the value of the Element to it
  17. $description = $describe->item(0)->nodeValue;
  18. //and display it on-screen
  19. echo $description;
  20. }
  21. }
  22. ?>

Output


I get something as follows:
    Image
    Current Conditions:
    Sunny, 20 C
    Forecast:
    Mon - Sunny. High: 25 Low: 9
    Tue - Sunny. High: 25 Low: 9
    Full Forecast at Yahoo! Weather
    (provided by The Weather Channel)
RewriteEngine On

RewriteRule ^(awesome|excellent|extraordinary)$ RT
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post May 12th, 2008, 4:34 am

  • ph81luc
  • Born
  • Born
  • No Avatar
  • Joined: Nov 17, 2008
  • Posts: 3
  • Status: Offline

Post November 17th, 2008, 10:29 am

Hi,

thanks for your code; I have applied it and receive the following error:
Catchable fatal error: Object of class DOMNodeList could not be converted to string in /home/a2163261/public_html/weather.php on line 12

do you know from where this can come from?

Thanks!

Post November 17th, 2008, 1:00 pm

what is the complete code for your page?
RewriteEngine On

RewriteRule ^(awesome|excellent|extraordinary)$ RT
  • ph81luc
  • Born
  • Born
  • No Avatar
  • Joined: Nov 17, 2008
  • Posts: 3
  • Status: Offline

Post November 18th, 2008, 7:18 am

Hi,

I just copied the content from the PHP script you post it. Make a simple PHP page and uploaded here: http://ph81luc.net46.net/weather.php

I have tested also with another website and there did not receive any result from running this page. That why I place that "Hello word" text in the file; for testing purposes;
  • Bogey
  • Disturbed
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 7128
  • Loc: Ozzuland
  • Status: Offline

Post November 27th, 2008, 10:19 am

I get everything perfect. Here is what I have in my file.

Code: [ Download ] [ Select ]
<?php
//I am using the DOM(Document Object Model) library to read the entire XML document into memory first.
$doc = new DOMDocument();
$doc->load('http://weather.yahooapis.com/forecastrss?p=SFXX0044&u=c');
//now I get all elements inside this document with the following name "channel", this is the 'root'
$channel = $doc->getElementsByTagName("channel");
//now I go through each item withing $channel
foreach($channel as $chnl)
{
//I then find the 'item' element inside that loop
$item = $chnl->getElementsByTagName("item");
foreach($item as $itemgotten)
{
//now I search within '$item' for the element "description"
$describe = $itemgotten->getElementsByTagName("description");
//once I find it I create a variable named "$description" and assign the value of the Element to it
$description = $describe->item(0)->nodeValue;
//and display it on-screen
echo $description;
}
}
?>
  1. <?php
  2. //I am using the DOM(Document Object Model) library to read the entire XML document into memory first.
  3. $doc = new DOMDocument();
  4. $doc->load('http://weather.yahooapis.com/forecastrss?p=SFXX0044&u=c');
  5. //now I get all elements inside this document with the following name "channel", this is the 'root'
  6. $channel = $doc->getElementsByTagName("channel");
  7. //now I go through each item withing $channel
  8. foreach($channel as $chnl)
  9. {
  10. //I then find the 'item' element inside that loop
  11. $item = $chnl->getElementsByTagName("item");
  12. foreach($item as $itemgotten)
  13. {
  14. //now I search within '$item' for the element "description"
  15. $describe = $itemgotten->getElementsByTagName("description");
  16. //once I find it I create a variable named "$description" and assign the value of the Element to it
  17. $description = $describe->item(0)->nodeValue;
  18. //and display it on-screen
  19. echo $description;
  20. }
  21. }
  22. ?>


Exactly as is how righteous_trespasser has it on his tutorial.
Learn PHP | I got 10 PHP tutorials! Check them out!
Dreamtale - Farewell
Just a note... I've giving up on web development and that stuff... Just lost all interest in it.
  • ashleyquick
  • Born
  • Born
  • No Avatar
  • Joined: Nov 20, 2008
  • Posts: 1
  • Status: Offline

Post December 10th, 2008, 7:27 pm

Hi!

Seems as if with the feed, you get "now", today and tomorrow's forecast. Do you know if there's a way to retrieve the day after "tomorrow" so that there are three days of forecasts? I've looked at the yahoo developer page for their weather feed and I can't discern if this is possible.

Thanks!
Ashley

Post December 11th, 2008, 12:32 am

Unfortunately not, the RSS feed only contains the weather for today and tomorrow.
RewriteEngine On

RewriteRule ^(awesome|excellent|extraordinary)$ RT
  • smith21
  • Born
  • Born
  • No Avatar
  • Joined: Jan 02, 2009
  • Posts: 2
  • Status: Offline

Post January 2nd, 2009, 3:33 am

Thanks man its really great information because this is a very good thinking to help anybody with your ways.so according to me good job and thanks again.
  • smoogdog
  • Born
  • Born
  • No Avatar
  • Joined: Feb 05, 2009
  • Posts: 1
  • Loc: Honolulu, Hawaii
  • Status: Offline

Post February 5th, 2009, 2:02 am

The code works great, but how would I go about limiting the information returned so I can only show the current conditions?
  • robin8cham
  • Born
  • Born
  • No Avatar
  • Joined: May 20, 2009
  • Posts: 1
  • Status: Offline

Post May 20th, 2009, 4:01 pm

Using supplied PHP code, get Parse error: syntax error, unexpected T_DNUMBER in /homepages/1/d252457728/htdocs/newweather.php on line 3, where line 3 = $doc = new DOMDocument();

Please advise.

Robin

Post May 21st, 2009, 11:35 pm

I can't really see why that would give you an error, that's not something that's just supposed to give an error. It might be because you don't have the "domxml" extension installed for PHP ...
RewriteEngine On

RewriteRule ^(awesome|excellent|extraordinary)$ RT
  • arifbata
  • Born
  • Born
  • No Avatar
  • Joined: May 23, 2009
  • Posts: 2
  • Status: Offline

Post May 23rd, 2009, 5:25 pm

Hi

Actually I have got a project in which i need to show the current weather conditions. Now the question is as in the "righteous_trespasser" tutorial its just showing the weather of one city at a time. what i need is i got a world map and when the user select a city a marker is placed on the map on that city and when the marker is clicked it should show the weather of that particular city. As say if there is a marker on sydney then it should show the weather of sydney. I hope you got my question.

Please Suggest,
Thanx a lot.
  • arifbata
  • Born
  • Born
  • No Avatar
  • Joined: May 23, 2009
  • Posts: 2
  • Status: Offline

Post May 23rd, 2009, 5:28 pm

Hi Robin,

the error which you are getting is as per my knowledge is due to the line numbers. You might have pasted a code from here to your file which consists of line numbers. Remove the line number and I hope your problem will be resolved.

Cheers.

Post May 24th, 2009, 11:27 pm

Hi, just go to the Yahoo Weather Home Page and in that list, find the region you are looking for and you'll see in the url is that region's unique code which you just have to enter into the request URL ... So let's say I am looking for all the region's in South-Africa I will go to this page and just look at the code for each region as I hover over the link ... and then I'll create a page that takes that code dynamically and sends a request URL and then shows the relevant data.
RewriteEngine On

RewriteRule ^(awesome|excellent|extraordinary)$ RT
  • thunderball
  • Born
  • Born
  • No Avatar
  • Joined: Oct 07, 2009
  • Posts: 2
  • Status: Offline

Post October 7th, 2009, 1:40 pm

Thanks for the tutorial, i was looking all over for this.

I pasted the code directly from the downloaded file and got this error:

Code: [ Download ] [ Select ]
Parse error: syntax error, unexpected T_OBJECT_OPERATOR on line 17

line 17 is "$description = $describe->item(0)->nodeValue;"
  1. Parse error: syntax error, unexpected T_OBJECT_OPERATOR on line 17
  2. line 17 is "$description = $describe->item(0)->nodeValue;"


any suggestions?

Thx
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post October 7th, 2009, 1:40 pm

Post Information

  • Total Posts in this topic: 20 posts
  • Moderator: Tutorial Writers
  • Users browsing this forum: No registered users and 2 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© Unmelted Enterprises 1998-2009. Driven by phpBB © 2001-2009 phpBB Group.