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.3 2004/11/16 19:34:05 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.     * developer ID
  32.     *
  33.     * If you do not already have one, please
  34.     * apply for a developer ID at http://developer.ebay.com
  35.     *
  36.     * @var string 
  37.     */
  38.     private $devId;
  39.     
  40.    /**
  41.     * application id
  42.     *
  43.     * @var string 
  44.     */
  45.     private $appId;
  46.  
  47.    /**
  48.     * application id
  49.     *
  50.     * @var certificate ID
  51.     */
  52.     private $certId;
  53.     
  54.    /**
  55.     * Auth & Auth token
  56.     *
  57.     * @var string 
  58.     */
  59.     private $token;
  60.  
  61.    /**
  62.     * auth username
  63.     *
  64.     * @var string 
  65.     */
  66.     private $requestUserId;
  67.  
  68.    /**
  69.     * auth password
  70.     *
  71.     * @var string 
  72.     */
  73.     private $requestPassword;
  74.  
  75.     /**
  76.     * name of the transport driver to use
  77.     *
  78.     * @var string 
  79.     */
  80.     private $transportDriver 'Curl';
  81.  
  82.    /**
  83.     * transpor driver
  84.     *
  85.     * @var  object Services_Ebay_Transport 
  86.     */
  87.     private $transport;
  88.     
  89.    /**
  90.     * URL to use
  91.     *
  92.     * @var  string 
  93.     */
  94.     private $url 'https://api.sandbox.ebay.com/ws/api.dll';
  95.  
  96.    /**
  97.     * XML_Serializer object
  98.     *
  99.     * @var object XML_Serializer 
  100.     */
  101.     private $ser;
  102.  
  103.    /**
  104.     * XML_Unserializer object
  105.     *
  106.     * @var object XML_Unserializer 
  107.     */
  108.     private $us;
  109.     
  110.    /**
  111.     * debug flag
  112.     *
  113.     * @var boolean 
  114.     */
  115.     public $debug = 1;
  116.     
  117.    /**
  118.     * XML wire
  119.     *
  120.     * @var string 
  121.     */
  122.     public $wire;
  123.     
  124.    /**
  125.     * compatibility level
  126.     *
  127.     * @var       integer 
  128.     */
  129.     public $compatLevel = 379;
  130.  
  131.    /**
  132.     * general detail Level
  133.     *
  134.     * @var  int 
  135.     */
  136.     private $detailLevel = 0;
  137.  
  138.    /**
  139.     * additional options for the serializer
  140.     *
  141.     * These options will be set by the call objects
  142.     *
  143.     * @var  array 
  144.     */
  145.     private $serializerOptions = array();
  146.     
  147.    /**
  148.     * create the session object
  149.     *
  150.     * @param    string  developer id
  151.     * @param    string  application id
  152.     * @param    string  certificate id
  153.     */
  154.     public function __construct($devId$appId$certId)
  155.     {
  156.         $this->devId $devId;
  157.         $this->appId $appId;
  158.         $this->certId $certId;
  159.  
  160.         $opts = array(
  161.                          'indent'             => '  ',
  162.                          'linebreak'          => "\n",
  163.                          'typeHints'          => false,
  164.                          'addDecl'            => true,
  165.                          'scalarAsAttributes' => false,
  166.                          'rootName'           => 'request',
  167.                          'rootAttributes'     => array'xmlns' => 'urn:eBayAPIschema' ),
  168.                     );
  169.         $this->ser = new XML_Serializer$opts );
  170.  
  171.         $opts = array(
  172.                     'forceEnum' => array('Error')
  173.                     );
  174.         $this->us  = new XML_Unserializer$opts );
  175.     }
  176.     
  177.    /**
  178.     * set the authentication token
  179.     *
  180.     * @param    string 
  181.     */
  182.     public function setToken($token)
  183.     {
  184.         $this->token $token;
  185.     }
  186.     
  187.    /**
  188.     * set the authentication username and password
  189.     *
  190.     * @param    string 
  191.     */
  192.     public function setAuthenticationData($username$password = null)
  193.     {
  194.         $this->requestUserId   $username;
  195.         $this->requestPassword $password;
  196.     }
  197.  
  198.     /**
  199.     * set the API URL
  200.     *
  201.     * @param    string 
  202.     */
  203.     public function setUrl()
  204.     {
  205.        $this->url $url;
  206.     }
  207.  
  208.    /**
  209.     * set the detail level
  210.     *
  211.     * @param    integer 
  212.     */
  213.     public function setDetailLevel($level)
  214.     {
  215.         $this->detailLevel $level;
  216.     }
  217.     
  218.    /**
  219.     * build XML code for a request
  220.     *
  221.     * @access   private
  222.     * @param    string      verb of the request
  223.     * @param    array|null parameters of the request
  224.     * @return   string      XML request
  225.     */
  226.     public function buildRequestBody$verb$params = array()$authType = Services_Ebay::AUTH_TYPE_TOKEN )
  227.     {
  228.         $request = array(
  229.                             'DetailLevel'     => $this->detailLevel,
  230.                             'ErrorLevel'      => 1,
  231.                             'SiteId'          => 0,
  232.                             'Verb'            => $verb
  233.                         );
  234.         switch ($authType{
  235.             case Services_Ebay::AUTH_TYPE_TOKEN:
  236.                 if (empty($this->token)) {
  237.                     throw new Services_Ebay_Auth_Exception('No authentication token set.');
  238.                 }
  239.                 $request['RequestToken'$this->token;
  240.                 break;
  241.             case Services_Ebay::AUTH_TYPE_USER:
  242.                 if (empty($this->requestUserId|| empty($this->requestPassword)) {
  243.                     throw new Services_Ebay_Auth_Exception('No authentication data (username and password) set.');
  244.                 }
  245.                 $request['RequestUserId']   $this->requestUserId;
  246.                 $request['RequestPassword'$this->requestPassword;
  247.                 break;
  248.             case Services_Ebay::AUTH_TYPE_NONE:
  249.                 if (empty($this->requestUserId)) {
  250.                     throw new Services_Ebay_Auth_Exception('No username has been set.');
  251.                 }
  252.                 $request['RequestUserId']   $this->requestUserId;
  253.                 break;
  254.         }
  255.  
  256.         $request array_merge($request$params);
  257.  
  258.         $this->ser->serialize($request$this->serializerOptions);
  259.         
  260.         return $this->ser->getSerializedData();
  261.     }
  262.  
  263.    /**
  264.     * send a request
  265.     *
  266.     * This method is used by the API methods. You
  267.     * may call it directly to access any eBay function that
  268.     * is not yet implemented.
  269.     *
  270.     * @access   public
  271.     * @param    string      function to call
  272.     * @param    array       associative array containing all parameters for the function call
  273.     * @return   array       response
  274.     * @todo     add real error handling
  275.     */
  276.     public function sendRequest$verb$params = array()$authType = Services_Ebay::AUTH_TYPE_TOKEN )
  277.     {
  278.         $this->wire = '';
  279.  
  280.         $body    $this->buildRequestBody($verb$params$authType);
  281.  
  282.         if (!isset($params['DetailLevel'])) {
  283.             $params['DetailLevel'$this->detailLevel;
  284.         }
  285.         
  286.         $headers = array(
  287.                             '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
  288.                             'X-EBAY-API-COMPATIBILITY-LEVEL' => $this->compatLevel,                                                 // Required. Regulates versioning of the XML interface for the API.
  289.                             '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.
  290.                             '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.
  291.                             '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.
  292.                             '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.
  293.                             'X-EBAY-API-SITEID'              => 0,                                                                  // 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.
  294.                             '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.
  295.                             '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.
  296.                             '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.
  297.                         );
  298.  
  299.         $file  SERVICES_EBAY_BASEDIR.'/Ebay/Transport/'.$this->transportDriver.'.php';
  300.         $class 'Services_Ebay_Transport_'.$this->transportDriver;
  301.  
  302.         include_once $file;
  303.         $tp = new $class;
  304.         
  305.         if ($this->debug > 0{
  306.             $this->wire .= $body."\n\n";
  307.         }
  308.  
  309.         $response $tp->sendRequest($this->url$body$headers);
  310.  
  311.         if (PEAR::isError($response)) {
  312.             return $response;
  313.         }
  314.         
  315.         if ($this->debug > 0{
  316.             $this->wire .= $response."\n\n";
  317.         }
  318.  
  319.         $this->us->unserialize$response );
  320.         
  321.         $result $this->us->getUnserializedData();
  322.  
  323.         if ($this->debug > 1{
  324.             echo $this->wire;
  325.         }
  326.         
  327.         if (isset($result['Errors'])) {
  328.             if (isset($result['Errors']['Error'])) {
  329.                 $message '';
  330.                 foreach ($result['Errors']['Error'as $error{
  331.                     $message $message ' ' $error['LongMessage'];
  332.                 }
  333.                 throw new Services_Ebay_Exception$message );
  334.             else {
  335.                 throw new Services_Ebay_API_Exception('Unknown error occured.');
  336.             }
  337.             
  338.         }
  339.  
  340.         return $result;
  341.     }
  342.     
  343.    /**
  344.     * set additional options for the serializer
  345.     *
  346.     * @param    array 
  347.     */
  348.     public function setSerializerOptions($opts = array())
  349.     {
  350.         $this->serializerOptions $opts;
  351.     }
  352. }
  353. ?>

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