Jump to content

Best Way to Get Temperature Readings by U.S. City


JipThePeople

Recommended Posts

This is very difficult.  "The easiest way" is going to take a new user a number of days.

 

The US weather service has various public data streams you can use, or you could try to crawl weather.com or weatherunderground.com's data files or pages.

 

-Dan

using the google api

<?PHP
function GetTemperature($what_zip) {
$url = "http://www.google.com/ig/api?weather=" . $what_zip;
$file = file_get_contents($url);
$needle = '<temp_f data="';
$marray = explode($needle, $file);
$marray2 = explode('"', $marray[1]);
return $marray2[0];
}

$city_array = array ("48185", "90032","39350","23452");
$count = count($city_array);
$i = 0;
while($i<$count) {
$zip = $city_array[$i];
echo $zip . " is " . GetTemperature($zip) . " degrees F<br>";
$i++;
}


?>

Thx litebearer. Looks like this is exactly the code I was looking for.

 

Unfortunately, my web host has the URL file-access is disabled:

 

"Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration"

 

I think your code can be altered to use curl functions as a work around. Do you know how to edit the Google code to use curl? Any advice will be greatly appreciated.

Well I found the solution (thx litebearer!):

 

function curl_get_file_contents($URL){

$c = curl_init();

curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($c, CURLOPT_URL, $URL);

$contents = curl_exec($c);

curl_close($c);

 

if ($contents) return $contents;

else return FALSE;

}

 

// Google API code

function GetTemperature($what_zip) {

$url = "http://www.google.com/ig/api?weather=" . $what_zip;

// Web host disabled file_get_contents(), so this is the workaround

$file = curl_get_file_contents($url);

$needle = '<temp_f data="';

$marray = explode($needle, $file);

$marray2 = explode('"', $marray[1]);

return $marray2[0];

}

 

$city_array = array ("48185", "90032","39350","23452");

$count = count($city_array);

$i = 0;

while($i<$count) {

$zip = $city_array[$i];

echo $zip . " is " . GetTemperature($zip) . " degrees F<br>";

$i++;

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.