PDA

View Full Version : How we can get the temperature of a city with PHP from a free feed ?



jesica
Jul 10th, 2008, 01:36 AM
Hello everybody,

i would like to ask how we can get the actual temperature of a city via some free rss feed ?
I listen that many big web sites like yahoo or microsoft they give some free feed about weather .

Can someone who knows to give me an example of how we can get just the temperature of a city, par example Madrid, with PHP ?

Thanks in adavance
waiting for your answers ;)

Inigoesdr
Jul 10th, 2008, 03:23 AM
http://www.google.com/search?hl=en&q=get+the+temperature+of+a+city+with+PHP+from+a+free+feed

jesica
Jul 10th, 2008, 12:59 PM
Dear friend,

i did googling before asking here but i did not get much .

Finally i found this code here which is parsing yahoo's weather :


<?php
$weather_feed = file_get_contents("http://weather.yahooapis.com/forecastrss?p=14607&u=f");
$weather = simplexml_load_string($weather_feed);
if(!$weather) die('weather failed');
$copyright = $weather->channel->copyright;

$channel_yweather = $weather->channel->children("http://xml.weather.yahoo.com/ns/rss/1.0");

foreach($channel_yweather as $x => $channel_item)
foreach($channel_item->attributes() as $k => $attr)
$yw_channel[$x][$k] = $attr;

$item_yweather = $weather->channel->item->children("http://xml.weather.yahoo.com/ns/rss/1.0");

foreach($item_yweather as $x => $yw_item) {
foreach($yw_item->attributes() as $k => $attr) {
if($k == 'day') $day = $attr;
if($x == 'forecast') { $yw_forecast[$x][$day . ''][$k] = $attr; }
else { $yw_forecast[$x][$k] = $attr; }
}
}
?>

The problem now is that when am testing it on my localhost or on the web(server) am getting the same errors !

The errors am getting are the following stuff :

Warning: file_get_contents(): URL file-access is disabled in the server configuration in /../index.php on line 11

Warning: file_get_contents(http://weather.yahooapis.com/forecastrss?p=14607&u=f): failed to open stream: no suitable wrapper could be found in /../index.php on line 11

Fatal error: Call to undefined function: simplexml_load_string() in /../index.php on line 12

Why am getting these errors and what i can i do to fix the occuring problem ..? :confused:

Inigoesdr
Jul 11th, 2008, 12:59 AM
You're either using PHP 4 or you have the SimpleXML extension disabled(it's enabled by default). Also, it looks as though your server admin has disabled allow_url_fopen, so you won't be able to use file_get_contents on a URL with your current host.

Mihai
Jul 11th, 2008, 02:29 AM
<!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="92352") {
$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 = "92352"; // 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>
<h1>Lake Arrowhead, California</h1>
<ul>
<li>Current Temperature: <?=$currentTemperature;?>&deg;C</li>
<li>Current Description: <?=$currentDescription;?></li>
</ul>
</body>
</html>

what about changing from zip code to city name ? how can i do that ?

somarya
Oct 20th, 2009, 08:41 AM
This code is working for Lake Arrowhead, CA i.e for USA only.
we need it to work with kuala lumpur,Malaysia zip code



<!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="92352") {
$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 = "92352"; // 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>
<h1>Lake Arrowhead, California</h1>
<ul>
<li>Current Temperature: <?=$currentTemperature;?>&deg;C</li>
<li>Current Description: <?=$currentDescription;?></li>
</ul>
</body>
</html>

what about changing from zip code to city name ? how can i do that ?

eak
Oct 20th, 2009, 04:03 PM
I like Google's weather API... I think it is unofficial at this point, but I found it somewhere online.

http://www.google.com/ig/api?weather=kuala%20lumpur,Malaysia