Surprise! It’s me and the Trulia API in PHP
By Robbie Paplin | August 13, 2007
When Drew told me of GeekEstate, I was told I could blog about anything geeky & real estate related. He said I could even blog about the Trulia API, if I wanted to. So I decided to take him up on the offer.
In the spirit of surprises, I decided not only to write a Trulia API blog post tutorial (on a blog founded by Zillow). But, to make it more surprising, I decided to write it in PHP (if you follow my writings on Rain City Guide, you should know I have a strong preference for ASP.net).
Since I’m new to PHP (but pretty familiar with web programming concepts), I decided to purchase a good PHP refernce book, download a good PHP IDE, roll up my sleeves and get to work. If the rest of this post is off the deep end, don’t worry about it. PHP web service programming is what I would call an advanced topic. And if you are a professional PHP programmer, be gentle, I’m a PHP virgin. Anyway, since most of us are new to PHP programming, I decided that writing an API demo in the same language that WordPress is developed in might make my tutorial more useful for the geeky RE.net folks among us. Besides, learning a little PHP never hurt anybody.
In order to use the Trulia API, you first need to get a Trulia user account and then you to register for an account key. After you have your account key, then the fun can begin. The Trulia API is available via a REST Web Service, so it’s pretty familiar if you’ve dealt with XML based web services before. For my tutorial, I decided to create a function that would create a market activity report for a given city. The final result looks something like this…
| Market Activity for San Francisco, CA | ||
| Week Ending Date |
Number Of Properties |
Median Listing Price |
| 2007-07-14 | 1,059 | $799,000 |
| 2007-07-21 | 1,068 | $801,667 |
| 2007-07-28 | 1,042 | $807,667 |
| 2007-08-04 | 902 | $801,929 |
| 2007-08-11 | 882 | $792,393 |
To generate this table I needed to call the TruliaStats.getCityStats web service. The Trulia APIs are different from the Zillow APIs in that they usually return a lot of raw data. The upside of this approach is that the developer has a lot more flexibility on how they want to combine or visualize the results of the web service calls. The downside with this approach is that visualizing the data in cool ways is a fair amount of work. In general, the Zillow API trades off flexibility and power for ease of use (so you can do more interesting things with Trulia API like Truliaholic and other applications, but getting a simple chart with the Zillow API is easier).
Without further ado, here’s the Trulia API / PHP sample code I wrote to create the above table… Just copy and paste it into a PHP page, replace the value of the $apikey variable in the marketwatch function with your API key, and you should be golden.
The code is pretty basic. The first couple lines creates the url used to make the web service call. The next several lines puts the HTTP response from the web service API into an XML DOM. The last several lines traverses the XML DOM, and creates a nice HTML table. If you want a table for a different city or for a different duration, just change the marketwatch function call at the bottom of the page.
Anyway, if any of you are considering using the Trulia API for your web site, I’d recommend you follow their terms of use and post any questions or suggestion you have on their forums. Happy coding!
Author's Website: http://www.caffeinatedsoftware.com
7 Responses to “Surprise! It’s me and the Trulia API in PHP”
Comments
« Exploding Myths: Part 1. Web Traffic - “Hit” Me With Your Best Shot | Home | Carnival of Real Estate #54 »







August 13th, 2007 at 12:09 pm
I should note that my example code appears to require PHP 5. (Due to my usage of the SimpleXML extension, which apparently doesn’t come with PHP 4).
I thought I would pass that on in case anybody else tries to run this on PHP 4 and gets a “parse error, unexpected T_OBJECT_OPERATOR” error.
Sorry about that…
August 13th, 2007 at 2:06 pm
Robbie-
Now, you’ll just have to write a plug-in using the Zillow API
We have an API addition coming that will give you some more data to work with.
August 13th, 2007 at 2:25 pm
Robbie, I bow to your geekness! I’ll have to give this a whirl, thanks!
August 13th, 2007 at 3:21 pm
Well done! Now all we need is a Zillow-Trulia mashup that uses both APIs. I built one on my home server but it’s too ugly for public consumption…
August 13th, 2007 at 7:36 pm
Hi Robbie,
I got a geeky boy alert and headed over. I don’t see much Seattle Data in Trulia. Is there enough to provide any kind of tables in our area?
August 13th, 2007 at 9:14 pm
Ardell,
It appears that Trulia has about 17-25% of the inventory that the NWMLS has for the greater Seattle area and the listings that they do have are usually lower in price than the full inventory from the “real thing”. I guess brokers with expensive listings don’t advertise on Trulia much.
Anyway, I’ve e-mailed you the “Trulia tables” for Seattle, Redmond, Kirkland, and Bellevue, for your reading enjoyment.
June 4th, 2008 at 8:18 pm
Ok … I am so lost with the coding talk. I think I will stick to using widgets that the programmers like you create.