eBay API: First Steps

    As you know, starting to learn something is most difficult, the eBay API, even though it has normal documentation, is no exception. I myself just recently began to study it and had to climb tightly on the dock and third-party resources before the desired picture began to take shape in my head.
    This post is intended for those who need to quickly start working with the eBay API and for myself, so as not to forget anything in the future.


    Quick start guide


    In principle, eBay has the so-called “ Quick Start Guide ”, so let's go through it first:
    1. Register a new developer - all requests to the API are accompanied by the transfer of some unique developer parameters. How to get them, read the second paragraph;

    2. After successfully registering and authorizing with developer.ebay.com, you can begin to generate the “Application Keys” that you will need to complete your API requests. 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. This clause refers to " Sample Application", with the help of 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 APIs "(I will write more about the main types of APIs below). On the page of this" Sample Application "there are 3 block:
    3.1 Sample Application Source Code - contains HTML / JS code, at the very bottom of which a JS lib is connected, the URL to which is the "Finding API" URL with the necessary parameters already substituted;
    3.2 Parsed Sample Result - contains the query result in the form of HTML pages;
    3.3 of Call Response - contains the result of the query in the form of the XML.

    4. References to sections devel Perov grouped by programming languages.

    Product API


    1. Finding API - designed to get a list of eBay products. This API provides the ability to search for 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 for categories and keywords;
    1.4 findItemsByProduct - search for products by product identifiers. Products may include, for example: phones, video games, books, etc.

    2. Shopping API- mainly intended to obtain detailed information about the product. To get 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 requests:
    2.1 GetSingleItem - returns all public information about one product by its ID;
    2.2 GetItemStatus - returns information about current product rates; 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 code of the country to which delivery will be made, the postal code of the buyer and the number of units of goods that will be / were purchased. Before fulfilling this request, you first need to make sure that the product has a shipping 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 have chosen a format for communicating with api via XML, I send requests to api using the POST method. But first, I’ll give you 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 about 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 " My Account " section .

    Both of these parameters, when using the POST method, must be passed 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 a product search using the “findItemsByKeywords” action. Where: 1. Http_Client is my small class for sending requests to api using cURL. It is in the code archive; 2. The request URL will look like this: svcs.sandbox.ebay.com/services/search/FindingService/v1 , but since the dock says that “v1” (api version) can change, I put it into a constant; 3. The setHeaders () method sets the request headers. The APP_ID constant stores my AppID; 4. The setBody () method sets the request body. It’s not difficult to guess in the keywords tag we pass the keywords. The entriesPerPage parameter tells api that we need to return 10 items (more about findItemsByKeywords action parameters can be found here

    // 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(
    '

    ' . htmlspecialchars($keywords) . '

    10

    '
    );

    // Send request
    $result = $httpClient->send();









    );

    5. Well, the send () method sends a request to api and returns its response, which will be placed in the $ result variable in the form of XML.

    Shopping API



    I will supplement my small application with the ability to view extended product information using the GetSingleItem Shopping API action requests. 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 - api request format. For the POST method, you must specify "XML";

    4. X-EBAY-API-VERSION - The api version that your application supports. For tests, I specified 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: 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 a request body. In the request body, I indicated the product identifier in the ItemID tag (by the way, I do not recommend casting it to int type, because the size of the product ID on eBay has long exceeded its size) and as additional data I requested TextDescription (a text description of the product, more about all the GetSingleItem action parameters can be read here ).

    // 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(
    '

    ' . $id . '
    TextDescription
    '
    );

    // Send request
    $result = $httpClient->send();




    Total



    I hope that this post will allow someone to reduce the time to familiarize themselves with the eBay API.

    My test application archive
    URL listing page for all APIs

    Also popular now: