用php访问xml数据很困难

时间:2014-09-12 04:38:24

标签: php html xml

我正在使用房地产API编写一个简单的应用程序。这是我第一次使用api,这是我第一次使用xml。

我想我错过了xml文件中的范围,但我不确定。我想要实现的目标是回应城市名称

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>

这是我的PHP代码。

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


?>

1 个答案:

答案 0 :(得分:1)

只需从回声中删除根节点(TruliaWebServices),这是不需要的。

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

<强> Fiddle