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.17 2005/06/05 13:28:39 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.    /**o
  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.     * transport 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.     * additional options for the unserializer
  190.     *
  191.     * These options will be set by the call objects
  192.     *
  193.     * @var  array 
  194.     */
  195.     private $unserializerOptions = array();
  196.  
  197.    /**
  198.     * errors returned by the webservice
  199.     *
  200.     * @var array 
  201.     */
  202.     private $errors = array();
  203.     
  204.    /**
  205.     * create the session object
  206.     *
  207.     * @param    string  developer id
  208.     * @param    string  application id
  209.     * @param    string  certificate id
  210.     * @param    string  external encoding, as eBay uses UTF-8, the session will encode and decode to
  211.     *                    the specified encoding. Possible values are ISO-8859-1 and UTF-8
  212.     */
  213.     public function __construct($devId$appId$certId$encoding 'ISO-8859-1')
  214.     {
  215.         $this->devId $devId;
  216.         $this->appId $appId;
  217.         $this->certId $certId;
  218.         $this->url = self::URL_SANDBOX;
  219.         
  220.         $opts = array(
  221.                          'indent'             => '  ',
  222.                          'linebreak'          => "\n",
  223.                          'typeHints'          => false,
  224.                          'addDecl'            => true,
  225.                          'encoding'           => 'UTF-8',
  226.                          'scalarAsAttributes' => false,
  227.                          'rootName'           => 'request',
  228.                          'rootAttributes'     => array'xmlns' => 'urn:eBayAPIschema' ),
  229.                     );
  230.         // UTF-8 encode the document, if the user does not already
  231.         // use UTF-8 encoding
  232.         if ($encoding !== 'UTF-8'{
  233.             $opts['encodeFunction''utf8_encode';
  234.         }
  235.  
  236.         $this->ser = new XML_Serializer($opts);
  237.  
  238.         $opts = array(
  239.                     'forceEnum'      => array('Error'),
  240.                     'encoding'       => 'UTF-8',
  241.                     'targetEncoding' => $encoding,
  242.                     );
  243.         $this->us  = new XML_Unserializer($opts);
  244.     }
  245.  
  246.    /**
  247.     * set the debug mode
  248.     *
  249.     * Possible values are:
  250.     * - Services_Ebay_Session::DEBUG_NONE
  251.     * - Services_Ebay_Session::DEBUG_STORE
  252.     * - Services_Ebay_Session::DEBUG_PRINT
  253.     *
  254.     * @param    integer 
  255.     * @see      getWire()
  256.     */
  257.     public function setDebug($debug)
  258.     {
  259.         $this->debug = $debug;
  260.     }
  261.  
  262.    /**
  263.     * get the XML code that was sent accross the network
  264.     *
  265.     * To use this, debug mode must be set to DEBUG_STORE or DEBUG_PRINT
  266.     *
  267.     * @return   string      xml wire
  268.     */
  269.     public function getWire()
  270.     {
  271.         return $this->wire;
  272.     }
  273.  
  274.    /**
  275.     * set the authentication token
  276.     *
  277.     * @param    string 
  278.     */
  279.     public function setToken($token)
  280.     {
  281.         $this->token $token;
  282.     }
  283.  
  284.    /**
  285.     * set the authentication username and password
  286.     *
  287.     * @param    string 
  288.     */
  289.     public function setAuthenticationData($username$password = null)
  290.     {
  291.         $this->requestUserId   $username;
  292.         $this->requestPassword $password;
  293.     }
  294.  
  295.    /**
  296.     * set the API URL
  297.     *
  298.     * @param    string 
  299.     *
  300.     *  Possible values are:
  301.     *  - Services_Ebay_Session::URL_SANDBOX
  302.     *  - Services_Ebay_Session::URL_PRODUCTION
  303.     *  - Other URLs as applicable
  304.     *
  305.     */
  306.     public function setUrl($url)
  307.     {
  308.        $this->url $url;
  309.     }
  310.  
  311.    /**
  312.     * set the site id
  313.     *
  314.     * @param    integer 
  315.     */
  316.     public function setSiteId($siteId)
  317.     {
  318.        $this->siteId $siteId;
  319.     }
  320.  
  321.    /**
  322.     * set the detail level
  323.     *
  324.     * @param    integer 
  325.     */
  326.     public function setDetailLevel($level)
  327.     {
  328.         $this->detailLevel $level;
  329.     }
  330.     
  331.    /**
  332.     * set the error language
  333.     *
  334.     * @param    string 
  335.     */
  336.     public function setErrorLanguage($language)
  337.     {
  338.        $this->errorLanguage $language;
  339.     }
  340.  
  341.    /**
  342.     * build XML code for a request
  343.     *
  344.     * @access   private
  345.     * @param    string      verb of the request
  346.     * @param    array|null parameters of the request
  347.     * @return   string      XML request
  348.     */
  349.     public function buildRequestBody$verb$params = array()$authType = Services_Ebay::AUTH_TYPE_TOKEN )
  350.     {
  351.         $request = array(
  352.                             'DetailLevel'     => $this->detailLevel,
  353.                             'ErrorLevel'      => 1,
  354.                             'SiteId'          => $this->siteId,
  355.                             'Verb'            => $verb
  356.                         );
  357.         switch ($authType{
  358.             case Services_Ebay::AUTH_TYPE_TOKEN:
  359.                 if (empty($this->token)) {
  360.                     throw new Services_Ebay_Auth_Exception('No authentication token set.');
  361.                 }
  362.                 $request['RequestToken'$this->token;
  363.                 break;
  364.             case Services_Ebay::AUTH_TYPE_USER:
  365.                 if (empty($this->requestUserId|| empty($this->requestPassword)) {
  366.                     throw new Services_Ebay_Auth_Exception('No authentication data (username and password) set.');
  367.                 }
  368.                 $request['RequestUserId']   $this->requestUserId;
  369.                 $request['RequestPassword'$this->requestPassword;
  370.                 break;
  371.             case Services_Ebay::AUTH_TYPE_NONE:
  372.                 if (empty($this->requestUserId)) {
  373.                     throw new Services_Ebay_Auth_Exception('No username has been set.');
  374.                 }
  375.                 $request['RequestUserId']   $this->requestUserId;
  376.                 break;
  377.         }
  378.  
  379.         $request array_merge($request$params);
  380.  
  381.         $this->ser->serialize($request$this->serializerOptions);
  382.         
  383.         return $this->ser->getSerializedData();
  384.     }
  385.  
  386.    /**
  387.     * send a request
  388.     *
  389.     * This method is used by the API methods. You
  390.     * may call it directly to access any eBay function that
  391.     * is not yet implemented.
  392.     *
  393.     * @access   public
  394.     * @param    string      function to call
  395.     * @param    array       associative array containing all parameters for the function call
  396.     * @param    integer     authentication type
  397.     * @param    boolean     flag to indicate, whether XML should be parsed or returned raw
  398.     * @return   array       response
  399.     * @todo     add real error handling
  400.     */
  401.     public function sendRequest$verb$params = array()$authType = Services_Ebay::AUTH_TYPE_TOKEN$parseResult = true )
  402.     {
  403.         $this->wire = '';
  404.  
  405.         if (!isset($params['ErrorLanguage']&& !is_null($this->errorLanguage)) {
  406.             $params['ErrorLanguage'$this->errorLanguage;
  407.         }
  408.  
  409.         $body    $this->buildRequestBody($verb$params$authType);
  410.  
  411.         if (!isset($params['DetailLevel'])) {
  412.             $params['DetailLevel'$this->detailLevel;
  413.         }
  414.         
  415.         $headers = array(
  416.                             '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
  417.                             'X-EBAY-API-COMPATIBILITY-LEVEL' => $this->compatLevel,                                                 // Required. Regulates versioning of the XML interface for the API.
  418.                             '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.
  419.                             '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.
  420.                             '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.
  421.                             '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.
  422.                             '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.
  423.                             '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.
  424.                             '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.
  425.                             '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.
  426.                         );
  427.  
  428.         $file  SERVICES_EBAY_BASEDIR.'/Ebay/Transport/'.$this->transportDriver.'.php';
  429.         $class 'Services_Ebay_Transport_'.$this->transportDriver;
  430.  
  431.         @include_once $file;
  432.         if (!class_exists($class)) {
  433.             throw new Services_Ebay_Transport_Exception('Could not load selected transport driver.');            
  434.         }
  435.         $tp = new $class();
  436.         
  437.         if ($this->debug > 0{
  438.             $this->wire .= "Sending request:\n";
  439.             $this->wire .= $body."\n\n";
  440.         }
  441.  
  442.         $response $tp->sendRequest($this->url$body$headers);
  443.  
  444.         if (PEAR::isError($response)) {
  445.             return $response;
  446.         }
  447.         
  448.         if ($this->debug > 0{
  449.             $this->wire .= "Received response:\n";
  450.             $this->wire .= $response."\n\n";
  451.         }
  452.  
  453.         if ($this->debug > 1{
  454.             echo $this->wire . "\n";
  455.         }
  456.         
  457.         if ($parseResult === false{
  458.             return $response;
  459.         }
  460.  
  461.         $this->us->unserialize$responsefalse$this->unserializerOptions );
  462.         $result $this->us->getUnserializedData();
  463.  
  464.         $errors = array();
  465.         
  466.         if (isset($result['Errors'])) {
  467.             if (isset($result['Errors']['Error'])) {
  468.                 foreach ($result['Errors']['Error'as $error{
  469.                     $tmp = new Services_Ebay_Error($error);
  470.                     // last errors
  471.                     array_push($errors$tmp);
  472.                     
  473.                     // all errors
  474.                     array_push($this->errors$tmp);
  475.                 }
  476.  
  477.                 // check for serious errors
  478.                 $message '';
  479.                 $severe  = array();
  480.                 foreach ($errors as $error{
  481.                     if ($error->getSeverityCode(== 2{
  482.                         continue;
  483.                     }
  484.                     $message .= $error->getLongMessage();
  485.                     $message .= "\n";
  486.                     array_push($severe$error);
  487.                 }
  488.                 if (!empty($severe)) {
  489.                     throw new Services_Ebay_API_Exception($message$severe);
  490.                 }
  491.             else {
  492.                 throw new Services_Ebay_API_Exception('Unknown error occured.');
  493.             }
  494.         }
  495.         return $result;
  496.     }
  497.  
  498.    /**
  499.     * get the errors and warnings that happened during the
  500.     * last API calls
  501.     *
  502.     * @param  boolean   whether to clear the internal error list
  503.     * @return array 
  504.     */
  505.     public function getErrors($clear = true)
  506.     {
  507.         $errors $this->errors;
  508.         if ($clear === true{
  509.             $this->errors = array();
  510.         }
  511.         return $errors;
  512.     }
  513.     
  514.    /**
  515.     * set additional options for the serializer
  516.     *
  517.     * @param    array 
  518.     */
  519.     public function setSerializerOptions($opts = array())
  520.     {
  521.         $this->serializerOptions $opts;
  522.     }
  523.  
  524.    /**
  525.     * set additional options for the unserializer
  526.     *
  527.     * @param    array 
  528.     */
  529.     public function setUnserializerOptions($opts = array())
  530.     {
  531.         $this->unserializerOptions $opts;
  532.     }
  533. }
  534. ?>

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