user2684521
user2684521

Reputation: 380

Having a hard time accessing xml data with php

Im writing a simple application using a real estate api. This is my first time using an api and is my first time using xml.

I think I am missing the scope in the xml file, but I am not sure. The goal I am trying to achieve is to echo the cities name

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<TruliaWebServices>
<request>
<Parameter>
<index>1</index>
<name>library</name>
<value>TruliaStats</value>
</Parameter>
<Parameter>
<index>2</index>
<name>function</name>
<value>getCityStats</value>
</Parameter>
<Parameter>
<index>3</index>
<name>city</name>
<value>San Diego=CA</value>
</Parameter>
<Parameter>
<index>4</index>
<name>state</name>
<value>CA</value>
</Parameter>
<Parameter>
<index>5</index>
<name>startDate</name>
<value>2014-05-06</value>
</Parameter>
<Parameter>
<index>6</index>
<name>endDate</name>
<value>2014-09-03</value>
</Parameter>
<Parameter>
<index>7</index>
<name>statType</name>
<value>listings</value>
</Parameter>
<Parameter>
<index>8</index>
<name>apikey</name>
<value>auww6cdtukpkm8j824eqbrh6</value>
</Parameter>
</request>
<response>
<TruliaStats>
<location>
<city>San Diego=CA</city>
<state>CA</state>
<searchResultsURL>http://www.trulia.com/CA/San_Diego%3DCA/</searchResultsURL>
<cityGuideURL>
http://www.trulia.com/real_estate/San_Diego%3DCA-California/
</cityGuideURL>
<heatMapURL>
http://www.trulia.com/home_prices/California/San_Diego%3DCA-heat_map/
</heatMapURL>
</location>
<listingStats/>
</TruliaStats>
</response>
</TruliaWebServices>

And here is my php code.

<?php 
        $url = "http://api.trulia.com/webservices.php?library=TruliaStats&function=getCityStats%20&city=San%20Diego=CA&state=CA&startDate=2014-05-06&endDate=2014-09-03&statType=listings&apikey=ssssssssssssssss";
        $xml = simplexml_load_file($url);
        echo $xml->TruliaWebServices->response->TruliaStats->location->city;


?>

Upvotes: 0

Views: 59

Answers (1)

Hanky Panky
Hanky Panky

Reputation: 46900

Simply remove the root node (TruliaWebServices) from your echo, that is not needed.

print_r($xml->response->TruliaStats->location->city);  // San Diego

Fiddle

Upvotes: 1

Related Questions