Call eBay Shopping API "GetCategoryInfo" operation with WsdlToPhp

  26 June 2013       This post has 0 comment       By Mikaël DELSOL

Requirements

Your server must run the latest PHP version with the native SoapClient class.

You first need to download the generated package from this page. It includes :

  • the sample file: the file that shows you how to start using the generated package
  • the autoload file: the file which loads all the generated class with the good path
  • the main WsdlClass class: the class from which each generated class inherits in order to inherit usefull methods and generic methods to deal with the SoapClient
  • the ClassMap class: the class which defines the mapping between native eBay SOAP structs and generated classes
  • all the classes required to communicate with the eBay SOAP API

If you have a good IDE, then it should be really easy to navigate trough all these numerous classes.

A simple case: call the "GetCategoryInfo" operation

First of all, you need to have an eBay Developper Account. It's free and easy to get one.

Then, you'll have to generate application keys from your eBay developper account dashboard. You can have either Sandbox keys or production keys which contains what we need most, the AppID.

To follow these steps, please refer to the online eBay documentation.

First step: load classes and set configuration

The generated package includes an autoload file which makes easy to load all the generated classes at once.

require_once __DIR__ . '/EbayShoppingAutoload.php';

We suppose your file is located in the root directory of the extracted package you just downloaded.

When all classes are loaded, you can define the configuration to call the SOAP API. This configuration is required to instantiate any EbayShopping service class.

$wsdl = array(); // we use the WSDL latest version $wsdl[EbayShoppingWsdlClass::WSDL_URL] = 'http://developer.ebay.com/webservices/latest/ShoppingService.wsdl'; $wsdl[EbayShoppingWsdlClass::WSDL_CACHE_WSDL] = WSDL_CACHE_NONE; $wsdl[EbayShoppingWsdlClass::WSDL_TRACE] = true;

eBay Shopping SOAP API has the specifity that it requires HTTP Headers when calling an operation. To do so, we need to send these HTTP Headers when the operation is called by the SoapClient object encapsulated in the EbayShoppingWsdlClass. In order to do that, we have to create a PHP stream context that contains the HTTP Headers which will then be passed as an option when instantiating the EbayShoppingServiceGet class.

// create PHP stream context $options = array(); $options['http'] = array(); $options['http']['header'] = array(); $ebayHeaders = array(); // You must copy-paste your AppID here $ebayHeaders['X-EBAY-API-APP-ID'] = 'YOUR PERSONAL APPID HERE'; // The X-EBAY-API-CALL-NAME header must contain the name of the operation you're calling. // So if you call another operation, don't forget to modify it! // In our case, we are going to call the "GetCategoryInfo" operation $ebayHeaders['X-EBAY-API-CALL-NAME'] = 'GetCategoryInfo'; // At the time this article is written, the latest version number is 825 $ebayHeaders['X-EBAY-API-VERSION'] = 825; foreach($ebayHeaders as $ebayHeaderName=>$ebayHeaderValue) array_push($options['http']['header'],"$ebayHeaderName: $ebayHeaderValue"); $options['http']['header'] = implode("\r\n",$options['http']['header']); $context = stream_context_create($options); // Add the PHP stream context to the WSDL options $wsdl[EbayShoppingWsdlClass::WSDL_STREAM_CONTEXT] = $context;

Last step: call the operation

Calling the operation is pretty trivial and is then done by calling the EbayShoppingServiceGet::GetCategoryInfo() method using the configuration set previously as the $wsdl variable and by passing the correct parameter to the method such as:

$ebayShoppingServiceGet = new EbayShoppingServiceGet($wsdl); // The sample code is within the sample-ebayshopping.php file, // so you don't have to know each operation parameter class to use in your mind. // Just a little explanation about the EbayShoppingStructGetCategoryInfoRequestType parameters : // - 1: the root category id defined by eBay // - ChildCategories: unique and only accepted value to get all the root category children. // If you don't pass this parameter, you'll then only get the root category if($ebayShoppingServiceGet->GetCategoryInfo(new EbayShoppingStructGetCategoryInfoRequestType(1,'ChildCategories'))) print_r($ebayShoppingServiceGet->getResult()); else print_r($ebayShoppingServiceGet->getLastError());

If everything works fine, the $ebayShoppingServiceGet->getResult() returns a EbayShoppingStructGetCategoryInfoResponseType object which contains the eBay root category and its children such as:

