Source for file Session.php
Documentation is available at Session.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Stephan Schmidt <schst@php.net> |
// +----------------------------------------------------------------------+
* Services/Ebay/Session.php
* manages IDs, authentication, serialization, etc.
* @author Stephan Schmidt <schst@php.net>
* Store debug data, so it can be displayed using getWire()
const URL_SANDBOX = 'https://api.sandbox.ebay.com/ws/api.dll';
const URL_PRODUCTION = 'https://api.ebay.com/ws/api.dll';
* If you do not already have one, please
* apply for a developer ID at http://developer.ebay.com
private $requestPassword;
* name of the transport driver to use
private $transportDriver = 'Curl';
* @var object Services_Ebay_Transport
* By default, the sandbox URL is used.
* @var object XML_Serializer
* XML_Unserializer object
* @var object XML_Unserializer
private $warningLevel = 'High';
private $errorLanguage = 'en_US';
* additional options for the serializer
* These options will be set by the call objects
private $serializerOptions = array ();
* additional options for the unserializer
* These options will be set by the call objects
private $unserializerOptions = array ();
* errors returned by the webservice
private $errors = array ();
* create the session object
* @param string developer id
* @param string application id
* @param string certificate id
* @param string external encoding, as eBay uses UTF-8, the session will encode and decode to
* the specified encoding. Possible values are ISO-8859-1 and UTF-8
public function __construct($devId, $appId, $certId, $encoding = 'ISO-8859-1')
$this->url = self ::URL_SANDBOX;
'scalarAsAttributes' => false
// UTF-8 encode the document, if the user does not already
if ($encoding !== 'UTF-8') {
$opts['encodeFunction'] = 'utf8_encode';
'forceEnum' => array ('Error'),
'targetEncoding' => $encoding,
$this->us = new XML_Unserializer ($opts);
* - Services_Ebay_Session::DEBUG_NONE
* - Services_Ebay_Session::DEBUG_STORE
* - Services_Ebay_Session::DEBUG_PRINT
* get the XML code that was sent accross the network
* To use this, debug mode must be set to DEBUG_STORE or DEBUG_PRINT
* @return string xml wire
* set the authentication token
* set the authentication username and password
$this->requestUserId = $username;
$this->requestPassword = $password;
* - Services_Ebay_Session::URL_SANDBOX
* - Services_Ebay_Session::URL_PRODUCTION
* - Other URLs as applicable
$this->errorLanguage = $language;
* build XML code for a request
* @param string verb of the request
* @param array|null parameters of the request
* @return string XML request
public function buildRequestBody( $verb, $params = array (), $authType = Services_Ebay ::AUTH_TYPE_TOKEN )
$this->opts['rootName'] = $verb. 'Request';
$this->opts['rootAttributes'] = array ( 'xmlns' => 'urn:ebay:apis:eBLBaseComponents' );
$this->ser = new XML_Serializer ($this->opts);
'ErrorLanguage' => $this->errorLanguage,
'WarningLevel' => $this->warningLevel
if (empty ($this->token)) {
$request['RequesterCredentials']['eBayAuthToken'] = $this->token;
if (empty ($this->requestUserId) || empty ($this->requestPassword)) {
$request['RequesterCredentials']['Username'] = $this->requestUserId;
$request['RequesterCredentials']['Password'] = $this->requestPassword;
if (empty ($this->requestUserId)) {
$request['RequesterCredentials']['Username'] = $this->requestUserId;
$this->ser->serialize ($request, $this->serializerOptions);
return $this->ser->getSerializedData ();
* This method is used by the API methods. You
* may call it directly to access any eBay function that
* is not yet implemented.
* @param string function to call
* @param array associative array containing all parameters for the function call
* @param integer authentication type
* @param boolean flag to indicate, whether XML should be parsed or returned raw
* @todo add real error handling
public function sendRequest( $verb, $params = array (), $authType = Services_Ebay ::AUTH_TYPE_TOKEN , $parseResult = true )
if (!isset ($params['ErrorLanguage']) && !is_null($this->errorLanguage)) {
$params['ErrorLanguage'] = $this->errorLanguage;
if (!isset ($params['WarningLevel']) && !is_null($this->warningLevel)) {
$params['WarningLevel'] = $this->warningLevel;
'X-EBAY-API-SESSION-CERTIFICATE' => sprintf( '%s;%s;%s', $this->devId, $this->appId, $this->certId ), // Required. Used to authenticate the function call. Use this format, where DevId is the same as the value of the X-EBAY-API-DEV-NAME header, AppId is the same as the value of the X-EBAY-API-APP-NAME header, and CertId is the same as the value of the X-EBAY-API-CERT-NAME header: DevId;AppId;CertId
'X-EBAY-API-COMPATIBILITY-LEVEL' => $this->compatLevel, // Required. Regulates versioning of the XML interface for the API.
'X-EBAY-API-DEV-NAME' => $this->devId, // Required. Developer ID, as registered with the Developer's Program. This value should match the first value (DevId) in the X-EBAY-API-SESSION-CERTIFICATE header. Used to authenticate the function call.
'X-EBAY-API-APP-NAME' => $this->appId, // Required. Application ID, as registered with the Developer's Program. This value should match the second value (AppId) in the X-EBAY-API-SESSION-CERTIFICATE header. Used to authenticate the function call.
'X-EBAY-API-CERT-NAME' => $this->certId, // Required. Certificate ID, as registered with the Developer's Program. This value should match the third value (CertId) in the X-EBAY-API-SESSION-CERTIFICATE header. Used to authenticate the function call.
'X-EBAY-API-CALL-NAME' => $verb, // Required. Name of the function being called, for example: 'GetItem' (without the quotation marks). This must match the information passed in the Verb input argument for each function.
'X-EBAY-API-SITEID' => $this->siteId, // Required. eBay site an item is listed on or that a user is registered on, depending on the purpose of the function call. This must match the information passed in the SiteId input argument for all functions.
'Content-Type' => 'text/xml', // Required. Specifies the kind of data being transmitted. The value must be 'text/xml'. Sending any other value (e.g., 'application/x-www-form-urlencoded') may cause the call to fail.
'Content-Length' => strlen( $body ) // Recommended. Specifies the size of the data (i.e., the length of the XML string) you are sending. This is used by eBay to determine how much data to read from the stream.
$class = 'Services_Ebay_Transport_'. $this->transportDriver;
$this->wire .= "Sending request:\n";
$this->wire .= $body. "\n\n";
$response = $tp->sendRequest ($this->url, $body, $headers);
if (PEAR ::isError ($response)) {
$this->wire .= "Received response:\n";
$this->wire .= $response. "\n\n";
if ($parseResult === false ) {
$this->us->unserialize ( $response, false , $this->unserializerOptions );
$result = $this->us->getUnserializedData ();
if (isset ($result['Ack']) && $result['Ack'] == 'Failure') {
if (isset ($result['Errors'])) {
if (isset ($result['Errors'][0 ])) {
foreach ($result['Errors'] as $error) {
foreach ($result['Errors'] as $key=> $value) {
// check for serious errors
foreach ($errors as $error) {
if ($error->getSeverityCode () == 'Warning') {
$message .= $error->getLongMessage ();
* get the errors and warnings that happened during the
* @param boolean whether to clear the internal error list
* set additional options for the serializer
$this->serializerOptions = $opts;
* set additional options for the unserializer
$this->unserializerOptions = $opts;
* set compatibility level if particular request needs
* another version of API instead of default one
Documentation generated on Mon, 11 Mar 2019 15:49:49 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|