As you know, starting to learn something is the hardest, the eBay API, even though its normal documentation is no exception. I myself only recently began to study it, and I had to crawl tightly around the dock and third-party resources before the necessary picture began to form in my head.
This post is intended for those who need to quickly start working with the eBay API and for myself, so that in the future nothing is forgotten.
Quick start guide
In principle, eBay has a so-called “
Quick Start Guide ”, so let's go through it first:
1.
Registration of a new developer - all requests to the API are accompanied by the transfer of some unique parameters of the developer. How to get them read in the second paragraph;
2. After successful registration and authorization on developer.ebay.com, you can proceed to generating the “Application Keys”, which you will need to fulfill requests to the API. To do this, go to the "
My Account " section and in the "Application Keys" block, generate 2 sets of keys:
2.1 Sandbox Keys - keys for requests to the test API;
2.2 Production Keys - keys for requests to the working API.
')
3. In this paragraph, a link is given to "
Sample Application ", with which you can test requests to one of the types of APIs (yes, as it turned out there are a lot of them :)) called "Finding API" (I will write more about the main types of API below). On the page of this “Sample Application” there are 3 blocks:
3.1 Sample Application Source Code - contains HTML / JS code, at the very bottom of which JS lib is connected, the URL of which is the “Finding API” URL with the necessary parameters already filled in;
3.2 Parsed Sample Result - contains the result of the query in the form of an HTML page;
3.3 Call Response - contains the result of the query in the form of XML.
4. Links to sections of developers grouped by programming languages.
API for working with products
1.
Finding API - designed to get a list of eBay products. This API provides the ability to search products by the following criteria:
1.1 findItemsByCategory - search for products by category;
1.2 findItemsByKeywords - search for products by keywords;
1.3 findItemsAdvanced - allows you to create a mixed search by category and keyword;
1.4 findItemsByProduct - search for products by product IDs. Products can be for example: phones, video games, books, etc.
2.
Shopping API - mainly intended to obtain detailed information about the product. To obtain any information about a product, you need to know its ID, which can be obtained from the list of found products in response to a request to the Finding API. This API allows you to perform the following queries:
2.1 GetSingleItem - returns all public information about a single product by its ID;
2.2 GetItemStatus - returns information about the current rates of goods, at the same time you can request information about 10 products;
2.3 GetShippingCosts - allows you to calculate the cost of delivery of goods to the buyer. For the calculation, you need to transfer the product ID, the country code to which the delivery will be made, the buyer's zip code and the number of units of goods that will be / were purchased. Before executing this request, you first need to make sure that the goods bear the delivery price;
2.4 GetMultipleItems is the same as GetSingleItem but you can request information about several products at once (maximum 20 products).
Finding API
Consider working with this api based on the simplest example. I chose for myself the format of communication with api via XML, I send requests to api using the POST method. But first, here’s a list of required parameters:
1. X-EBAY-SOA-OPERATION-NAME - the value of this parameter should be the name of the action you are going to perform. For example: findItemsByKeywords, findItemsByCategory;
2. X-EBAY-SOA-SECURITY-APPNAME - in this parameter you need to substitute your AppID, which can be found / generated in the section "
My Account ".
Both of these parameters, when using the POST method, must be sent in the request header. You can read more about the general parameters for the Finding API on the
Making an API Call page.
Now let's look at an example of product search using the “findItemsByKeywords” action.
// Get entity of http client
$httpClient = new Http_Client('http://svcs.sandbox.ebay.com/services/search/FindingService/' . FINDING_API_VERSION);
// Prepare headers
$httpClient->setHeaders(
array(
'X-EBAY-SOA-OPERATION-NAME: findItemsByKeywords',
'X-EBAY-SOA-SECURITY-APPNAME: ' . APP_ID
)
);
// Prepare body
$httpClient->setBody(
'<?xml version="1.0" encoding="utf-8"?>
<findItemsByKeywordsRequest xmlns="http://www.ebay.com/marketplace/search/v1/services">
<keywords>' . htmlspecialchars($keywords) . '</keywords>
<paginationInput>
<entriesPerPage>10</entriesPerPage>
</paginationInput>
</findItemsByKeywordsRequest>'
);
// Send request
$result = $httpClient->send();
Where:
1. Http_Client - my small class for sending requests to api using cURL. It is in the code archive;
2. The request URL will look completely like this:
svcs.sandbox.ebay.com/services/search/FindingService/v1 , but since it is written in the dock that “v1” (api version) can change, I rendered it to a constant;
3. The setHeaders () method sets the request headers. The constant APP_ID stores my AppID;
4. The setBody () method sets the request body. As it is not difficult to guess in the keywords tag, we pass on keywords. The entriesPerPage parameter tells api that we need to return 10 products (for more details about the findItemsByKeywords action parameters, see
here );
5. Well, the send () method sends a request to api and returns its response, which in the form of XML will be placed in the $ result variable.
Shopping API
I will supplement my small application with the ability to view extended product information using requests to the GetSingleItem Shopping API action. First I will list the necessary parameters:
1. X-EBAY-API-CALL-NAME - the parameter sets the name of the action within the Shopping API. For example: GetSingleItem, GetItemStatus;
2. X-EBAY-API-APP-ID - the parameter passes your AppID;
3. X-EBAY-API-REQUEST-ENCODING - request format to api. For the POST method, you need to specify "XML";
4. X-EBAY-API-VERSION - the version of api that your application supports. For tests, I indicated 699.
All parameters, when using the POST method, must be passed in the request header. You can read more about the general parameters for the Shopping API on the
Making an API Call page.
Here is my small code that requests detailed information on the product:
// Get entity of http client
$httpClient = new Http_Client('http://open.api.sandbox.ebay.com/shopping');
// Prepare headers
$httpClient->setHeaders(
array(
'X-EBAY-API-CALL-NAME: GetSingleItem',
'X-EBAY-API-APP-ID: ' . APP_ID,
'X-EBAY-API-REQUEST-ENCODING: ' . SHOPPING_API_DATA_FORMAT,
'X-EBAY-API-VERSION: ' . SHOPPING_API_VERSION
)
);
// Prepare body
$httpClient->setBody(
'<?xml version="1.0" encoding="utf-8"?>
<GetSingleItemRequest xmlns="urn:ebay:apis:eBLBaseComponents">
<ItemID>' . $id . '</ItemID>
<IncludeSelector>TextDescription</IncludeSelector>
</GetSingleItemRequest>'
);
// Send request
$result = $httpClient->send();
Here, as in the previous example, I set the URL, for the Shopping API test area it will be “
open.api.sandbox.ebay.com/shopping ”. Then I form an array of headers and the body of the request. In the request body, I indicated the product identifier in the ItemID tag (by the way, I do not advise to convert it to int type, since the size of the product ID on eBay has long passed for its size) and I requested TextDescription as additional data (a text description of the product, more about all the parameters of the GetSingleItem action can be read
here ).
Total
I hope that this post will allow someone to reduce the time to become familiar with the eBay API.
Archive of my test applicationA page with a list of URLs for all APIs.