Building an application or site in PHP that uses the eBay API can at first be quite a daunting task, just trying to get your head around that vast selection of API's available to the budding developer for eBay can take a while in its self before you even write your first line of code. So with that in mind I have put together this 'quick start' guide to the basics of dealing with listing and managing items with the eBay Trading API. (For further info on the trading api click here)
You Essentially have two options available for you as a PHP developer, you can start from scratch and build your system from the ground up using XML and CURL or you can use the PHP SDK (PHP Accelerator Toolkit for eBay - Trading API Edition) available from either the ebay developer portal or intradesys. I recommend intradesys if your going down this route as the version on their site is a good 200 versions more up to date than the one from ebay.
If you want to code from scratch then take a look at another tutorial Click Here as the rest of this post only deals with using the SDK.
Before going any further your going to need to get an ebay developer program account and generate a set of sandbox keys. These consist of:
- DevID
- AppID
- CertID
You will also going to need a user token. This can also be generated from the developer account and will related to a sandbox ebay user ID.
Ok so lets start to build our connection to ebay. Your going to want to start by including the various files needed by the PHP SDK.
<?php
$rootPath = "../app/";
require_once ($rootPath.'../EbatNs747/EbatNs_Logger.php');
require_once ($rootPath.'../EbatNs747/EbatNs_Session.php');
require_once ($rootPath.'../EbatNs747/EbatNs_ServiceProxy.php');
require_once ($rootPath.'../EbatNs747/AbstractResponseType.php');
require_once ($rootPath.'../EbatNs747/AckCodeType.php');
Here we are setting the root path to your application and then using that as a reference to load in the required files using require_once. The two most important here are Session and ServiceProxy as these actually deal with the communication with ebay.
Now we need to start our ebay class off, this is where all the hard work will be done. If you don’t understand OOP ( Object Orientated Programming ) then you should probably have a read about it first otherwise some of this might be lost on you
class EbayClass
{
protected $logger = null;
protected $proxy = null;
protected $session = null;
public $appMode = '1';
function __construct() {
$this->init($logLevel);
parent::__construct();
}
public function init($logLevel = 0)
{
$this->session = new EbatNs_Session();
$this->sessionSetup();
$this->proxy = new EbatNs_ServiceProxy($this->session);
$this->proxy->setTransportOptions(array("HTTP_VERBOSE" => true) );
}
So lets break down what we have here so far, we have included the basic ebay SDK files (these contain the functions we need later. Then we have defined a constructor which runs our init function. Then our init function starts to build our connection with ebay, first by making a new instance of EbatNs_Session as well as a new instance of the EbatNs_ServiceProxy then set the proxy transport options. However you will notice we have called something else in the middle of all this ‘sessionSetup()’. This is a function in our ebay class that gives our new instance of EbatNs_Session some of the data required for its connection to ebay.
public function sessionSetup(){
$this->session->setAppMode($this->appMode);
$this->session->setAppId($this->getAppId());
$this->session->setDevId($this->getDevId());
$this->session->setCertId($this->getCertId());
}
Remember the details I said you needed from the ebay developer program? Yep this is where we are setting them for our new session instance. One element I hadn’t mentioned before is the appMode value, this tells the ebay SDK if we are using the sandbox or the live site 1= sandbox 0 = live.
The order we set these values is actually somewhat important because the setAppMode function in session actually attempts to set the values of AppID, DevID and CertID from a config file that we are not using so if we don’t call this first then we would lose the values we set of our own. I won’t show the code for each of the get functions here, they are simple enough as they just return a string.
So now we have essentially a functional connection to ebay’s api. Not that we can do much with it at this point, we need to include another file from the SDK yet. We are going to test our connection using the api function ‘GeteBayOfficialTime’ and for this we need to include that request type in our code like this:
require_once ($rootPath.'../EbatNs747/EbatNs_Logger.php');
require_once ($rootPath.'../EbatNs747/EbatNs_Session.php');
require_once ($rootPath.'../EbatNs747/EbatNs_ServiceProxy.php');
require_once ($rootPath.'../EbatNs747/AbstractResponseType.php');
require_once ($rootPath.'../EbatNs747/AckCodeType.php');
require_once ($rootPath.'../EbatNs747/GeteBayOfficialTimeRequestType.php');
So now we can make a new function that creates a new instance of GeteBayOfficialTimeRequestType then passes it to ebay via the proxy object and returns the result.
public function FetchEbayOfficialTime(){
$timeRequest = new GeteBayOfficialTimeRequestType();
$result = $this->proxy->GeteBayOfficialTime($timeRequest);
return $result;
}
So in theory this should return a result object a little like this…
<?xml version="1.0" encoding="utf-8"?>
<GeteBayOfficialTimeResponse xmlns="urn:ebay:apis:eBLBaseComponents">
<Ack> AckCodeType </Ack>
<Build> string </Build>
<CorrelationID> string </CorrelationID>
<Errors> ErrorType
<ErrorClassification> ErrorClassificationCodeType </ErrorClassification>
<ErrorCode> token </ErrorCode>
<ErrorParameters ParamID="string"> ErrorParameterType
<Value> string </Value>
</ErrorParameters>
<LongMessage> string </LongMessage>
<SeverityCode> SeverityCodeType </SeverityCode>
<ShortMessage> string </ShortMessage>
</Errors>
<HardExpirationWarning> string </HardExpirationWarning>
<Timestamp> dateTime </Timestamp>
<Version> string </Version>
</GeteBayOfficialTimeResponse>
So lets put all that together and instantiate our new class:
$ebay = new EbayClass();
$timeresults = $ebay->FetchEbayOfficialTime();
echo $timeresults->Timestamp;
This should echo out something along the lines of.. 2012-08-02T08:05:14.983Z
Amazing eh? Well ok maybe not but still its proof we now have a functioning connection to ebay.
Next time we will look at how to list a simple product using our new found api skills...