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:

https://www.yahoo.com/news/weather/

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

<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>

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
//I am using the DOM(Document Object Model) library to read the entire XML document into memory first.
$doc = new DOMDocument();
$doc->load('https://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;
}
}
?>

Output

I get something as follows:

The sun shining

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)

This page was published on It was last revised on

Contributing Authors

0

21 Comments

  • Votes
  • Oldest
  • Latest
Commented

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!

add a comment
0
Commented

what is the complete code for your page?

add a comment
0
Commented
Updated

Hi,

I just copied the content from the PHP script you post it. Make a simple PHP page and uploaded it.

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;

add a comment
0
BO
70 2
Commented

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

<?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;
}
}
?>

Exactly as is how righteous_trespasser has it on his tutorial.

add a comment
0
Commented

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

add a comment
0
Commented

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

add a comment
0
Commented

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.

add a comment
0
Commented

The code works great, but how would I go about limiting the information returned so I can only show the current conditions?

add a comment
0
Commented

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

add a comment
0
Commented

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 ...

add a comment
0
Commented

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.

add a comment
0
Commented

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.

add a comment
0
Commented
Updated

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.

add a comment
0
Commented

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

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

Parse error: syntax error, unexpected T_OBJECT_OPERATOR on line 17

line 17 is "$description = $describe->item(0)->nodeValue;"

any suggestions?

Thx

add a comment
0
Commented

nevermind, i did not have php5 installed, it works 🙂

add a comment
0
Commented

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.

add a comment
0
BO
70 2
Commented

I'm pretty sure it's

channel->item->geo:lat
add a comment
0
Commented

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.

add a comment
0
BO
70 2
Commented

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 🙂

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.

add a comment
0
Commented

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.

add a comment
0
Commented

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

Thanks.

add a comment
0