Zillow数据 - json_encode无效 - 适用于常规变量

时间:2016-03-02 00:32:44

标签: javascript php ajax zillow

我有一个可能会或可能没有解决的问题,但我似乎是唯一一个使用纯JavaScript而不是JQuery来完成我的简单AJAX请求的人。

首先是我的AJAX:

function getZestimate(address,csz){
var xmlhttp = new XMLHttpRequest();

    var userdata = "address="+address+"&csz="+csz;

    xmlhttp.open("POST","../wp-content/themes/realhomes/submit_address.php",true);

    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    xmlhttp.onreadystatechange = function(){
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
            retrieve = JSON.parse(xmlhttp.responseText);
            document.getElementById("zestimateArea").innerHTML = 
                '<div id="zillowWrap">
                    <div id="logoANDtag">
                     <a href="http://www.zillow.com"><img src="http://www.zillow.com/widgets/GetVersionedResource.htm?path=/static/logos/Zillowlogo_150x40.gif" width="150" height="40" alt="Zillow Real Estate Search" id="ZillowLogo" /></a>
                     <span id="zestimateTag">Zestimate&reg;</span>
                    </div>
                    <span id="zestimatePrice">'+retrieve[0]+'</span>
                 </div>
                 <div id="zillowDisclaimer">
                   <span>&copy; Zillow, Inc., 2006-2014. Use is subject to <a href="http://www.zillow.com/corp/Terms.htm">Terms of Use</a></span
                   <span>What&rsquo;s a <a href="http://www.zillow.com/wikipages/What-is-a-Zestimate">Zestimate?</a>
                 </div>';
        }
        else{
            document.getElementById("zestimateArea").innerHTML = "Error!"
        }
    }

    xmlhttp.send(userdata);
    document.getElementById("zestimateArea").innerHTML = "Generating...";

    return false;
}

接下来,这是我的PHP:

<?php
    $zillow_id = '1234';
    $search = $_POST['address'];
    $citystate = $_POST['csz'];
    $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);

    $zpidNum = $data->response->results->result[0]->zpid;

    $zurl = "http://www.zillow.com/webservice/GetZestimate.htm?zws-id=".$zillow_id."&zpid=".$zpidNum;
    $zresult = file_get_contents($zurl);
    $zdata = simplexml_load_string($zresult);

    $zestimate=$zdata->response->zestimate->amount;
    $street=$zdata->response->address->street;
    $city=$zdata->response->address->city;
    $state=$zdata->response->address->state;
    $zip=$zdata->response->address->zip;
    $one='one';
    $two='two';
    header("Content-Type: application/json; charset=utf-8", true);
    echo json_encode(array($zestimate,$street));
?>

我的AJAX中返回的是[object Object],我的控制台没有错误。

但是,请参阅2个变量$one$two?如果我将它们放在json_encode echo json_encode(array($one,$two));之内,它会像它应该的那样返回one

我不确定与Zillow数据的区别。我可以echo个别没问题。但我需要发送多个值来使用。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

当您使用SimpleXML解析文档时,所有节点都是objects,当您尝试回显它们时会被转换为字符串,但是当给予像json_encode这样的函数时,您不会得到你期望的结果。

要使它们成为json_encode的字符串,请尝试以下方法:

$zestimate = (string)$zdata->response->zestimate->amount;
$street    = (string)$zdata->response->address->street;

echo json_encode([$zestimate, $street]);