Search code examples
phpmysqlsdkebay-api

How to make joined calls using Ebay SDK using davidtsadler/ebay-sdk-php?


Before asking the question, I'm an admitted novice with the SDK and all of it is a struggle for me right now, in addition, I think I may not fully understand the policies/limits of Ebay yet. I'm not sure what is "allowed" or "proper" so I don't get blocked for improper use (like too many calls or something like that).

The question: Can you call for a request inside a loop of another request, similar to MySQL/PHP when you first request the ID's and them loop over them to return the details?

Example: I would want to look up targeted ebay-motors sellers, return a set number or keyword search group of listings from those sellers. (I believe the SDK handles this as one request - ItemFilter SellerID / Keywords)

Then, for each listing I'd want the listed compatible vehicles for each listing (this being the "2nd" loop/request per listing).

This is my "logic" (or lack there of) to get the result I want. If I can't use loops but I can "join" listing to compatibles like a spreadsheet might look, that would work too.

//Two responses?..one from each request
$response = $service->findItemsAdvanced($request);

// how to get compatibles from item id in request/response 1 ?//
$response2 = $service-> /* ??? */  ($request2);


// Iterate over the items returned in the response.
foreach ($response->searchResult->item as $item) {
    //an easy var name for reference
    var mylistId = $item->itemId, 
    // lets the see the ID's //
    printf(  
        "(%s) %s\n",
        $item->itemId,
        $item->title
    );

   //maybe the request and response is in the loop???
   // $requestTWO = get compatibles linked to mylistId
   // $responseTWO = return compatibles
   foreach ($responseTWO->searchResult->item as $compats) {
       // print new responses
       printf(  
        "(%s) %s\n",
        $compats->make, 
        $compats->model,
        $compats->year
    );
} 

This seems like overkill to have a new request for more details.


Solution

  • The Finding service does not return the compatibility information that you need. For each item that is returned you will have to make a separate call to GetSingleItem in Shopping service.

    <?php
    require __DIR__.'/vendor/autoload.php';
    
    use \DTS\eBaySDK\Sdk;
    use \DTS\eBaySDK\Constants;
    use \DTS\eBaySDK\Finding;
    use \DTS\eBaySDK\Shopping;
    
    $sdk = new Sdk([
        'credentials' => [
            'devId' => 'DEV ID',
            'appId' => 'APP ID',
            'certId' => 'CERT ID',
        ],
        'globalId'    => Constants\GlobalIds::MOTORS
    ]);
    
    /**
     * Create the service objects.
     */
    $finding = $sdk->createFinding([
        'apiVersion' => '1.13.0'
    ]);
    
    $shopping = $sdk->createShopping([
        'apiVersion' => '981'
    ]);
    
    /**
     * Create the finding request.
     */
    $findingRequest = new Finding\Types\FindItemsAdvancedRequest();
    /**
     * Ask for items from these sellers. You specify up to 100 sellers.
     */
    $itemFilter = new Finding\Types\ItemFilter();
    $itemFilter->name = 'Seller';
    $itemFilter->value = [
        'brakemotive76',
        'primechoiceautoparts'
    ];
    $findingRequest->itemFilter[] = $itemFilter;
    /**
     * You can optionally narrow the search down further by only requesting
     * listings that match keywords or categories.
     */
    //$request->keywords = 'Brake Pads';
    //$request->categoryId = ['33560', '33561'];
    
    /**
     * eBay can return more than one page of results.
     * So just start at page 1 to begin with.
     */
    $findingRequest->paginationInput = new Finding\Types\PaginationInput();
    $pageNum = 1;
    
    do {
        $findingRequest->paginationInput->pageNumber = $pageNum;
    
        $findingResponse = $finding->findItemsAdvanced($findingRequest);
    
        // Handle any errors returned from the API.
        if (isset($findingResponse->errorMessage)) {
            foreach ($findingResponse->errorMessage->error as $error) {
                printf(
                    "%s: %s\n\n",
                    $error->severity=== Finding\Enums\ErrorSeverity::C_ERROR ? 'Error' : 'Warning',
                    $error->message
                );
            }
        }
    
        if ($findingResponse->ack !== 'Failure') {
            /**
             * For each item make a second request to the Shopping service to get the compatibility information.
             */
            foreach ($findingResponse->searchResult->item as $item) {
                $shoppingRequest = new Shopping\Types\GetSingleItemRequestType();
                $shoppingRequest->ItemID = $item->itemId;
                /**
                 * We have to tell the Shopping service to return the comaptibility and item specifics information as
                 * it will not by default.
                 */
                $shoppingRequest->IncludeSelector = 'ItemSpecifics, Compatibility';
    
                $shoppingResponse = $shopping->getSingleItem($shoppingRequest);
    
                if (isset($shoppingResponse->Errors)) {
                    foreach ($shoppingResponse->Errors as $error) {
                        printf(
                            "%s: %s\n%s\n\n",
                            $error->SeverityCode === Shopping\Enums\SeverityCodeType::C_ERROR ? 'Error' : 'Warning',
                            $error->ShortMessage,
                            $error->LongMessage
                        );
                    }
                }
    
                if ($shoppingResponse->Ack !== 'Failure') {
                    $item = $shoppingResponse->Item;
    
                    print("\n$item->Title\n");
    
                    if (isset($item->ItemSpecifics)) {
                        print("\nThis item has the following item specifics:\n\n");
                        foreach ($item->ItemSpecifics->NameValueList as $nameValues) {
                            printf(
                               "%s: %s\n",
                                $nameValues->Name,
                                 implode(', ', iterator_to_array($nameValues->Value))
                            );
                        }
                    }    
    
                    if (isset($item->ItemCompatibilityCount)) {
                        printf("\nThis item is compatible with %s vehicles:\n\n", $item->ItemCompatibilityCount);
    
                        foreach ($item->ItemCompatibilityList->Compatibility as $compatibility) {
                            foreach ($compatibility->NameValueList as $nameValues) {
                                if ($nameValues->Name != '') {
                                    printf(
                                        "%s: %s\n",
                                        $nameValues->Name,
                                        implode(', ', iterator_to_array($nameValues->Value))
                                    );
                                }
                            }
                            printf("Notes: %s \n", $compatibility->CompatibilityNotes);
                        }
                    }
                }
            }
        }
    
        $pageNum += 1;
    
    } while ($pageNum <= $findingResponse->paginationOutput->totalPages);