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