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

Search Real Estate and Homes for Sale

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!