...

View Full Version : PHP - Retrieving Yahoo Weather script



mannkd
12-11-2010, 11:25 AM
Hi. I have written the following script in which i retrieve weather information from the Yahoo Weather RSS feed.

the program works like this:

The user enters the city name for which they want to view the weather. The city name is checked with the list of places and then the weather for tht place is retrieved.

However, i need to make this a little autonomous, that is, i want make the user to enter the city name only once and then refresh the weather every hour so that the user does not need to enter the city name everytime to view the weather, unless he/she wants to change the city.

Can anyone please help me out on how to go about it as i pretty new to PHP and do not know all of its functionalities.

Here is my HTML code to get city name from user:

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.style3 {
color: #FFFFFF;
font-weight: bold;
}
-->
</style>
</head>

<body background="black-circles.jpg">
<p align="right"><a href="logout.php" target="_parent"><em>Logout</em></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<form id="form1" name="form1" method="post" action="we.php">
<table width="372" border="0" align="center">
<tr>
<td><span class="style3">Enter city name </span></td>
<td><div align="center"><span class="style3">:</span></div></td>
<td><label>
<input type="text" name="city" />
</label></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td colspan="3"><label>
<div align="center">
<input type="submit" name="submit" value="Submit" />
</label>
</div></td>
</tr>
</table>
</form>
</body>
</html>

here is my PHP code to retrieve the weather:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<?php
//autorefreshing the page every 30 seconds to keep the weather updated. Right now its set to abu dhabi, but i want it to b set to the cityname that user chooses dynamically rather than me setting it manually
header("Refresh:30; url=http://ipurse.byethost31.com/we.php?city=Abu+Dhabi&unit=c&submit=Submit");
$cityname=$_GET["city"];



$file = fopen("places.txt", "r");
while(!feof($file))
{
$str = fgets($file);
list($cname, $zip) = explode(":", $str);

if($cityname == $cname)
{
$cityzipcode = $zip;
}
}
fclose($file);

function retrieveYahooWeather($zipCode = '$cityzipcode')
{
$yahooUrl = "http://weather.yahooapis.com/forecastrss";
$yahooZip = "?p=$zipCode";
$yahooUnit = "&u=c";
$yahooFullUrl = $yahooUrl . $yahooZip . $yahooUnit;
$curlObject = curl_init();
curl_setopt($curlObject,CURLOPT_URL,$yahooFullUrl);
curl_setopt($curlObject,CURLOPT_HEADER,false);
curl_setopt($curlObject,CURLOPT_RETURNTRANSFER,true);
$returnYahooWeather = curl_exec($curlObject);
curl_close($curlObject);
return $returnYahooWeather;
}

$placeZipCode = $cityzipcode;
$weatherXmlString = retrieveYahooWeather($placeZipCode);
$weatherXmlObject = new SimpleXMLElement($weatherXmlString);
$currentCondition = $weatherXmlObject->xpath("//yweather:condition");
$weatherLoca = $weatherXmlObject->xpath("//yweather:location");
$currentTemperature = $currentCondition[0]["temp"];
$currentDescription = $currentCondition[0]["text"];
$currentTitle = $currentTitle[0]["text"];
$weatherLoca = $weatherLoca[0]["city"];

if($temperatureUnit == 'c')
{
$currentTemperature = (($currentTemperature-32)*5/9);
$currentTemperature = round($currentTemperature);
}

$file1 = fopen("weather.txt", "w+");
fwrite($file1, "Current weather for $cityname:\n$currentDescription, $currentTemperature C");
fclose($file1);
?>

<body>
<p>Current Weather for <?php print $weatherLoca; ?>:</p>
<p><?php print $currentDescription?>, <?php print $currentTemperature?>&deg;C</p>
</body>
</html>

Please the help would be really appreciated :)

phpdeveloper
12-11-2010, 02:53 PM
Please look at below url this might be help full to you.

URL : http://www.bitrepository.com/show-yahoo-weather.html
Demo : http://www.bitrepository.com/demo/show-yahoo-weather/



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum