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

Documentation generated on Mon, 11 Mar 2019 14:00:24 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.