Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Yahoo! Weather API Example Using SimpleXML in PHP
<?php
if(isset($_POST['zipcode']) && is_numeric($_POST['zipcode'])){
$zipcode = $_POST['zipcode'];
}else{
$zipcode = '50644';
}
$result = file_get_contents('http://weather.yahooapis.com/forecastrss?p=' . $zipcode . '&u=f');
$xml = simplexml_load_string($result);
//echo htmlspecialchars($result, ENT_QUOTES, 'UTF-8');
$xml->registerXPathNamespace('yweather', 'http://xml.weather.yahoo.com/ns/rss/1.0');
$location = $xml->channel->xpath('yweather:location');
if(!empty($location)){
foreach($xml->channel->item as $item){
$current = $item->xpath('yweather:condition');
$forecast = $item->xpath('yweather:forecast');
$current = $current[0];
$output = <<<END
<h1 style="margin-bottom: 0">Weather for {$location[0]['city']}, {$location[0]['region']}</h1>
<small>{$current['date']}</small>
<h2>Current Conditions</h2>
<p>
<span style="font-size:72px; font-weight:bold;">{$current['temp']}&deg;F</span>
<br/>
<img src="http://l.yimg.com/a/i/us/we/52/{$current['code']}.gif" style="vertical-align: middle;"/>&nbsp;
{$current['text']}
</p>
<h2>Forecast</h2>
{$forecast[0]['day']} - {$forecast[0]['text']}. High: {$forecast[0]['high']} Low: {$forecast[0]['low']}
<br/>
{$forecast[1]['day']} - {$forecast[1]['text']}. High: {$forecast[1]['high']} Low: {$forecast[1]['low']}
</p>
END;
}
}else{
$output = '<h1>No results found, please try a different zip code.</h1>';
}
?>
<html>
<head>
<title>Weather</title>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
label {
font-weight: bold;
}
</style>
</head>
<body>
<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" />
</form>
<hr />
<?php echo $output; ?>
</body>
</html>
@fakeplastictrees

This comment has been minimized.

Copy link

@fakeplastictrees fakeplastictrees commented Jan 9, 2015

if i want see a celcius ? not fahrenheit. how?

@samstevens

This comment has been minimized.

Copy link

@samstevens samstevens commented Jan 26, 2015

Just change the u=f to u=c in
$result = file_get_contents('http://weather.yahooapis.com/forecastrss?p=' . $zipcode . '&u=f');

@ellenna

This comment has been minimized.

Copy link

@ellenna ellenna commented Mar 25, 2015

Why does not work for other citties exept from the usa ??

@dhammucool

This comment has been minimized.

Copy link

@dhammucool dhammucool commented Mar 13, 2016

in the above how to see weather for Indian cities?

@paulazh

This comment has been minimized.

Copy link

@paulazh paulazh commented Mar 24, 2016

Amazing!! thank you.

@Nitrotoxin

This comment has been minimized.

Copy link

@Nitrotoxin Nitrotoxin commented Mar 27, 2016

doesn't work =(
Warning: file_get_contents(http://weather.yahooapis.com/forecastrss?p=50644&u=f): failed to open stream: HTTP request failed! HTTP/1.0 401 Unauthorized in C:\xampp\htdocs\weather.php on line 7

Did they change the yahoo weather api recently?

@ixlipixli

This comment has been minimized.

Copy link

@ixlipixli ixlipixli commented Mar 29, 2016

I am having the same problem. The code is not working anymore.

@ntsasng

This comment has been minimized.

Copy link

@ntsasng ntsasng commented Mar 29, 2016

Same issue.

@m4rky77

This comment has been minimized.

@Dzonny

This comment has been minimized.

Copy link

@Dzonny Dzonny commented Mar 31, 2016

Shame to see this happened. Any chance for the alternative script maybe?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment