Skip to content

Instantly share code, notes, and snippets.

@davidtsadler
Last active June 13, 2016 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidtsadler/c06bc7f02e0dd3b95b23028ee1a059e7 to your computer and use it in GitHub Desktop.
Save davidtsadler/c06bc7f02e0dd3b95b23028ee1a059e7 to your computer and use it in GitHub Desktop.

If you are comfortable with using Composer then I have developed an SDK for PHP that you can install. It simplifies the development of projects by removing the need for you to deal with XML and HTTP requests. The SDK enables you to construct requests and handle responses in an OOP way.

Below are three examples of how to get an item using the Trading, Finding, and Shopping services. Note that each service has it's own pros and cons. For example: The Trading service will probably return the most information about an item but it does require an auth token in order to use it. Where as the Shopping service does not require an auth token but less information may be returned. Which service you use will depend on your project requirements.

To get the most out of using the SDK I would suggest that you first read the getting started guide for a quick overview. Then read the eBay documentation for each of the operations that are used in the examples below. You should be able to see how the SDK is used to construct the requests required by the operations and how the names of the object properties are taken directly from the eBay documentation.

SDK Links

GitHub repo

SDK Getting started guide

Google Group for supping the SDK. This is the best place to ask questions if you want to know more about the SDK.

I've also made the examples available as Gists. These have some extra code in them that deals with handling errors returned from the API.

Gist for GetItem

Gist for findItemsAdvanced

Gist for GetSingleItem

eBay Operations

Trading: GetItem

Finding: findItemsAdvanced

Shopping: GetSingleItem

Trading GetItem example

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

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

$service = new Services\TradingService([
   'authToken'   => 'your-auth-token',
   'credentials' => [
      'appId'  => 'your-app-id',
      'certId' => 'your-cert-id',
      'devId'  => 'your-dev-id'        
   ],
   'siteId'      => Constants\SiteIds::GB
]);

$request = new Types\GetItemRequestType() ;
$request->ItemID = '191888592762';
$request->DetailLevel = ['ReturnAll'];

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

$item = $response->Item;

printf(
   "%s\n%s\n%s\n%s\n%s\n(%s)%.2f\n",
   $item->ListingDetails->ViewItemURL,
   $item->ItemID,
   $item->Title,
   $item->Description,
   $item->Quantity,
   $item->SellingStatus->CurrentPrice->currencyID,
   $item->SellingStatus->CurrentPrice->value
);

print("Domestic Shipping Information\n");
foreach ($item->ShippingDetails->ShippingServiceOptions as $shipping) {
   printf(
       "[%s] (%s)%.2f\n",
       $shipping->ShippingService,
       $shipping->ShippingServiceCost->currencyID,
       $shipping->ShippingServiceCost->value
   );
}

Finding findItemsAdvanced example

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

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

$service = new Services\FindingService([
   'credentials' => [
      'appId'  => 'your-app-id',
      'certId' => 'your-cert-id',
      'devId'  => 'your-dev-id'        
   ],
   'globalId'    => Constants\GlobalIds::GB
]);

$request = new Types\FindItemsAdvancedRequest();
$request->keywords = '191888592762';

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

$item = $response->searchResult->item[0];

printf(
   "%s\n%s\n%s\n(%s)%.2f\n",
   $item->viewItemURL,
   $item->itemId,
   $item->title,
   $item->sellingStatus->currentPrice->currencyId,
   $item->sellingStatus->currentPrice->value
);

print("Domestic Shipping Information\n");
printf(
   "(%s)%.2f\n",
   $item->shippingInfo->shippingServiceCost->currencyId,
   $item->shippingInfo->shippingServiceCost->value
);

Shopping GetSingleItem example

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

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

$service = new Services\ShoppingService([
   'credentials' => [
      'appId'  => 'your-app-id',
      'certId' => 'your-cert-id',
      'devId'  => 'your-dev-id'        
   ],
   'siteId'      => Constants\SiteIds::GB
]);

$request = new Types\GetSingleItemRequestType();
$request->ItemID = '191888592762';
$request->IncludeSelector = 'Details,Description,ShippingCosts';

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

$item = $response->Item;

printf(
   "%s\n%s\n%s\n%s\n%s\n(%s)%.2f\n",
   $item->ViewItemURLForNaturalSearch,
   $item->ItemID,
   $item->Title,
   $item->Description,
   $item->Quantity,
   $item->CurrentPrice->currencyID,
   $item->CurrentPrice->value
);

print("Domestic Shipping Information\n");
printf(
   "(%s)%.2f\n",
   $item->ShippingCostSummary->ShippingServiceCost->currencyID,
   $item->ShippingCostSummary->ShippingServiceCost->value
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment