Code Valley Code Valley -4 years ago 197
PHP Question

XML Raw Response using ebay-sdk-php

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 \DTS\eBaySDK\Constants;
use \DTS\eBaySDK\Trading\Services;
use \DTS\eBaySDK\Trading\Types;
use \DTS\eBaySDK\Trading\Enums;

//SERVICE CREATION
$Service = new Services\TradingService([
'credentials' => $Config['production']['credentials'],
'sandbox' => false,
'siteId' => Constants\SiteIds::MOTORS,
'httpOptions' => [
'verify' => false
]
]);

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

Answer Source

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 \DTS\eBaySDK\Constants;
use \DTS\eBaySDK\Trading\Services;
use \DTS\eBaySDK\Trading\Types;
use \DTS\eBaySDK\Trading\Enums;

use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

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 Services\TradingService([
    'credentials' => $config['production']['credentials'],
    'authToken'   => $config['production']['authToken'],
    'siteId'      => Constants\SiteIds::MOTORS,
    'httpHandler' => new ResponseLogger(__DIR__.'/categories.xml')
]);

$response = $service->getCategories(
    new Types\GetCategoriesRequestType([
        'DetailLevel'  => ['ReturnAll'],
        'WarningLevel' => 'High'
    ])
);

if (isset($response->Errors)) {
    foreach ($response->Errors as $error) {
        printf(
            "%s: %s\n%s\n\n",
            $error->SeverityCode === Enums\SeverityCodeType::C_ERROR ? 'Error' : 'Warning',
            $error->ShortMessage,
            $error->LongMessage
        );
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download