Simple API Searching Example

It’s been almost a year since we gave you some introductory tips on getting started with the eBay API, so we thought it would be a good idea to encourage those of you who haven’t yet taken the plunge by publishing a simple step by step guide. In this post, we’ll show you how to create a simple search page, and hopefully by the end, you’ll be able to produce something like this mini site. So let’s get started... API Image

Step 1 Get signed up to the eBay Developers Program. You will be assigned an AppID which will look something like ABCDE12f3-gh45-67i8-9012-jk345lm6789. This is your unique key to access the API.

Step 2 Construct your API call. We’ll use the findItemsAdvanced call in the Finding API. The call is made using a URL. By adding in extra parameters to the URL, you can apply any of the search options and filters that are available if you were searching through the main eBay sites. The following example URL, when put together will return search results for the term iPod Nano with fixed priced items only from the UK site, in XML. Try it out, but remember to put in your own AppID after ‘SECURITY-APPNAME=’ &SECURITY-APPNAME= ABCDE12f3-gh45-67i8-9012-jk345lm6789 &RESPONSE-DATA-FORMAT=XML &REST-PAYLOAD=true &GLOBAL-ID=EBAY-GB &paginationInput.entriesPerPage=5 &paginationInput.pageNumber=1 &keywords=iPod+Nano &itemFilter(0).name=ListingType &itemFilter(0).value(0)=FixedPrice

Step 3 Now you need to take the response and turn it into something useful on your site – this is the tricky part! The API can return data in several formats – XML, JSON, Name Value pairs and SOAP. In this example, we’ll use PHP to render an XML response. Here is the source code for the example search page. You can also download the complete page with CSS and images here. Grab it and a play around with the code, page layout and stylesheet.

This script will construct a findItemsAdvanced API call, as detailed above, and then use a PHP function to run through each item within the returned XML in a loop. This loop repeats until it reaches the last product within the XML. Each time the loop runs, it extracts the relevant information within the listing, and adds it as a row to a HTML table, which is then printed out at the end. Download it and have a play around, but remember to insert your AppID in the indicated place, otherwise it won’t work!

Step 4 Affiliatise your links. The developers program is not automatically connected to your EPN account. In order to do this, you’ll need to add some extra parameters onto the call URL: &affiliate.trackingId=[yourcampaignid] &affiliate.networkId=9 &affiliate.customId=[customid]

Check out this section of the call reference for more information on adding the affiliate parameters.

Hopefully the example code will help you to better understand how to implement the eBay API, and provide a good starting platform for you to build extra functionality into your site.

James Skelland, Technical Solutions Manager at R.O.EYE