Help with Zillow API, PHP, XML.

Does anyone have any links to examples, or have any personal examples of how to use the Get Demographics API from Zillow? I can handle working with PHP, but I really know nothing about XML. Any help at all???
LVL 1
websethAsked:
Who is Participating?
 
Ray PaseurConnect With a Mentor Commented:
Here is the section of code that first turns the string of XML into an object, and then extracts individual data elements from the object.

HTH, ~Ray
// EXAMINE THE RESULT RECEIVED IN XML FORMAT
// $y_buf IS A STRING OF XML INFORMATION - WE TURN IT INTO AN OBJECT
// $ydata IS A RESULTING OBJECT THAT CAN BE ACCESSED WITH OOP SYNTAX
$ydata  = new SimpleXMLElement($y_buf);
 
// EXTRACT INFORMATION FROM THE OBJECT AND ASSIGN IT TO LOCAL VARIABLES
// NOTE THE ARROW SYNTAX USED TO POINT INSIDE THE OBJECT
$y_qual  = $ydata->Result["precision"];
$y_warn  = $ydata->Result["warning"];
$y_lat   = $ydata->Result->Latitude;
$y_lon   = $ydata->Result->Longitude;
$y_state = $ydata->Result->State;
$y_zip   = $ydata->Result->Zip;
 
// NOW WE CAN WORK WITH THE LOCAL VARIABLES
echo "<br />The State is $y_state!";

Open in new window

0
 
Ray PaseurCommented:
PHP5?  You probably want to use SimpleXML to extract the elements.  I can show you a sample using the Yahoo Geocoder.  The principles are the same - you just need to change the XML fields. ~Ray
<?php // RAY_yahoo_geocoder.php
 
// LOCAL FUNCTIONS, KEYS AND CODES
require_once('_config.php');
 
// GET A FREEFORM LOCATION STATEMENT
$loc = '';
foreach ($_GET as $k => $v) { $$k = get_clean_text_string($v); }
if ($a != '') { $loc .= $a . ', '; }
if ($c != '') { $loc .= $c . ', '; }
if ($s != '') { $loc .= $s . ', '; }
if ($z != '') { $loc .= $z . ', '; }
$loc = trim(substr($loc, 0, -2));
 
?>
<html><head><title><?=$a?> <?=$c?> <?=$s?> <?=$z?> Yahoo/Google Geocoder Query</title></head>
<body>
<form method="get">
Addr: <input type="text" name="a" autocomplete="off" value="<?=$a?>" />
City: <input type="text" name="c" autocomplete="off" value="<?=$c?>" />
ST:   <input type="text" name="s" autocomplete="off" value="<?=$s?>" size="2" />
Zip:  <input type="text" name="z" autocomplete="off" value="<?=$z?>" size="8" />
<input type="submit" name="_go" value="go" />
</form>
</body>
</html>
<?php
 
 
if (empty($_GET)) { die(); }
 
 
// YAHOO CALL
$yahoo_url	= "http://local.yahooapis.com/MapsService/V1/geocode?appid=" . YAHOO_API;
 
/* NOTE YOU CAN ASK FOR INDIVIDUAL PIECES OF AN ADDRESS
$yahoo_url	.= "&street=" .	urlencode($a);
$yahoo_url	.= "&city=" .	urlencode($c);
$yahoo_url	.= "&state=" .	urlencode($s);
$yahoo_url	.= "&zip=" .	urlencode($z);
*/
 
// YOU CAN MAKE A FREEFORM QUERY FROM THE PARTS OF AN ADDRESS
$yahoo_url	.= "&location=" . urlencode($loc);
 
// SHOW THE QUERY - DO NOT DISPLAY THE API KEY
$yahoo_no_api_url	= str_replace(YAHOO_API, "[YahooAPI]", $yahoo_url);
echo "<h2>Yahoo! Geocoder Query Returns XML</h2>\n";
echo "<h4>ManPage here: <a target=\"_ymp\" href=\"http://developer.yahoo.com/maps/rest/V1/geocode.html\">http://developer.yahoo.com/maps/rest/V1/geocode.html</a><br />\n";
echo "Accuracy Constants here: <a target=\"_ymp\" href=\"http://developer.yahoo.com/maps/rest/V1/geocode.html\">http://developer.yahoo.com/maps/rest/V1/geocode.html</a> (see Response Fields)</h4>\n";
echo "Query String: $yahoo_no_api_url <br />\n";
 
