TUTORIAL: Read weather from Yahoo! weather with PHP

  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6230
  • Loc: South-Africa

Post 3+ Months Ago

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: [ 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: [ 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: [ 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)
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • ph81luc
  • Born
  • Born
  • ph81luc
  • Posts: 3

Post 3+ Months Ago

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!
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6230
  • Loc: South-Africa

Post 3+ Months Ago

what is the complete code for your page?
  • ph81luc
  • Born
  • Born
  • ph81luc
  • Posts: 3

Post 3+ Months Ago

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
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

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

Code: [ 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.
  • ashleyquick
  • Born
  • Born
  • ashleyquick
  • Posts: 1

Post 3+ Months Ago

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
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6230
  • Loc: South-Africa

Post 3+ Months Ago

Unfortunately not, the RSS feed only contains the weather for today and tomorrow.
  • smith21
  • Born
  • Born
  • smith21
  • Posts: 2

Post 3+ Months Ago

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
  • smoogdog
  • Posts: 1
  • Loc: Honolulu, Hawaii

Post 3+ Months Ago

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
  • robin8cham
  • Posts: 1

Post 3+ Months Ago

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
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6230
  • Loc: South-Africa

Post 3+ Months Ago

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 ...
  • arifbata
  • Born
  • Born
  • arifbata
  • Posts: 2

Post 3+ Months Ago

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
  • arifbata
  • Posts: 2

Post 3+ Months Ago

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.
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6230
  • Loc: South-Africa

Post 3+ Months Ago

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.
  • thunderball
  • Born
  • Born
  • thunderball
  • Posts: 2

Post 3+ Months Ago

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: [ 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
  • thunderball
  • Born
  • Born
  • thunderball
  • Posts: 2

Post 3+ Months Ago

nevermind, i did not have php5 installed, it works :)
  • kpbca2000
  • Born
  • Born
  • kpbca2000
  • Posts: 2

Post 3+ Months Ago

Hello Sir your this php tutorial for xml parsing is really help full to me but can you tell me how can i get access and display the geo:lat and geo:long elements in <item> please reply as soon as possible because i am doing my MSc's dissertaion.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

I'm pretty sure it's
Code: [ Select ]
channel->item->geo:lat
  • kpbca2000
  • Born
  • Born
  • kpbca2000
  • Posts: 2

Post 3+ Months Ago

sorry to ask again but i managed any how to get data from geo:lat and geo:long now can any one tell me how can i get data of temp from <yweather:condition text="Mostly Cloudy" code="26" temp="57" date="Tue, 29 Nov 2005 3:56 pm PST"></yweather:condition> i am using DOMDocument() in php5.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

I don't think my previous post was correct. If it was, than ignore this post, if it wasn't, then don't ignore this post :)

Code: [ Select ]
channel->item->geo['lat']


I did that before and I don't really remember how I did it. I'm not in the mood to test things right now though.
  • ventage
  • Born
  • Born
  • ventage
  • Posts: 2

Post 3+ Months Ago

Hello,

Thank you for this useful tutorial. It's working perfect.

But is it possible to remove those:

Forecast:
Wed - Mostly Clear. High: 18 Low: 8
Thu - Sunny. High: 19 Low: 8

Full Forecast at Yahoo! Weather

(provided by The Weather Channel)

and use custom icons for it?

Thank you for your time and waiting for response.
  • ventage
  • Born
  • Born
  • ventage
  • Posts: 2

Post 3+ Months Ago

Ok i find the way to hiding those texts but can please someone could tell me how to use custom icons.

Thanks.

Post Information

  • Total Posts in this topic: 22 posts
  • Moderator: Tutorial Writers
  • Users browsing this forum: No registered users and 1 guest
  • 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
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.