Oct 27, 2009

Ebay API

Our group projects deals with the translation from a keyword, searching eBay and Amazon for this keyword and retranslating the title of the result before displaying it. In this post I like to introduce how we managed the ebay search and how we work on the ebay data.
The first to know is that ebay needs you to sign-up for their developer program. After signing-up, for free, you are able to get the developer-keys. These keys are necessary to connect to the ebay server. Ebay supports a webservice. We get XML data by using the proxy.
Now, I will explain our sourcecode:

## JavaScript
var url="proxyEbay.php?rk="+trans;


The url for the request consists of our proxy file and the translated keyword (trans).

## PHP
$rk = $_REQUEST['rk'];


The handed value is saved in the variable $rk.

http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.0.0&SECURITY-APPNAME=YOUR_AppID!!! &GLOBAL-ID=EBAY-US&keywords=YOUR_KEYWORD!!! &paginationInput.entriesPerPage=10';
This is the link to the ebay search. You have to change "YOUR_AppID!!!" to your ebay application id from the developers program and the "YOUR_KEYWORD!!!" with your keyword.

##PHP
$ur1 = 'http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.0.0&SECURITY-APPNAME=YOUR_AppID!!!&GLOBAL-ID=EBAY-US&keywords=';
$ur2 = '&paginationInput.entriesPerPage=10';
$uri =$ur1.$rk.$ur2;


We splitted the link into two parts to change the keyword.
The first part consists of the ebay function we use (findItemsByKeywords), the actual service version, our developer id. Please be aware that I do not publish our developer id. And the Ebay site we search.
The second part describes the entries that will be shown.
The $uri will be built from the first part, the translated keyword that we passed to the PHP file, $rk, and the second part with the help of the .methode.

The rest of the proxy is similar to the given one: With this url, we will start a curl-session and make the call.

The XML data we receive looks like this ( I shortend it. This is the XML header and only the first item)
<findItemsByKeywordsResponse xmlns:ms="http://www.ebay.com/marketplace/services" xmlns="http://www.ebay.com/marketplace/search/v1/services">
<ack>Success</ack>
<version>1.0.1</version>
<timestamp>2009-10-22T00:05:40.077Z</timestamp>
<searchResult count="10">

<item>
<itemId>220495266410</itemId>
<title>2 PEDROS PEDRO'S BICYCLES BIKES STICKERS, DECALS. NEW!!</title>
<globalId>EBAY-US</globalId>
<primaryCategory>
<categoryId>106953</categoryId>
<categoryName>Decals, Stickers</categoryName>
</primaryCategory>

<galleryURL>http://thumbs3.ebaystatic.com/pict/2204952664108080_1.jpg</galleryURL>
<viewItemURL>http://cgi.ebay.com/2-PEDROS-PEDROS-BICYCLES-BIKES-STICKERS-DECALS-NEW_W0QQitemZ220495266410QQcmdZViewItemQQptZCycling_Parts_Accessories</viewItemURL>
<paymentMethod>PayPal</paymentMethod>
<autoPay>false</autoPay>
<postalCode>27408</postalCode>
<location>Greensboro,NC,USA</location>
<country>US</country>
<shippingInfo>
<shippingServiceCost currencyId="USD">1.0</shippingServiceCost>
<shippingType>Flat</shippingType>
<shipToLocations>Worldwide</shipToLocations>
</shippingInfo>
<sellingStatus>
<currentPrice currencyId="USD">2.99</currentPrice>
<convertedCurrentPrice currencyId="USD">2.99</convertedCurrentPrice>
<bidCount>0</bidCount>
<sellingState>Active</sellingState>
<timeLeft>P0DT0H0M32S</timeLeft>
</sellingStatus>
<listingInfo>
<bestOfferEnabled>false</bestOfferEnabled>
<buyItNowAvailable>false</buyItNowAvailable>
<startTime>2009-10-15T00:06:12.000Z</startTime>
<endTime>2009-10-22T00:06:12.000Z</endTime>
<listingType>Auction</listingType>
<gift>false</gift>
</listingInfo>
</item>



Now it is easy to pick out the desired data. You can look manually for a specified tag by "getElementById("title")[0].childNodes[0].nodeValue" to get the tilte.
Or you can itarate over all found data. For the iteration you only need the count of the items:
test= request.responseXML;

for (i=0;i<test.getelementsbytagname("title").length;i++){>
title=test.getElementsByTagName("title")[i].childNodes[0].nodeValue;
}


We use this iteration to get the title retranslated into to users language.
I hope this will help someone with his project. If you have question or problems, feel free to contact me :)

No comments:

Post a Comment