// EXECUTE YAHOO GEOCODER QUERY
// NOTE - USE ERROR SUPPRESSION OR IT WILL BARF UP THE YAHOO API - ON FAILURE RETURNS HTTP 400 BAD REQUEST
if ($fp = @fopen($yahoo_url, 'r')) {
	while (!feof($fp)) { $y_buf .= fgets($fp); }
	fclose($fp);
} else {
	echo "<br />Yahoo Query Failed\n";
}
 
// EXAMINE THE RESULT
if ($y_buf != '') { // NOT EMPTY, WE GOT DATA
	$ydata	= new SimpleXMLElement($y_buf);
 
// CHECK FOR ANY ERROR MESSAGE
	$y_err	= $ydata->Message;
	if ($y_err == '') {
		$y_qual	= $ydata->Result["precision"];
		$y_warn	= $ydata->Result["warning"];
		$y_lat	= $ydata->Result->Latitude;
		$y_lon	= $ydata->Result->Longitude;
		$y_state= $ydata->Result->State;
		$y_zip	= $ydata->Result->Zip;
 
// NO ERROR MESSAGE, SHOW THE RESULT
		echo "<br />Yahoo Geocoder Result: \n";
		echo "<a target=\"geo_Y\" href=\"http://www.google.com/maps?f=q&hl=en&geocode=&q=" . $y_lat .','. $y_lon . "&ie=UTF8&z=15&iwloc=addr\">$y_lat $y_lon</a><br />Precision=$y_qual \n";
		if ($y_warn != '') {
			echo "<br />Warning: $y_warn \n";
			if ($y_state != $s) {
				echo "<br />Input State $s does not match Yahoo State $y_state.  Google Data may be better. \n";
			}
		}
	} else {
		echo "<br />Yahoo Geocoder Error: \n";
		echo "$y_err \n";
	}
// NO RESULT - SOMETHING IS SICK AT YAHOO
} else {
	echo "<br />Yahoo! Fubar $y_buf \n";
}
 
// SHOW THE XML FROM YAHOO
$y_buf_p	= ereg_replace('>', ">\n", $y_buf);
echo "<pre>";
echo htmlentities($y_buf_p);
echo "</pre>\n";
 
?>

Open in new window

0
 
websethAuthor Commented:
thank you for the comment....i will test it later this pm.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Ray PaseurCommented:
Here is the Zillow man page.  The processing area in my script is near line 62 in the snippet above.

http://www.zillow.com/howto/api/GetDemographics.htm

Best regards, ~Ray
0
 
websethAuthor Commented:
OK, using the sample query from Zillow, changing a few things in the code, it appears that it's processing correctly:

http://www.level10realty.com/api/index.php

Now....how in the world do I use the XML variables? Based on what you said, it looks like it will start at line 68 on your code snippet, but I'm not sure where to start. Can you give me a quick example? Once I get the variables, I know what to do with them.....but the XML is killing me......

Thank you again.
0
 
websethAuthor Commented:
Line 62, not 68, sorry!
0
 
Ray PaseurCommented:
Yeah, the XML is not very intuitive.  Here are the basics from my example.  Also, read the man page here:
http://us.php.net/manual/en/function.simplexml-element-construct.php

First of all, this is what I get back from the Yahoo Geocoder request.

Next I will post the commented code.
<?xml version="1.0"?>
<ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:maps" xsi:schemaLocation="urn:yahoo:maps http://api.local.yahoo.com/MapsService/V1/GeocodeResponse.xsd">
<Result precision="address">
<Latitude>
38.931399</Latitude>
<Longitude>
-77.147662</Longitude>
<Address>
1446 Colleen Ln</Address>
<City>
McLean</City>
<State>
VA</State>
<Zip>
22101-3104</Zip>
<Country>
US</Country>
</Result>
</ResultSet>

Open in new window

0
 
Ray PaseurCommented:
BTW, you can use var_dump() to print out the contents of a SimpleXMLElement object.
0
 
websethAuthor Commented:
PERFECT! I understand NOW! Thank YOU~!
0
 
Ray PaseurCommented:
Excellent.  I always find an example is better than a thousand lines of theory - visual learner, I guess!

Glad I could help, ~Ray
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.