Showing posts with label Xpath. Show all posts
Showing posts with label Xpath. Show all posts

Mar 5, 2010

Zillow Real Estate API

For our group project we wanted to provide some housing statistics for people who were looking to relocate.The API that we chose to do this was the Zillow API which is a real estate API that allows you to do all kinds of searches for housing information in different areas. The types of info I was looking for was median home prices as well as other pertinent info. I used the following URL to get the data:

$zip = $_REQUEST['zipID'];
$url = 'http://www.zillow.com/webservice/GetDemographics.htm?zws-id=X1-ZWz1c2hu5r9fd7_5f91a&zip=';
$fullUrl = $url.$zip;

This basically provides a XML output that I used to grab the data. Below is the output from the query.



Once I had the data I then grabbed the Zip code returned as well as all of the attribute tags and its child tags. I did this withe the following code.

$zip = $xml->xpath('/Demographics:demographics/response/region/zip/text()');

$attr = $xml->xpath('//attribute');

Once I had all of the attribute tags into the array I was able to access them by using a for loop in order to iterate through all of them and return them back to the Javascript code and finally to the users browser.

for($i=0; $i<10;>name
echo $attr[$i]->values->zip->value[0]
echo $attr[$i]->values->nation->value[0]
}

You can see that the Xpath methods use a object type of structure to access the child nodes. The -> denotes a child node and once you know the path you can traverse all of the tags. For instance the query echo $attr[$i]->values->zip->value[0] is used to access the following data:



Once you have grabbed all of the data you can simply use a series of echos from the PHP script to send the data to the Javascript in a nicely formated string.



Anyway, the final output looks like this. I is not to fancy but it provides the data necessary.

More on Parsing XML

I have been working quite a bit more on the parsing portion of Iteration 3 for the projects and I have found that using Xpath is both a very simple as well as a effective method of parsing the XML. So far I have been able to find tons of documentation as well of examples of how to search for the tags that you need as well as using that data. I used the weather example in my last post but for this on I will use the CareerBuilder API as the example. Here is the beginning of the PHP script:

$zip = $_REQUEST['zipID']; //Here I get the variables from the Javascript
$job = $_REQUEST['jobID'];

$url = 'http://api.careerbuilder.com/v1/jobsearch?&DeveloperKey=WDhb6SR73QBGH8TTJV4T&Location=';
$url2 = '&Keywords=';
$fullUrl = $url.$zip.$url2.$job; //Here I concatenate the strings into one URL

Here I pass the PHP script two variables from my Javascirpt, a job to search for as well as a zip code to search within a given location. Here is the javascript so it makes a bit more sense. I pass the job variable as a parameter while the zip variable is a global so it can be accessed anywhere in the Javascript.

//*********************************************************************************

function getJob(job) { //Here I'm passing the job variable to the function
jbRequest = jobRequest();
if (jbRequest == null) {
return;
}

url= "scripts/career.php?zipID=" + escape(zip) +"&jobID=" + escape(job); //Passing to PHP
jbRequest.open("GET", url, true);
jbRequest.onreadystatechange = displayJob;
jbRequest.send(null);

}


Once in the PHP script I do the standard request for the data

$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $fullUrl); //Here is the full URL variable
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch); //Here I put the request return into the data variable
curl_close($ch);


Next I use SimpleXML to get the $data variable into an accessible variable.

$xml = new SimpleXMLElement($data);

Next I start searching for the data. Here is the output from the URL that is passed to the API.

http://api.careerbuilder.com/v1/jobsearch?&DeveloperKey=WDhb6SR73QBGH8TTJV4T&Location=48189&Keywords=Computer%20Programmer




Next I begin the the search. Here I search for all tags=JobSearchResults

$attr = $xml->xpath('//JobSearchResult'); //Search for all tags JobSearchResults and put them in an Array

This data is put into an array called $attr (for attributes) I made this up and it can be anything that you want. Once the data is there I can begin getting the info that I need.

$count = count($attr); //Count the number of results and use that in the for loop

for($i=0; $i<$count; $i++){ echo $attr[$i]->Company; //Access the data in the JobSearchResults/Company tag
echo $attr[$i]->JobTitle /Access the data in the JobSearchResults/JobTitle
echo $attr[$i]->Pay
echo $attr[$i]->Location

}

Well I hope that this information is helpful for someone. Here is the output from the parse.