我正在使用Zillow API来获取搜索结果,我遇到了麻烦。我正在使用他们的GetSearchResults工具,我得到错误:
尝试获取非对象的属性
在我使用->
表示法从XML获取值的行上。我已经阅读过关于它的讨论,也许我正在做错误但是我没有看错。如何从此对象获取数据?
这是我目前的代码。
<?php
$zillow_id = 'X1-ZWz19g3j9ffabv_7galu';
$search = isset($_GET['5411 lydia ave'])?$_GET['5411 lydia ave']:"";
$citystate = isset($_GET['kansascitymo64110'])?$_GET['kansascitymo64110']:"";
$address = urlencode($search);
$citystatezip = urlencode($citystate);
$url = "http://www.zillow.com/webservice/GetSearchResults.htm?zws-id=$zillow_id&address=$address&citystatezip=$citystatezip";
$result = file_get_contents($url);
$data = simplexml_load_string($result);
$zpid=$data->response->results->result[0]->zpid;
echo $zpid;
?>
以下是我所说的XML:
<SearchResults:searchresults xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance" xmlns:SearchResults="http://www.zillow.com/static/xsd/SearchResults.xsd" xsi:schemaLocation="http://www.zillow.com/static/xsd/SearchResults.xsd http://www.zillowstatic.com/vstatic/5b67875/static/xsd/SearchResults.xsd">
<request>...</request>
<message>...</message>
<response>
<results>
<result>
<zpid>2349353</zpid>
<links>...</links>
<address>
<street>5411 Lydia Ave</street>
<zipcode>64110</zipcode>
<city>Kansas City</city>
<state>MO</state>
<latitude>39.02831</latitude>
<longitude>-94.568747</longitude>
</address>
<zestimate>...</zestimate>
<localRealEstate>...</localRealEstate>
</result>
</results>
</response>
</SearchResults:searchresults>
答案 0 :(得分:0)
result
至少不是数组。所以正确的语法是
echo $data->response->results->result->zpid;
答案 1 :(得分:0)
如果你有多个这样的结果:
<response>
<results>
<result>
<zpid>2349353</zpid>
</result>
<result>
<zpid>5676567</zpid>
</result>
<result>
<zpid>987987</zpid>
</result>
</results>
</response>
您应该获得zpid
,如下所示:
$zpid=$data->response->results->result[0]->zpid; // 2349353
$zpid=$data->response->results->result[1]->zpid; // 5676567
$zpid=$data->response->results->result[2]->zpid; // 987987