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

时间:2017-02-28 00:52:34

标签: php ebay-api

我想从此代码中获取Raw XML Response。但我得到了对象表示。我喜欢将XML Response存储在一个文件中。我希望有一个解决方法。

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

2 个答案:

答案 0 :(得分:6)

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

下面的示例显示了如何创建一个使用Guzzle发送和处理响应的简单处理程序。该类能够将其保存到您指定的文件中。这比使用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
        );
    }
}

答案 1 :(得分:1)

之前我还没有使用过这个软件包,但是看看GitHub上的代码看起来\DTS\eBaySDK\Trading\Services\TradingService::getCategories会返回一个\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);
}