Mar 5, 2010

Weatherbug Proxy Pass through

So I managed to get the proxy pass through working and returning data for iteration 2 of the individual project. Since it took me a while to figure it out I thought that I would pass on my experience in case others are running into the same difficulties. I used the WeatherBug API, and the documentation can be found at WeatherBug.

The first thing I did was to use the createRequest method that was provided to us in class.



Next I had to create the getWeather function to call the proxy. I handed it a zip as a parameter so it could find the weather for a location.




Next I created the proxy script. I did this a bit differently than professor Drake showed us but I was able to find quite a bit of documentation and sample code out there to choose from. This one first takes the zip code that I passed to it and puts it in the zip variable. It then takes the cll url and puts it into the $url vaiable. Then I append the zip to the end of the $url variable by concatenating the strings $fullUrl = $url.$zip. The reset is the call to the API and thn it puts the data into the $data variable.




I then just echo the $data variable and it then sends that data to the displayWeather() function in my javascript.




This function then takes the return data variable and then displays it in my web page in the div id=weather tag. While it took me a while to figure out how to get this to work, it was pretty easy to recreate for my other APIs. I literaly just needed to change all of the global variables and then just call the different URL. I was able to use this same code with 3 other APIs for the group project.

1 comment:

  1. Initially I got confused with the code which Professor Drake gave and I could find several other ways to do it in various documentation. But I used Professors method because that seemed pretty straight forward.

    ReplyDelete