Parsing XML data on Zillow and other API websites

I have the code below that is trying to infer data from a url string from zillow.com (as an example). Zws-id is not inserted in the url string due to privacy and security concerns, but the page correctly returns all xml api data for state, city and surroundings. The question is how to pull out a specific piece of data, not 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 with full data and is obviously messed up because it has not been analyzed or organized in any way.

One tiny excerpt from what comes back: "state":"Washington","city":"Seattle","neighborhood":"Ballard","latitude":"47.668328","longitude":"-122.384536",...

How would I make it out and get what I need? If I only needed latitude, how could I use the $.parseJSON(data)correct syntax to pull out that particular element (latitude)?

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

Zillow.com offers information such as the "Sample API Output", as well as several other sites that I have seen, but how can I use this information? Is this useful because it displays labels of what each item will be called up? http://www.zillow.com/howto/api/GetDemographics.htm

A simple jump start will help a ton. Thank!

+5
source share
1 answer

Hope this helps you.

To process the code in PHP:

, PHP, . , XML PHP, print_r() :

<?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);

:

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

, . , , , API Zillow, , , . , :

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

Javascript

javascript $.parseJSON(data). PHP Javascript, echo :

<?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)));

html :

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

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

, !

+4

All Articles