EbayShoppingStructGetCategoryInfoResponseType Object ( [CategoryArray] => EbayShoppingStructCategoryArrayType Object ( [Category] => Array ( [0] => EbayShoppingStructCategoryType Object ( [CategoryID] => 1 [CategoryLevel] => 1 [CategoryName] => Collectibles [CategoryParentID] => -1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles [CategoryIDPath] => 1 [LeafCategory] => [any] => ) [1] => EbayShoppingStructCategoryType Object ( [CategoryID] => 34 [CategoryLevel] => 2 [CategoryName] => Advertising [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Advertising [CategoryIDPath] => 1:34 [LeafCategory] => [any] => ) [2] => EbayShoppingStructCategoryType Object ( [CategoryID] => 1335 [CategoryLevel] => 2 [CategoryName] => Animals [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Animals [CategoryIDPath] => 1:1335 [LeafCategory] => [any] => ) [3] => EbayShoppingStructCategoryType Object ( [CategoryID] => 13658 [CategoryLevel] => 2 [CategoryName] => Animation Art & Characters [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Animation Art & Characters [CategoryIDPath] => 1:13658 [LeafCategory] => [any] => ) [4] => EbayShoppingStructCategoryType Object ( [CategoryID] => 66502 [CategoryLevel] => 2 [CategoryName] => Arcade, Jukeboxes & Pinball [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Arcade, Jukeboxes & Pinball [CategoryIDPath] => 1:66502 [LeafCategory] => [any] => ) [5] => EbayShoppingStructCategoryType Object ( [CategoryID] => 14429 [CategoryLevel] => 2 [CategoryName] => Autographs [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Autographs [CategoryIDPath] => 1:14429 [LeafCategory] => [any] => ) [6] => EbayShoppingStructCategoryType Object ( [CategoryID] => 66503 [CategoryLevel] => 2 [CategoryName] => Banks, Registers & Vending [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Banks, Registers & Vending [CategoryIDPath] => 1:66503 [LeafCategory] => [any] => ) [7] => EbayShoppingStructCategoryType Object ( [CategoryID] => 3265 [CategoryLevel] => 2 [CategoryName] => Barware [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Barware [CategoryIDPath] => 1:3265 [LeafCategory] => [any] => ) [8] => EbayShoppingStructCategoryType Object ( [CategoryID] => 156277 [CategoryLevel] => 2 [CategoryName] => Beads [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Beads [CategoryIDPath] => 1:156277 [LeafCategory] => [any] => ) [9] => EbayShoppingStructCategoryType Object ( [CategoryID] => 29797 [CategoryLevel] => 2 [CategoryName] => Bottles & Insulators [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Bottles & Insulators [CategoryIDPath] => 1:29797 [LeafCategory] => [any] => ) [10] => EbayShoppingStructCategoryType Object ( [CategoryID] => 562 [CategoryLevel] => 2 [CategoryName] => Breweriana, Beer [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Breweriana, Beer [CategoryIDPath] => 1:562 [LeafCategory] => [any] => ) [11] => EbayShoppingStructCategoryType Object ( [CategoryID] => 898 [CategoryLevel] => 2 [CategoryName] => Casino [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Casino [CategoryIDPath] => 1:898 [LeafCategory] => [any] => ) [12] => EbayShoppingStructCategoryType Object ( [CategoryID] => 397 [CategoryLevel] => 2 [CategoryName] => Clocks [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Clocks [CategoryIDPath] => 1:397 [LeafCategory] => [any] => ) [13] => EbayShoppingStructCategoryType Object ( [CategoryID] => 63 [CategoryLevel] => 2 [CategoryName] => Comics [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Comics [CategoryIDPath] => 1:63 [LeafCategory] => [any] => ) [14] => EbayShoppingStructCategoryType Object ( [CategoryID] => 3913 [CategoryLevel] => 2 [CategoryName] => Cultures & Ethnicities [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Cultures & Ethnicities [CategoryIDPath] => 1:3913 [LeafCategory] => [any] => ) [15] => EbayShoppingStructCategoryType Object ( [CategoryID] => 13777 [CategoryLevel] => 2 [CategoryName] => Decorative Collectibles [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Decorative Collectibles [CategoryIDPath] => 1:13777 [LeafCategory] => [any] => ) [16] => EbayShoppingStructCategoryType Object ( [CategoryID] => 137 [CategoryLevel] => 2 [CategoryName] => Disneyana [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Disneyana [CategoryIDPath] => 1:137 [LeafCategory] => [any] => ) [17] => EbayShoppingStructCategoryType Object ( [CategoryID] => 10860 [CategoryLevel] => 2 [CategoryName] => Fantasy, Mythical & Magic [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Fantasy, Mythical & Magic [CategoryIDPath] => 1:10860 [LeafCategory] => [any] => ) [18] => EbayShoppingStructCategoryType Object ( [CategoryID] => 13877 [CategoryLevel] => 2 [CategoryName] => Historical Memorabilia [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Historical Memorabilia [CategoryIDPath] => 1:13877 [LeafCategory] => [any] => ) [19] => EbayShoppingStructCategoryType Object ( [CategoryID] => 907 [CategoryLevel] => 2 [CategoryName] => Holiday & Seasonal [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Holiday & Seasonal [CategoryIDPath] => 1:907 [LeafCategory] => [any] => ) [20] => EbayShoppingStructCategoryType Object ( [CategoryID] => 13905 [CategoryLevel] => 2 [CategoryName] => Kitchen & Home [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Kitchen & Home [CategoryIDPath] => 1:13905 [LeafCategory] => [any] => ) [21] => EbayShoppingStructCategoryType Object ( [CategoryID] => 1401 [CategoryLevel] => 2 [CategoryName] => Knives, Swords & Blades [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Knives, Swords & Blades [CategoryIDPath] => 1:1401 [LeafCategory] => [any] => ) [22] => EbayShoppingStructCategoryType Object ( [CategoryID] => 1404 [CategoryLevel] => 2 [CategoryName] => Lamps, Lighting [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Lamps, Lighting [CategoryIDPath] => 1:1404 [LeafCategory] => [any] => ) [23] => EbayShoppingStructCategoryType Object ( [CategoryID] => 940 [CategoryLevel] => 2 [CategoryName] => Linens & Textiles (1930-Now) [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Linens & Textiles (1930-Now) [CategoryIDPath] => 1:940 [LeafCategory] => [any] => ) [24] => EbayShoppingStructCategoryType Object ( [CategoryID] => 1430 [CategoryLevel] => 2 [CategoryName] => Metalware [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Metalware [CategoryIDPath] => 1:1430 [LeafCategory] => [any] => ) [25] => EbayShoppingStructCategoryType Object ( [CategoryID] => 13956 [CategoryLevel] => 2 [CategoryName] => Militaria [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Militaria [CategoryIDPath] => 1:13956 [LeafCategory] => [any] => ) [26] => EbayShoppingStructCategoryType Object ( [CategoryID] => 124 [CategoryLevel] => 2 [CategoryName] => Paper [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Paper [CategoryIDPath] => 1:124 [LeafCategory] => [any] => ) [27] => EbayShoppingStructCategoryType Object ( [CategoryID] => 966 [CategoryLevel] => 2 [CategoryName] => Pens & Writing Instruments [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Pens & Writing Instruments [CategoryIDPath] => 1:966 [LeafCategory] => [any] => ) [28] => EbayShoppingStructCategoryType Object ( [CategoryID] => 14005 [CategoryLevel] => 2 [CategoryName] => Pez, Keychains, Promo Glasses [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Pez, Keychains, Promo Glasses [CategoryIDPath] => 1:14005 [LeafCategory] => [any] => ) [29] => EbayShoppingStructCategoryType Object ( [CategoryID] => 14277 [CategoryLevel] => 2 [CategoryName] => Photographic Images [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Photographic Images [CategoryIDPath] => 1:14277 [LeafCategory] => [any] => ) [30] => EbayShoppingStructCategoryType Object ( [CategoryID] => 39507 [CategoryLevel] => 2 [CategoryName] => Pinbacks, Bobbles, Lunchboxes [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Pinbacks, Bobbles, Lunchboxes [CategoryIDPath] => 1:39507 [LeafCategory] => [any] => ) [31] => EbayShoppingStructCategoryType Object ( [CategoryID] => 914 [CategoryLevel] => 2 [CategoryName] => Postcards [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Postcards [CategoryIDPath] => 1:914 [LeafCategory] => [any] => ) [32] => EbayShoppingStructCategoryType Object ( [CategoryID] => 29832 [CategoryLevel] => 2 [CategoryName] => Radio, Phonograph, TV, Phone [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Radio, Phonograph, TV, Phone [CategoryIDPath] => 1:29832 [LeafCategory] => [any] => ) [33] => EbayShoppingStructCategoryType Object ( [CategoryID] => 1446 [CategoryLevel] => 2 [CategoryName] => Religion & Spirituality [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Religion & Spirituality [CategoryIDPath] => 1:1446 [LeafCategory] => [any] => ) [34] => EbayShoppingStructCategoryType Object ( [CategoryID] => 3213 [CategoryLevel] => 2 [CategoryName] => Rocks, Fossils & Minerals [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Rocks, Fossils & Minerals [CategoryIDPath] => 1:3213 [LeafCategory] => [any] => ) [35] => EbayShoppingStructCategoryType Object ( [CategoryID] => 152 [CategoryLevel] => 2 [CategoryName] => Science Fiction & Horror [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Science Fiction & Horror [CategoryIDPath] => 1:152 [LeafCategory] => [any] => ) [36] => EbayShoppingStructCategoryType Object ( [CategoryID] => 412 [CategoryLevel] => 2 [CategoryName] => Science & Medicine (1930-Now) [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Science & Medicine (1930-Now) [CategoryIDPath] => 1:412 [LeafCategory] => [any] => ) [37] => EbayShoppingStructCategoryType Object ( [CategoryID] => 113 [CategoryLevel] => 2 [CategoryName] => Sewing (1930-Now) [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Sewing (1930-Now) [CategoryIDPath] => 1:113 [LeafCategory] => [any] => ) [38] => EbayShoppingStructCategoryType Object ( [CategoryID] => 165800 [CategoryLevel] => 2 [CategoryName] => Souvenirs & Travel Memorabilia [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Souvenirs & Travel Memorabilia [CategoryIDPath] => 1:165800 [LeafCategory] => [any] => ) [39] => EbayShoppingStructCategoryType Object ( [CategoryID] => 593 [CategoryLevel] => 2 [CategoryName] => Tobacciana [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Tobacciana [CategoryIDPath] => 1:593 [LeafCategory] => [any] => ) [40] => EbayShoppingStructCategoryType Object ( [CategoryID] => 13849 [CategoryLevel] => 2 [CategoryName] => Tools, Hardware & Locks [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Tools, Hardware & Locks [CategoryIDPath] => 1:13849 [LeafCategory] => [any] => ) [41] => EbayShoppingStructCategoryType Object ( [CategoryID] => 868 [CategoryLevel] => 2 [CategoryName] => Trading Cards [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Trading Cards [CategoryIDPath] => 1:868 [LeafCategory] => [any] => ) [42] => EbayShoppingStructCategoryType Object ( [CategoryID] => 417 [CategoryLevel] => 2 [CategoryName] => Transportation [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Transportation [CategoryIDPath] => 1:417 [LeafCategory] => [any] => ) [43] => EbayShoppingStructCategoryType Object ( [CategoryID] => 597 [CategoryLevel] => 2 [CategoryName] => Vanity, Perfume & Shaving [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Vanity, Perfume & Shaving [CategoryIDPath] => 1:597 [LeafCategory] => [any] => ) [44] => EbayShoppingStructCategoryType Object ( [CategoryID] => 69851 [CategoryLevel] => 2 [CategoryName] => Vintage, Retro, Mid-Century [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Vintage, Retro, Mid-Century [CategoryIDPath] => 1:69851 [LeafCategory] => [any] => ) [45] => EbayShoppingStructCategoryType Object ( [CategoryID] => 45058 [CategoryLevel] => 2 [CategoryName] => Wholesale Lots [CategoryParentID] => 1 [CategoryParentName] => [ItemCount] => [CategoryNamePath] => Collectibles:Wholesale Lots [CategoryIDPath] => 1:45058 [LeafCategory] => [any] => ) ) ) [CategoryCount] => 46 [UpdateTime] => 2013-04-29T02:22:19.000Z [CategoryVersion] => 104 [Timestamp] => 2013-06-26T11:34:18.484Z [Ack] => Success [Errors] => [Build] => E825_CORE_APILW_16096208_R1 [Version] => 825 [CorrelationID] => )

Otherwise, you have to analyze the last error catched when the EbayShoppingServiceGet::GetCategoryInfo() method has been called by calling the generic EbayShoppingWsdlClass::getLastError() method.

Conclusion

As you can see, getting the eBay categoies list is pretty trivial and quick. We saw that each value is OOP and that responses are entirely returned as objects so we can easily manipulate them. The generated classes aim to ease the communication with SOAP Web services at each step. If you have any question, feel free to send me comments on this topic. Now, you can easily call any other eBay Shopping API operation by following this methodology.

Edit: Now you can do it using the soap client web interface.