River
River

Reputation: 83

How to fetch my listings of product from eBay?

What I'm trying to do is to simply fetch my listings from eBay to my example site, I used __GetSellerItem__ http://developer.ebay.com/DevZone/XML/docs/reference/ebay/GetSellerList.html.

But I guess I'm lacking on how to output or show it in my site. And if you seem to find my codes have some errors please feel free to say so. I'm open for your considerations.

//show all errors - useful whilst developing
error_reporting(E_ALL);

require 'vendor/autoload.php';

use \DTS\eBaySDK\Constants;
use \DTS\eBaySDK\Trading\Services;
use \DTS\eBaySDK\Trading\Types;

// Create the service object.
$service = new Services\TradingService(array(
    'apiVersion' => '863',
    'sandbox' => true,
    'siteId' => Constants\SiteIds::US
));

// Create the request object.
$request = new Types\GetSellerListRequestType();
$request->RequesterCredentials = new Types\CustomSecurityHeaderType();
$request->RequesterCredentials->eBayAuthToken = 'I have this one';

// Send the request to the service operation.
$response = $service->getSellerList($request);

Upvotes: 0

Views: 1703

Answers (1)

user2066189
user2066189

Reputation:

I've expanded your example code so that it will paginate through the items returned from the GetSellerList operation.

require __DIR__.'/vendor/autoload.php';

$config = require __DIR__.'/configuration.php';

use \DTS\eBaySDK\Constants;
use \DTS\eBaySDK\Trading\Services;
use \DTS\eBaySDK\Trading\Types;
use \DTS\eBaySDK\Trading\Enums;

$service = new Services\TradingService(array(
    'apiVersion' => '921',
    'siteId' => Constants\SiteIds::US
));

$request = new Types\GetSellerListRequestType();

$request->RequesterCredentials = new Types\CustomSecurityHeaderType();
$request->RequesterCredentials->eBayAuthToken = '<YOUR PRODUCTION AUTH TOKEN>';

/**
 * The API does not return all the information available. This is to keep the size of the response small.
 * Since we want to display the title and price information we have to request that this is returned.
 */
$request->DetailLevel[] = 'ItemReturnDescription';

/**
 * Ask for items that started in the last 30 days.
 */
$request->StartTimeFrom = (new \DateTime('now', new \DateTimeZone('UTC')))->modify('-30 days');
$request->StartTimeTo = new \DateTime('now', new \DateTimeZone('UTC'));

/**
 * Request that we would like the information returned 100 entries to a page.
 */
$request->Pagination = new Types\PaginationType();
$request->Pagination->EntriesPerPage = 100;

$pageNum = 1;

do {
    $request->Pagination->PageNumber = $pageNum;

    $response = $service->getSellerList($request);

    echo "==================\nResults for page $pageNum\n==================\n";

    /**
     * Display any errors returned by the API.
     */
    if (isset($response->Errors)) {
        foreach ($response->Errors as $error) {
            printf("%s: %s\n%s\n\n",
                $error->SeverityCode === Enums\SeverityCodeType::C_ERROR ? 'Error' : 'Warning',
                $error->ShortMessage,
                $error->LongMessage
            );
        }
    }

    if ($response->Ack !== 'Failure') {
        /**
         * Iterate through the array of items that was returned.
         */
        foreach ($response->ItemArray->Item as $item) {
            printf("(%s) %s: %s %.2f\n",
                $item->ItemID,
                $item->Title,
                $item->SellingStatus->CurrentPrice->currencyID,
                $item->SellingStatus->CurrentPrice->value
            );
        }
    }

    /**
     * Continue requesting pages of information until no more are returned.
     */
    $pageNum += 1;

} while(isset($response->ItemArray) && $pageNum <= $response->PaginationResult->TotalNumberOfPages);

Upvotes: 2

Related Questions