Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing XML data in Zillow and other API websites

Tags:

php

xml

api

I have the code below working to pull data from a url string from zillow.com (as an example). The zws-id isn't pasted in the url string due to privacy and security reasons but the page returns all api xml data for the state, city, and neighborhood properly. The question would be how to pull a specific piece of data rather than ALL data.

<?php
$url = "http://www.zillow.com/webservice/GetDemographics.htm?zws-id=<my_api_id>&state=WA&city=Seattle&neighborhood=Ballard";
echo json_encode(simplexml_load_string(file_get_contents($url)));
?>

This returns a page full of data and is obviously jumbled because it hasn't been parsed or organized in any way.

One tiny excerpt of what is returned: "state":"Washington","city":"Seattle","neighborhood":"Ballard","latitude":"47.668328","longitude":"-122.384536",...

How would I parse this and get what I need? If I wanted the latitude only how could I use $.parseJSON(data) or the proper syntax to pull that specific item (latitude)?

Of course I know that's WAY off... but I'm brand new at API, xml, json working together and I don't know if there is a standard way to do this or if it is specific to the company I'm using the API with.

Zillow.com offers info such as "Sample API Output" just like a few other sites I've seen but how would I make use of this info? Is it beneficial because it shows the labels of what each item would be called? http://www.zillow.com/howto/api/GetDemographics.htm

Just a simple jump start would help a ton. Thank you!

like image 622
LITguy Avatar asked Feb 18 '13 07:02

LITguy


1 Answers

I hope this helps you out.

To handle the code in PHP:

I notice the code starts out in PHP so let me start from there. If you want to see what the XML object looks like in PHP use print_r() like so:

<?php
    $url = "http://www.zillow.com/webservice/GetDemographics.htm?zws-id=<my_api_id>&state=WA&city=Seattle&neighborhood=Ballard";

    $data = simplexml_load_string(file_get_contents($url));

    print_r($data);

That should give you a result like this:

SimpleXMLElement Object
(
    [response] => Object
    {
        [region] => Object
        {
            [id] => WA,
            [state] => Seattle,
            [city] => Ballard,
            [neighborhood] => Ballard,
            [latitude] => 47.668304,
            [longitude] => -122.384601
        }
    }
)

I've truncated a lot of the response to make it easier to read. By so doing, I may have messed it from the actual Zillow API response but hopefully it helps you out. So to get the Longitude and the latitude from that response you would do this:

<?php
    $latitude = $data->response->region->latitude;
    $longitude = $data->response->region->longitude;

To handle the code in Javascript

The second part of your question includes javascript code $.parseJSON(data). To get the code from PHP into Javascript you would have to echo it out like so:

<?php
    $url = "http://www.zillow.com/webservice/GetDemographics.htm?zws-id=<my_api_id>&state=WA&city=Seattle&neighborhood=Ballard";

    $data = json_encode(simplexml_load_string(file_get_contents($url)));

And then in your html you do:

<script>
    var data = $.parseJSON('<?php echo $data ?>');

    console.log(data); // you can look at the data in your browser console
</script>

I hope that helps!

like image 96
chrislondon Avatar answered Nov 11 '22 11:11

chrislondon