Read weather information using yahoo API in PHP, MySQL

Many times we see websites that read weather from an RSS feed on another site, here I will show you how to read weather onto your site from Yahoo. Here we will create example on read weather information using yahoo API in php mysql.

How to get the XML from

To read the weather from Yahoo API, you need to send a request as follows:

where p is the location code. In this tutorial I am using “Calcutta/Kolkata, India”, and as you can see the location code or zip code is INXX0028, if you live in the USA you can just use the ZIP code where you live in. Next, ‘u’ is the unit in which the weather must be displayed, I chose ‘f’ for Fahrenheit, but if you want, you can also choose ‘c’ for Celsius.

The final output

read weather information using yahoo api

The XML response which is returned from Yahoo weather API:

<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 />
<yweather:condition text code temp date>
<yweather:forecast day date low high text code />
<yweather:forecast day date low high text code />
<guid Ispermalink></guid>

In the above XML response, the “root” element is “channel” and beneath that we have other elements as well, the only element we want to get from here is the following one:


So let’s have a look at the code we are going to use. I am echoing the result as well as saving data to MySQL database.

Create Database table

The following table will store weather information from Yahoo api.

CREATE TABLE `weather_yahoo` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`conditions` text COLLATE latin1_general_ci,

Retrieve weather info from XML response and insert them into database and finally show to the user.

The below source codes read weather information using yahoo api and parse the XML response and display weather information on the web page as well as store the weather information into the mysql database.

<!DOCTYPE html>
<meta charset="UTF-8">
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
label {
font-weight: bold;
//database connection parameters
$username = "root";
$password = "";
$hostname = "localhost";
$database = "cdcol";

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");

//select a database to work with
$selected = mysql_select_db($database, $dbhandle) or die("Could not select examples");

//Once you input the zip code through input form your result is displayed here
//Check whether zip code entered through input form else use default zip
if (isset($_POST['zipcode'])) {
$zipcode = $_POST['zipcode'];
} else {
$zipcode = 'INXX0028';

//Get the entire content for this zip code with temperature in Farhenheit unit
$result = file_get_contents('' . $zipcode . '&u=f');
//interpret the xml string into an object
$xml = simplexml_load_string($result);

$xml->registerXPathNamespace('yweather', '');
$location = $xml->channel->xpath('yweather:location');

$html = '';
if (!empty($location)) {
foreach ($xml->channel->item as $item) {
$current = $item->xpath('yweather:condition');
$forecasts = $item->xpath('yweather:forecast');
$current = $current[0];
$html .= '<h1 style="margin-bottom: 0">Weather for ' . $location[0]['city'] . ', ' . $location[0]['region'] . '</h1>';
$html .= '<small>Today: ' . $current['date'] . '</small>';
$html .= '<h2>Current Conditions</h2>';
$html .= '<p>';
$html .= '<span style="font-size:72px; font-weight:bold;">' . $current['temp'] . '&deg;F</span>';
$html .= '<br/>';
$html .= '<img src="' . $current['code'] . '.gif" style="vertical-align: middle;"/>&nbsp;' . $current['text'];
$html .= '</p>';
$html .= '<h2>Forecast</h2>';
foreach ($forecasts as $forecast) {
$html .= '<p>' . $forecast['day'] . ' - ' . $forecast['text'] . ', High: ' . $forecast['high'] . '&deg;F, Low: ' . $forecast['low'] . '&deg;F</p>';
} else {
$output = '<h1>No results found, please try a different zip code.</h1>';
<!-- input form where you will input zip code for your location -->
<form method="POST" action="">
<label>Zip Code:</label> <input type="text" name="zipcode" size="8" value="" /><br /><input type="submit" name="submit" value="Lookup Weather" />
<hr />
echo $html;

if ($html) {
$html = mysql_real_escape_string($html);

$sql = "INSERT INTO weather_yahoo(conditions) VALUES('" . $html . "')";
$retval = mysql_query($sql);
if (!$retval) {
die('Could not enter data: ' . mysql_error());
} else {
echo '<h2>No result found</h2>';


Here is the final output onto the browser. Here we are showing weather information for Calcutta, India. You may choose city and country as per your choice.

Weather for Calcutta,
Today: Sun, 06 Apr 2014 12:00 pm IST
Current Conditions



Sun - Mostly Sunny, High: 99°F, Low: 77°F

Mon - Sunny, High: 102°F, Low: 78°F

Tue - Sunny, High: 101°F, Low: 78°F

Wed - Sunny, High: 102°F, Low: 78°F

Thu - Partly Cloudy, High: 97°F, Low: 76°F

If you any query please leave a comment.

Leave a Comment