Showing posts with label WeatherBug API. Show all posts
Showing posts with label WeatherBug API. Show all posts

Mar 5, 2010

7 Day Forcast WeatherBug

For my individual project I used a simple current conditions search to retrieve the current conditions for an area. However, for our group project I wanted to provide a 5 or 7 day forcast. While this wasn't much different for getting the data, parsing through the results was much different as I had to create loops to search through the iterations. See my previous posts to see the code behind in order to return the data but for this post I'm going to show you how to search through the data and parse the XML. First I used the following URL to get the data.

http://A6357896562.api.wxbug.net/getForecastRSS.aspx?ACode=A6357896562&OutputType=1&zipCode=48189.

This returns the following data:




After I get the data it is time for parsing. I use the SimpleXML Xpath function to get the data into a searchable variable.

$xml = new SimpleXMLElement($data);

Then I simply create a for loop to iterate through the data and diplay the results in HTML format. Since there are HTML tags I can't cut and paste the text into the blog as it tries to interpret the tags. However, I have put up a screenshot of the for loop and the methods used to optain the XML tag info HERE.

Here is the final output:

SimpleXML Paring the Data

In my last post I should you how I was able to get the data from the xml dump to be displayed in the browser. The problem with that was that it was just data. It was unorganized and in no logical format that the user could discern, well all that is about to change. While it took a little time for me to understand how to parse the data, once I figured it out, things moved along fairly quickly. I chose to parse the data using SimpleXML and Xpath in my PHP proxy function. I found that was the easiest way to get the data formatted for proper display. More can be found on the Internet about SimpleXML and Xpath.

The most important part of this process is to get the data into a variable so it can be parsed. If you look at my last post you will see the process of creating the curl session. Once the session is complete you need to take that $data variable and use it to create the $xml variable.

$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $fullUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);

$xml = new SimpleXMLElement($data);

This simple line takes the $data returned from the curl session and puts it into a SimpleXML format so that we can parse it. THe next step is to look through your xml by taking the url in your request and putting that into the firefox browser. This will show you the tags and their attributes that are returned so you can figure out what you need. For this instance I used:

http://a6357896562.api.wxbug.net/getLiveWeatherRSS.aspx?ACode=A6357896562&OutputType=1&zipCode=48189

I ended up with the following output:




From here I can start to figure out the tags I want to grab. I do this by using a object type structure. I created variables in the PHP script to hold the resutls of the query. In this one $zip = $xml->xpath('//aws:city-state/@zipcode'); I search for the aws:city-state tag and then I grab the attribute "zipcode".

$timezone = $xml->xpath('//aws:time-zone/@text');
$temphigh = $xml->xpath('/aws:weather/aws:ob/aws:temp-high/text()');
$templow = $xml->xpath('/aws:weather/aws:ob/aws:temp-low/text()');
$feelslike = $xml->xpath('/aws:weather/aws:ob/aws:feels-like/text()');
$city = $xml->xpath('/aws:weather/aws:ob/aws:city-state/text()');
$zip = $xml->xpath('//aws:city-state/@zipcode');
$current_temp = $xml->xpath('/aws:weather/aws:ob/aws:temp /text()');
$elevation = $xml->xpath('/aws:weather/aws:ob/aws:elevation/text()');
$wind = $xml->xpath('/aws:weather/aws:ob/aws:wind-speed/text()');
$lat = $xml->xpath('/aws:weather/aws:ob/aws:latitude/text()');
$long = $xml->xpath('/aws:weather/aws:ob/aws:longitude/text()');
$icon = $xml->xpath('//aws:current-condition/@icon');

Once I had all the data that I need I then format the data into a nice presentable form by using a series of HTML Table tags. This allows me to creat the format and then just hand the JAvascript function something that it needs to spit out. Unfortunately the blog is freaking out and wont let me upload a picture so I put it on my emich site instead Here...Good Luck

Here is the finished Product: