skip to Main Content

I want to get Raw XML Response from this code. But I am getting Object Representation. I like to store the XML Response in a file. I hope there is an workaround.

<?php   
//REQUIRED FILES INCLUSION
require_once(__DIR__.'/../../vendor/autoload.php');
require_once(__DIR__.'/../../../Config/Config.php');
//require_once(__DIR__.'/../../../Helper.php');

//NAMESPACE
use DTSeBaySDKConstants;
use DTSeBaySDKTradingServices;
use DTSeBaySDKTradingTypes;
use DTSeBaySDKTradingEnums;

//SERVICE CREATION
$Service = new ServicesTradingService([
    'credentials' => $Config['production']['credentials'],
    'sandbox'     => false,
    'siteId'      => ConstantsSiteIds::MOTORS,
    'httpOptions' => [
        'verify' => false
    ]
]);

//CATEGORY PARAMETERS
$Parameters=array(
    //'DetailLevel' => array('ItemReturnCategories'),
    'DetailLevel' => array('ReturnAll'),
    'WarningLevel' => 'High'
    );
//REQUEST 
$Request = new TypesGetCategoriesRequestType($Parameters);
$Request->RequesterCredentials = new TypesCustomSecurityHeaderType();
$Request->RequesterCredentials->eBayAuthToken = $Config['production']['authToken'];
$Response = $Service->getCategories($Request);
print_r($Response);

2

Answers


  1. I haven’t used this package before, but looking at the code on GitHub it looks like DTSeBaySDKTradingServicesTradingService::getCategories returns an instance of DTSeBaySDKTypesBaseType which contains a method called toRequestXml which you might be able to use.

    From GitHub:

    /**
     * Converts the object to a XML request string.
     *
     * @return string The XML request string.
     */
    public function toRequestXml()
    {
        return $this->toXml(self::$requestXmlRootElementNames[get_class($this)], true);
    }
    
    Login or Signup to reply.
  2. It is possible to pass your own HTTP handler to the SDK via the httpHandler configuration option. This means you can intercept the raw response body before letting the SDK parse it.

    The example below shows how to create a simple handler that uses Guzzle to send and process the response. The class is able to save it to a file that you specify. This is better than using the toRequestXml method as that does not give you the actual XML sent by eBay. It gets the object to generate the XML and therefore will be different to the eBay response.

    <?php
    require __DIR__.'/vendor/autoload.php';
    $config = require __DIR__.'/configuration.php';
    
    use DTSeBaySDKConstants;
    use DTSeBaySDKTradingServices;
    use DTSeBaySDKTradingTypes;
    use DTSeBaySDKTradingEnums;
    
    use GuzzleHttpClient;
    use GuzzleHttpClientInterface;
    use PsrHttpMessageRequestInterface;
    use PsrHttpMessageResponseInterface;
    
    class ResponseLogger
    {
        private $client;
        private $logPath;
    
        public function __construct($logPath)
        {
            $this->logPath = $logPath;
            $this->client = new Client();
        }
    
        /**
         * This will be called by the SDK and will handle sending the request to the API
         * Because of this it will be able to handle saving the response to a file.
         */
        public function __invoke(RequestInterface $request, array $options)
        {
            return $this->client->sendAsync($request)->then(
                function (ResponseInterface $response) use ($request) {
                    $stream = $response->getBody();
                    file_put_contents($this->logPath, $stream);
                    /**
                     * We have to rewind to the start of the steam before giving back to the SDK to process!
                     * If we don't the SDK will try and parse from the end of the response body.
                     */
                    $stream->rewind();
    
                    return $response;
                }
            );
        }
    }
    
    $service = new ServicesTradingService([
        'credentials' => $config['production']['credentials'],
        'authToken'   => $config['production']['authToken'],
        'siteId'      => ConstantsSiteIds::MOTORS,
        'httpHandler' => new ResponseLogger(__DIR__.'/categories.xml')
    ]);
    
    $response = $service->getCategories(
        new TypesGetCategoriesRequestType([
            'DetailLevel'  => ['ReturnAll'],
            'WarningLevel' => 'High'
        ])
    );
    
    if (isset($response->Errors)) {
        foreach ($response->Errors as $error) {
            printf(
                "%s: %sn%snn",
                $error->SeverityCode === EnumsSeverityCodeType::C_ERROR ? 'Error' : 'Warning',
                $error->ShortMessage,
                $error->LongMessage
            );
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search