0

So I must be missing something. I can retrieve the zpid and the zestimate no problem doing the following:

$zdata->response->zpid; //zpid
$zdata->response->zestimate->amount; //zestimate

But then when I try what appears to be the obvious equivalent to retrieve a part of the address:

$zdata->response->address->street;
$zdata->response->address->city;

None of it works! Why?? Clearly I must be missing something here. Below is my entire code

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

    //echo $zdata->response->zestimate->amount;

    //$zestimate=$zdata->response->zestimate->amount;
    $zstreet=$zdata->response->address->street;
    echo $street;
?>

Looking at the XML output as seen on Zillow's own documentation, I am following the same pattern to try to get the street as to get the zestimate. I am not very familiar with working with XML so it is very possible I am missing something.

So I am getting an error in my console that shows the following:

Uncaught SyntaxError: Unexpected token T

The 'T' seems to be the first letter of the street that is entered, as it changes accordingly. Perhaps this could shine some light on the issue?

I'll post my AJAX too but I don't know why there would be something wrong with it. As stated above, I am able to display the ZPID and Zestimate just fine, only the address isn't working.

AJAX/JS:

function validateAddress(){
    var address = document.getElementById('address').value;
    var csz = document.getElementById('city_state_zip').value;

    if (address == null || address == "" || csz == null || csz == "") {
        return false;
    }
    else{
        getZestimate(address,csz);
    }
}

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);
              retrieve = xmlhttp.responseText;
            document.getElementById("zestimateArea").innerHTML = '<div id="zillowWrap"><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+'</span><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>';
        }
        else{
            document.getElementById("zestimateArea").innerHTML = "Error!"
        }
    }

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

    return false;
}
Christine268
  • 722
  • 2
  • 13
  • 32
  • Can you `var_dump($zdata->response);`? – Machavity Feb 26 '16 at 03:12
  • @Machavity $vaddress=var_dump($zdata->response);echo $vaddress; Fails. I am fetching this with AJAX so its doing a 4 or 200 status, not really sure how I can get a more clear error message from the PHP side. – Christine268 Feb 26 '16 at 04:32
  • That's not how you [use var_dump](http://stackoverflow.com/questions/14743342/how-do-i-properly-use-print-r-or-var-dump). As far as the AJAX end, use the browser's console (F12) to show what the AJAX response is – Machavity Feb 26 '16 at 13:12
  • @Machavity I've never used var_dump and was just going off what you said, I wasn't aware that you gave me incorrect syntax in your comment. I will edit my post to show the error in the console, I had just assumed that since it was showing the error message I customized that it just meant it couldn't process and the reason would be on the PHP side, what it is sending over. – Christine268 Feb 27 '16 at 23:37
  • @Christine268 That isn't incorrect syntax in the comment, you just misinterpreted the syntax present. Nowhere in the noted comment does the OP of the comment assign the results of `var_dump` to a variable. – Daedalus Feb 27 '16 at 23:42
  • @Daedalus Well I clicked the link he provided in his next comment and I don't see why it *wouldn't* be able to be placed in a variable, a variable just holds data after all. If "not correct syntax" isn't the correct interpretation of the issue then again, I was just using it the only way I would know how, as he's the one who asked me to run it, assuming the way he typed it. Anyway, unless you have something to add to the topic, I do not see how your comment is relevant to my issue. All you are doing is pointing out something I interpreted wrong without even at least helping with that. – Christine268 Feb 27 '16 at 23:47
  • @Christine268 The answer on the linked post relays how to use the function. And even if that doesn't do it, always refer to the php manual. Besides that, what you list is not a php error, but a js error, and you haven't posted your js. – Daedalus Feb 27 '16 at 23:49
  • @Machavity $vaddress=var_dump($zdata->response->address->street); (because I still don't see why it *can't* be placed into a variable. But I can take it out if you like, whether in the variable or out: echo var_dump($zdata->response->address->street); I get the following error in my console: Uncaught SyntaxError: Unexpected token o. It shows the same error even when I try to put the syntax for the zestimate in there instead. – Christine268 Feb 27 '16 at 23:50
  • @Daedalus Why would there be something wrong with my JS when it is running successfully when I retrieve the zestimate and zpid just not when I try to do the address? Nothing indicates that the data is not being sent to the PHP or not being returned. It is only the street and other elements of the address that aren't working, but sure, I will edit and show the AJAX. – Christine268 Feb 27 '16 at 23:52
  • @Christine268 Please, humor me. It could easily be a problem with your ajax request; I've experienced the exact error message before, and I know what it means. – Daedalus Feb 27 '16 at 23:53
  • @Machavity Ok I apologize, when I still was doing a JSON parse on the AJAX response (for when I was trying to send multiple items back in an array before I knew the address wouldn't work the same) I was getting the error stated but now it shows: object(SimpleXMLElement)#6 (2) { ["@attributes"]=> array(1) { ["currency"]=> string(3) "USD" } [0]=> string(6) "152605" } Which shows the zesimate and object(SimpleXMLElement)#6 (1) { [0]=> string(23) "123 Street Dr " } which shows the Street, as resquested, but along with the rest of the clutter. What can I do with this? – Christine268 Feb 28 '16 at 00:01
  • @Christine268 The error you were experiencing is because the returned data was not JSON, and you were attempting to parse it as JSON. The data is actually a class instance of SimpleXMLElement, there should be a method in that instance to transform the data into bare text or JSON. – Daedalus Feb 28 '16 at 00:04
  • @Daedalus Well the Zestimate and ZPID still returned just fine before I realized I was still parsing it with JSON. So I didn't actually think anything of it, why would that be? It IS returning the street fine now though. But this all started because I WAS sending JSON when I was trying to do multiple elements (address and zestimate) but it wasn't working. So I took a step back and had confirmed the zestimate was still working individually and then tried the address individually. – Christine268 Feb 28 '16 at 00:07
  • @Daedalus well that is certainly good to know. But I am back to original problem. What is wrong with this? `$multiple=array($zestimate,$zaddress); echo json_encode($multiple);` When I switch the line of code in the AJAX to parse JSON. I can send those variables individually but not in an array as I am trying to do. Oh, and the variable in the AJAX instead of being `retrieve` I am doing `retrieve[0]`. I again get the error Uncaught SyntaxError: Unexpected token T – Christine268 Feb 28 '16 at 00:14
  • @Christine268 I deleted my comment, but I'm reposting the relevant part; a integer is valid json, an un-double quoted string is not: https://jsfiddle.net/Daedalus/fqq3o113/ ([relevant docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse)). – Daedalus Feb 28 '16 at 00:17
  • @Christine268 Post the results of `console.log(retrieve)` in the question. – Daedalus Feb 28 '16 at 00:20
  • @Daedalus I am placing `console.log(retrieve)` in right after I assign the variable in the AJAX (and before the innterHTML) but I am seeing nothing in the console log except for that error. Another thing I am not super familiar with using (probably bad as I know it can be ever useful). – Christine268 Feb 28 '16 at 00:28
  • @Daedalus I am just a hot mess when it comes to making my own problems. I still had another echo in the PHP that was going through instead of the json_encode one. The AJAX is retrieving (and in the console.log) I am getting the following: `[Object, Object]` – Christine268 Feb 28 '16 at 00:43
  • @Christine268 I'm going to assume both of those objects have a `>` kind of arrow next to them or something; click them and check their contents, assuming you're using Chrome as your browser. – Daedalus Feb 28 '16 at 01:04
  • @Daedalus I clicked on the arrow next to `[Object,Object]` (there was only the one to the left.. yes Chrome browser), and below is what it showed. `[Object, Object] 0: Object 1: Object length: 2 __proto__: Array[0]` I tested that the encoding and parsing is working fine by doing `$one='one';$two='two';$multiple=array($one,$two);echo json_encode($multiple);` and I was able to successfully get 'one' to show (console log showed (one, two). So it appears to be the Zillow data that isn't encoding/parsing correctly ? – Christine268 Feb 28 '16 at 01:10
  • It's all good, just doing `echo $zestimate.' '.$street.' '.$city.' '.$state.' '.$zip;` with a split on the JS side.. less of a headache. :D – Christine268 Feb 28 '16 at 02:41

1 Answers1

0

So when I went to post my AJAX as a last ditch effort for help I had seen I still had this line of code:

retrieve = JSON.parse(xmlhttp.responseText);

As Daedalus helpfully explained, this wasn't an issue when I was retrieving integers but posed a problem when I was retrieving text. I had originally put that line of code in when I was trying to retrieve both the Zestimate and the address together in an array encoded with JSON. When it was unsuccessful I took a step back to see if I could retrieve the address individually with no success. I never thought twice about that line of code since the AJAX still seemed to work fine.

Hence the perplexing outcome.

Changing that line back to:

retrieve = xmlhttp.responseText;

Allowed me to retrieve the address with success.

Don't you had simple mistakes that cause huge problems? Back to figuring out why the JSON encode and parsing wasn't working, but that's a question for a different post.

Christine268
  • 722
  • 2
  • 13
  • 32