XML 使用 ebay-sdk 的原始响应-php

XML Raw Response using ebay-sdk-php

我想从这段代码中获取原始 XML 响应。但是我得到了对象表示。我喜欢将 XML 响应存储在一个文件中。我希望有解决方法。

<?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);

我以前没有使用过这个包,但是查看 GitHub 上的代码看起来 \DTS\eBaySDK\Trading\Services\TradingService::getCategories returns an instance of \DTS\eBaySDK\Types\BaseType 包含一个名为 toRequestXml 的方法,您可以使用它.

来自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);
}

可以通过 httpHandler 配置选项将您自己的 HTTP 处理程序传递给 SDK。这意味着您可以在让 SDK 解析它之前拦截原始响应主体。

下面的示例展示了如何创建一个使用 Guzzle 发送和处理响应的简单处理程序。 class 能够将其保存到您指定的文件中。这比使用 toRequestXml 方法要好,因为它不会为您提供 eBay 发送的实际 XML。它获取生成 XML 的对象,因此将不同于 eBay 响应。

<?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
        );
    }
}