POSSA / freepbx-weather-by-zip

FreePBX module, reads back weather forcast from dtmf zip code input
4 stars 5 forks source link

Weather service query fails if city contains a space #10

Closed lgaetz closed 11 years ago

lgaetz commented 12 years ago

agi-bin/nvweather-zip.php first queries mysql zipcodes table to match the zip code with the name of the city. The city, state is used to structure the URL for the weather service. If the city name contains more than one word the URL fails and user gets the "I'm sorry, no weather information ...."

Problem is not mysql related

lgaetz commented 12 years ago

Problem is with fopen:

$query = "http://www.srh.noaa.gov/port/port_zc.php?inputstring=$zip" ;

$fd = fopen($query, "r");
if (!$fd) {
 echo "<p>Unable to open web connection. \n";
 exit;
}
$value = "";
while(!feof($fd)){
        $value .= fread($fd, 4096);
}
fclose($fd);

Using Beverly Hills zip code of 90210, the query becomes: http://www.srh.noaa.gov/port/port_zc.php?inputstring=90210 which redirects to: http://mobile.weather.gov/port_mp_ns.php?CityName=Beverly%20Hills&site=LOX&State=CA&warnzone=CAZ041 but fopen captures: http://mobile.weather.gov/port_mp_ns.php?CityName=Beverly

lgaetz commented 12 years ago

Additional info added by Hyksos on PIAF forum:

The first URL get redirected to a second one on a different host, which get redirected on the same host this time but to a different location. So 2 redirections, in your browser you see the "%20" because your browser does urlencode stuff by itself, fopen won't and needs the "%20" to be there or it fails.

The second redirect where you see the %20 in your browser actually looks like that for real:

HTTP/1.1 302 Found
Via: 1.1 SNISAWEBFWP2 Connection: Keep-Alive Proxy-Connection: Keep-Alive Content-Length: 0 Date: Sat, 05 May 2012 20:03:50 GMT
Location: port_mp_ns.php?CityName=Beverly Hills&site=LOX&State=CA&warnzone=CAZ041 Content-Type: text/html; charset=UTF-8
Server: Apache

No "%20" ... so fopen goes there and the stuff is not a valid URL

lgaetz commented 11 years ago

Fixed on latest commit