JipThePeople Posted November 19, 2010 Share Posted November 19, 2010 I am looking for the easiest way to get live data for the current temperature for various U.S. cities. I just need to capture the data and display on the page. Any recommendations? Quote Link to comment https://forums.phpfreaks.com/topic/219199-best-way-to-get-temperature-readings-by-us-city/ Share on other sites More sharing options...
ManiacDan Posted November 19, 2010 Share Posted November 19, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/219199-best-way-to-get-temperature-readings-by-us-city/#findComment-1136660 Share on other sites More sharing options...
litebearer Posted November 19, 2010 Share Posted November 19, 2010 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++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/219199-best-way-to-get-temperature-readings-by-us-city/#findComment-1136677 Share on other sites More sharing options...
JipThePeople Posted November 19, 2010 Author Share Posted November 19, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/219199-best-way-to-get-temperature-readings-by-us-city/#findComment-1136689 Share on other sites More sharing options...
JipThePeople Posted November 19, 2010 Author Share Posted November 19, 2010 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++; } Quote Link to comment https://forums.phpfreaks.com/topic/219199-best-way-to-get-temperature-readings-by-us-city/#findComment-1136699 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.