Services_Ebay
[ class tree: Services_Ebay ] [ index: Services_Ebay ] [ all elements ]

Source for file Session.php

Documentation is available at Session.php

  1. <?PHP
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stephan Schmidt <schst@php.net>                             |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Session.php,v 1.6 2004/12/01 19:50:23 schst Exp $
  20.  
  21. /**
  22.  * Services/Ebay/Session.php
  23.  *
  24.  * manages IDs, authentication, serialization, etc.
  25.  *
  26.  * @package  Services_Ebay
  27.  * @author   Stephan Schmidt <schst@php.net>
  28.  */
  29. {
  30.    /**
  31.     * Debugging disabled
  32.     */
  33.     const DEBUG_NONE = 0;
  34.  
  35.    /**
  36.     * Store debug data, so it can be displayed using getWire()
  37.     */
  38.     const DEBUG_STORE = 1;
  39.  
  40.    /**
  41.     * Display debug data
  42.     */
  43.     const DEBUG_PRINT = 2;
  44.  
  45.    /**
  46.     * developer ID
  47.     *
  48.     * If you do not already have one, please
  49.     * apply for a developer ID at http://developer.ebay.com
  50.     *
  51.     * @var string 
  52.     */
  53.     private $devId;
  54.     
  55.    /**
  56.     * application id
  57.     *
  58.     * @var string 
  59.     */
  60.     private $appId;
  61.  
  62.    /**
  63.     * application id
  64.     *
  65.     * @var certificate ID
  66.     */
  67.     private $certId;
  68.     
  69.    /**
  70.     * Auth & Auth token
  71.     *
  72.     * @var string 
  73.     */
  74.     private $token;
  75.  
  76.    /**
  77.     * auth username
  78.     *
  79.     * @var string 
  80.     */
  81.     private $requestUserId;
  82.  
  83.    /**
  84.     * auth password
  85.     *
  86.     * @var string 
  87.     */
  88.     private $requestPassword;
  89.  
  90.     /**
  91.     * name of the transport driver to use
  92.     *
  93.     * @var string 
  94.     */
  95.     private $transportDriver 'Curl';
  96.  
  97.    /**
  98.     * transpor driver
  99.     *
  100.     * @var  object Services_Ebay_Transport 
  101.     */
  102.     private $transport;
  103.     
  104.    /**
  105.     * URL to use
  106.     *
  107.     * By default, the sandbox URL is used.
  108.     *
  109.     * @var  string 
  110.     */
  111.     private $url 'https://api.sandbox.ebay.com/ws/api.dll';
  112.  
  113.    /**
  114.     * site id
  115.     *
  116.     * @var  integer 
  117.     */
  118.     private $siteId = 0;
  119.  
  120.    /**
  121.     * XML_Serializer object
  122.     *
  123.     * @var object XML_Serializer 
  124.     */
  125.     private $ser;
  126.  
  127.    /**
  128.     * XML_Unserializer object
  129.     *
  130.     * @var object XML_Unserializer 
  131.     */
  132.     private $us;
  133.     
  134.    /**
  135.     * debug flag
  136.     *
  137.     * @var boolean 
  138.     */
  139.     public $debug = 0;
  140.     
  141.    /**
  142.     * XML wire
  143.     *
  144.     * @var string 
  145.     */
  146.     public $wire = null;
  147.     
  148.    /**
  149.     * compatibility level
  150.     *
  151.     * @var       integer 
  152.     */
  153.     public $compatLevel = 379;
  154.  
  155.    /**
  156.     * general detail Level
  157.     *
  158.     * @var  int 
  159.     */
  160.     private $detailLevel = 0;
  161.  
  162.    /**
  163.     * error language
  164.     *
  165.     * @var  int 
  166.     */
  167.     private $errorLanguage = null;
  168.  
  169.    /**
  170.     * additional options for the serializer
  171.     *
  172.     * These options will be set by the call objects
  173.     *
  174.     * @var  array 
  175.     */
  176.     private $serializerOptions = array();
  177.     
  178.    /**
  179.     * create the session object
  180.     *
  181.     * @param    string  developer id
  182.     * @param    string  application id
  183.     * @param    string  certificate id
  184.     */
  185.     public function __construct($devId$appId$certId)
  186.     {
  187.         $this->devId $devId;
  188.         $this->appId $appId;
  189.         $this->certId $certId;
  190.  
  191.         $opts = array(
  192.                          'indent'             => '  ',
  193.                          'linebreak'          => "\n",
  194.                          'typeHints'          => false,
  195.                          'addDecl'            => true,
  196.                          'scalarAsAttributes' => false,
  197.                          'rootName'           => 'request',
  198.                          'rootAttributes'     => array'xmlns' => 'urn:eBayAPIschema' ),
  199.                     );
  200.         $this->ser = new XML_Serializer$opts );
  201.  
  202.         $opts = array(
  203.                     'forceEnum' => array('Error')
  204.                     );
  205.         $this->us  = new XML_Unserializer$opts );
  206.     }
  207.     
  208.    /**
  209.     * set the debug mode
  210.     *
  211.     * Possible values are:
  212.     * - Services_Ebay_Session::DEBUG_NONE
  213.     * - Services_Ebay_Session::DEBUG_STORE
  214.     * - Services_Ebay_Session::DEBUG_PRINT
  215.     *
  216.     * @param    integer 
  217.     * @see      getWire()
  218.     */
  219.     public function setDebug($debug)
  220.     {
  221.         $this->debug = $debug;
  222.     }
  223.     
  224.    /**
  225.     * get the XML code that was sent accross the network
  226.     *
  227.     * To use this, debug mode must be set to DEBUG_STORE or DEBUG_PRINT
  228.     *
  229.     * @return   string      xml wire
  230.     */
  231.     public function getWire()
  232.     {
  233.         return $this->wire;
  234.     }
  235.  
  236.    /**
  237.     * set the authentication token
  238.     *
  239.     * @param    string 
  240.     */
  241.     public function setToken($token)
  242.     {
  243.         $this->token $token;
  244.     }
  245.  
  246.    /**
  247.     * set the authentication username and password
  248.     *
  249.     * @param    string 
  250.     */
  251.     public function setAuthenticationData($username$password = null)
  252.     {
  253.         $this->requestUserId   $username;
  254.         $this->requestPassword $password;
  255.     }
  256.  
  257.    /**
  258.     * set the API URL
  259.     *
  260.     * @param    string 
  261.     */
  262.     public function setUrl($url)
  263.     {
  264.        $this->url $url;
  265.     }
  266.  
  267.    /**
  268.     * set the site id
  269.     *
  270.     * @param    integer 
  271.     */
  272.     public function setSiteId($siteId)
  273.     {
  274.        $this->siteId $siteId;
  275.     }
  276.  
  277.    /**
  278.     * set the detail level
  279.     *
  280.     * @param    integer 
  281.     */
  282.     public function setDetailLevel($level)
  283.     {
  284.         $this->detailLevel $level;
  285.     }
  286.     
  287.    /**
  288.     * set the error language
  289.     *
  290.     * @param    string 
  291.     */
  292.     public function setErrorLanguage($language)
  293.     {
  294.        $this->errorLanguage $language;
  295.     }
  296.  
  297.    /**
  298.     * build XML code for a request
  299.     *
  300.     * @access   private
  301.     * @param    string      verb of the request
  302.     * @param    array|null parameters of the request
  303.     * @return   string      XML request
  304.     */
  305.     public function buildRequestBody$verb$params = array()$authType = Services_Ebay::AUTH_TYPE_TOKEN )
  306.     {
  307.         $request = array(
  308.                             'DetailLevel'     => $this->detailLevel,
  309.                             'ErrorLevel'      => 1,
  310.                             'SiteId'          => $this->siteId,
  311.                             'Verb'            => $verb
  312.                         );
  313.         switch ($authType{
  314.             case Services_Ebay::AUTH_TYPE_TOKEN:
  315.                 if (empty($this->token)) {
  316.                     throw new Services_Ebay_Auth_Exception('No authentication token set.');
  317.                 }
  318.                 $request['RequestToken'$this->token;
  319.                 break;
  320.             case Services_Ebay::AUTH_TYPE_USER:
  321.                 if (empty($this->requestUserId|| empty($this->requestPassword)) {
  322.                     throw new Services_Ebay_Auth_Exception('No authentication data (username and password) set.');
  323.                 }
  324.                 $request['RequestUserId']   $this->requestUserId;
  325.                 $request['RequestPassword'$this->requestPassword;
  326.                 break;
  327.             case Services_Ebay::AUTH_TYPE_NONE:
  328.                 if (empty($this->requestUserId)) {
  329.                     throw new Services_Ebay_Auth_Exception('No username has been set.');
  330.                 }
  331.                 $request['RequestUserId']   $this->requestUserId;
  332.                 break;
  333.         }
  334.  
  335.         $request array_merge($request$params);
  336.  
  337.         $this->ser->serialize($request$this->serializerOptions);
  338.         
  339.         return $this->ser->getSerializedData();
  340.     }
  341.  
  342.    /**
  343.     * send a request
  344.     *
  345.     * This method is used by the API methods. You
  346.     * may call it directly to access any eBay function that
  347.     * is not yet implemented.
  348.     *
  349.     * @access   public
  350.     * @param    string      function to call
  351.     * @param    array       associative array containing all parameters for the function call
  352.     * @return   array       response
  353.     * @todo     add real error handling
  354.     */
  355.     public function sendRequest$verb$params = array()$authType = Services_Ebay::AUTH_TYPE_TOKEN )
  356.     {
  357.         $this->wire = '';
  358.  
  359.         if (!isset($params['ErrorLanguage']&& !is_null($this->errorLanguage)) {
  360.             $params['ErrorLanguage'$this->errorLanguage;
  361.         }
  362.  
  363.         $body    $this->buildRequestBody($verb$params$authType);
  364.  
  365.         if (!isset($params['DetailLevel'])) {
  366.             $params['DetailLevel'$this->detailLevel;
  367.         }
  368.         
  369.         $headers = array(
  370.                             '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
  371.                             'X-EBAY-API-COMPATIBILITY-LEVEL' => $this->compatLevel,                                                 // Required. Regulates versioning of the XML interface for the API.
  372.                             '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.
  373.                             '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.
  374.                             '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.
  375.                             '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.
  376.                             '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.
  377.                             'X-EBAY-API-DETAIL-LEVEL'        => $params['DetailLevel'],                                             // Required. Controls amount or level of data returned by the function call. May be zero if the function does not support varying detail levels. This must match the information passed in the DetailLevel input argument for each function.
  378.                             '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.
  379.                             '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.
  380.                         );
  381.  
  382.         $file  SERVICES_EBAY_BASEDIR.'/Ebay/Transport/'.$this->transportDriver.'.php';
  383.         $class 'Services_Ebay_Transport_'.$this->transportDriver;
  384.  
  385.         @include_once $file;
  386.         if (!class_exists($class)) {
  387.             throw new Services_Ebay_Transport_Exception('Could not load selected transport driver.');            
  388.         }
  389.         $tp = new $class();
  390.         
  391.         if ($this->debug > 0{
  392.             $this->wire .= "Sending request:\n";
  393.             $this->wire .= $body."\n\n";
  394.         }
  395.  
  396.         $response $tp->sendRequest($this->url$body$headers);
  397.  
  398.         if (PEAR::isError($response)) {
  399.             return $response;
  400.         }
  401.         
  402.         if ($this->debug > 0{
  403.             $this->wire .= "Received response:\n";
  404.             $this->wire .= $response."\n\n";
  405.         }
  406.  
  407.         $this->us->unserialize$response );
  408.         
  409.         $result $this->us->getUnserializedData();
  410.  
  411.         if ($this->debug > 1{
  412.             echo $this->wire;
  413.         }
  414.         
  415.         if (isset($result['Errors'])) {
  416.             if (isset($result['Errors']['Error'])) {
  417.                 $message '';
  418.                 foreach ($result['Errors']['Error'as $error{
  419.                     $message $message ' ' $error['LongMessage'];
  420.                 }
  421.                 throw new Services_Ebay_Exception$message );
  422.             else {
  423.                 throw new Services_Ebay_API_Exception('Unknown error occured.');
  424.             }
  425.             
  426.         }
  427.  
  428.         return $result;
  429.     }
  430.     
  431.    /**
  432.     * set additional options for the serializer
  433.     *
  434.     * @param    array 
  435.     */
  436.     public function setSerializerOptions($opts = array())
  437.     {
  438.         $this->serializerOptions $opts;
  439.     }
  440. }
  441. ?>

Documentation generated on Mon, 11 Mar 2019 13:59:53 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.