Sunday, June 24, 2012

PHP Show current temprature of city through zipcode

<?php echo "NOTE : Please on curl library from php.ini file";?>
<!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=utf-8" />
<title>Yahoo! Weather API RSS</title>
<?php
function retrieveYahooWeather($zipCode="394230") {
    $yahooUrl = "http://weather.yahooapis.com/forecastrss";
    $yahooZip = "?p=$zipCode";
    $yahooFullUrl = $yahooUrl . $yahooZip;
    $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;
}
$localZipCode = "07306"; // Lake Arrowhead, CA
$weatherXmlString = retrieveYahooWeather($localZipCode);
$weatherXmlObject = new SimpleXMLElement($weatherXmlString);
$currentCondition = $weatherXmlObject->xpath("//yweather:condition");
$currentTemperature = $currentCondition[0]["temp"];
$currentDescription = $currentCondition[0]["text"];
?>
</head>
<body>
<ul>
    <li>Current Temperature: <?=$currentTemperature;?>&deg;F</li>
    <li>Current Description: <?=$currentDescription;?></li>
</ul>
</body>
</html>

No comments:

Post a Comment

Please mention your comments.......