Jump to content

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


JipThePeople

Recommended Posts

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++;
}


?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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++;

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.