PHP5 Example Using PEAR to Make XML-RPC Easy
For the XML-RPC functionality in our sample code, we'll be using the PEAR XML-RPC package. PEAR is an incredibly helpful library of PHP packages that you can install on top of your standard PHP setup.
- For more information on PEAR, see this PEAR Introduction.
- See our FAQ for more information on how to install the PEAR XML-RPC package with PEAR.
Oodle API PHP5 Class
Abstracting the Oodle API connection details away into a separate file will make things much easier for you. It'll make your code cleaner and it will make our sample code here cleaner as well. Fortunately, if you're using PHP5, we've already created an Oodle API PHP5 class that you are free to use:
Sample API Request
Let's get started with a simple query. We'll query for "2006 Toyota Prius" in the "San Francisco" region. To do that, call the
get() method and pass in the following parameters:
{
partner_id = 'TEST',
region = 'sf',
q = '2006 Toyota Prius'
}
Code
<?php
// use the Oodle API PHP5 class
require_once('oodle_api_php5.php');
// instantiate a new OodleApi object from that class
$oodleApi = new OodleApi();
// prepare a "get()" method call to the Oodle API
$method = 'get';
$params = array(
'partner_id' => 'TEST',
'region' => 'sf',
'q' => '2006 Toyota Prius'
);
// make the request
$listings = $oodleApi->make_request($method,$params);
// use PHP's "print_r" to dump the entire data structure we got back
print_r($listings);
?>
That's it! Just a dozen lines or so of actual code.
Sample API Response
The response to the Oodle API call above will contain:
- "items" - an array of items that match the query
- "total" - an integer representing how many total results Oodle has for that query
Here's how it might look:
Array
(
[items] => Array
(
[0] => Array
(
[title] => 2006 TOYOTA PRIUS Mileage 16341
[location] => Hayward
[zipcode] => 94544
[color] => Grey
[condition] => Used
[make] => Toyota
[mileage] => 16341
[model] => Prius
[price] => 23981.0
[privateparty] => 0
[sellertype] => Dealer
[year] => 2006
[itemlocation] => usa_ca_hayward
[url] => http://www.oodle.com/rx/536639749-648p454-...
[source] => AutoGuide.com
[category_attr] => vehicle/car
[category_name] => Cars
[id] => 536639749
[thumb] => http://oodleimg.com/item/536639749_1s.jpg
[image_width] => 112
[image_height] => 84
[image_alt] => 2006 TOYOTA PRIUS Mileage 1634
[create_time] => 1183013747
[has_photo] => Thumbnail
[latitude] => 37.6338005
[longitude] => -122.0618973
[paid] => 1
)
[1] => Array
(
[title] => 2006 Toyota Prius
[body] => 2006 TOYOTA PRIUS HYBRID, ac, ps, pdl, cc, ...
[location] => Santa Clara
[zipcode] => 95054
[condition] => Used
[make] => Toyota
...
)
)
[total] => 27
}
Clearly the response above is just a dump of the data, but you can see what's going on -- in this case, it's an array of items. Each individual element of the array represents a single listing. Listings come with a rich hash of listing data.
It wouldn't take much work at all to layer a UI on top of this data, but we'll leave that up to you and the custom needs for your project!
The goal here is demonstrate that you can request listings from the Oodle API with just a small chunk of code. It's really quite simple once you get the XML-RPC plumbing working, and even that isn't too tough.
Build Up From There!
In the example above, we only sent in three parameters:
{
partner_id = 'YOUR-API-KEY',
region = 'sf',
q = '2006 Toyota Prius'
}
You can do much more complex queries by passing in more parameters on the get() call -- for example, you could:
- use dimensions to limit by a certain color
- use filters to pare down by a specific price range
- sort the listings by create time or by distance from a particular location
All of those possibilities are described on the documentation page for the
get() method.
Good luck!