Ebay使用GuzzleHttp客户端v6时不支持的API调用错误

时间:2017-05-25 13:07:11

标签: ebay-api guzzle6 guzzlehttp

我正在尝试使用ebay Trading API获取sessionid。我可以通过使用Curl成功获得会话ID,但是一旦我尝试通过Guzzle Http客户端获取会话ID,请从ebay获取以下错误

  

FailureUnsupported API调用.API调用" GeteBayOfficialTime"是   此版本无效或不受支持.2ErrorRequestError18131002

我想我使用GuzzleHttp客户端的方式存在一些问题。我目前正在使用GuzzleHttp v6和新版本。下面是我通过调用actionTest函数

来获取会话ID的代码

public function actionTest(){

$requestBody1 = '<?xml version="1.0" encoding="utf-8" ?>';
$requestBody1 .= '<GetSessionIDRequest xmlns="urn:ebay:apis:eBLBaseComponents">';
$requestBody1 .= '<Version>989</Version>';
$requestBody1 .= '<RuName>test_user-TestAs-Geforc-ldlnmtua</RuName>';    
$requestBody1 .= '</GetSessionIDRequest>';


 $headers = $this->getHeader();


      $client = new Client();

    $request = new Request('POST','https://api.sandbox.ebay.com/ws/api.dll',$headers,$requestBody1);
    $response = $client->send($request);

    /*$response = $client->post('https://api.sandbox.ebay.com/ws/api.dll', [
        'headers' => $headers,
        'body' => $requestBody1
    ]);*/

    echo $response->getBody();die;

}

public function getHeader()
{
    $header = array(
        'Content-Type: text/xml',
        'X-EBAY-API-COMPATIBILITY-LEVEL: 989',
        'X-EBAY-API-DEV-NAME: a4d749e7-9b22-441e-8406-d3b65d95d41a',
        'X-EBAY-API-APP-NAME: TestUs-GeforceI-SBX-345ed4578-10122cfa',
        'X-EBAY-API-CERT-NAME: PRD-120145f62955-96aa-4d748-b1df-6bf4',
        'X-EBAY-API-CALL-NAME: GetSessionID',
        'X-EBAY-API-SITEID: 203',
    );
    return $header;
}

Plz建议我提出要求的方式可能存在缺陷。我已经通过引用各种参考站点和guzzle官方文档尝试/修改了guzzle请求调用,但错误保持不变。

1 个答案:

答案 0 :(得分:1)

您需要按照documentation

中的说明传递一个关联的标头数组
public function getHeader()
{
    return [
        'Content-Type'                   => 'text/xml',
        'X-EBAY-API-COMPATIBILITY-LEVEL' => '989',
        'X-EBAY-API-DEV-NAME'            => '...',
        'X-EBAY-API-APP-NAME'            => '...',
        'X-EBAY-API-CERT-NAME'           => '...',
        'X-EBAY-API-CALL-NAME'           => '...',
        'X-EBAY-API-SITEID'              => '203',
    ];
}

如果您感兴趣,可以使用SDK来简化代码。如何调用GetSessionID的示例如下所示。

<?php
require __DIR__.'/vendor/autoload.php';

use \DTS\eBaySDK\Trading\Services\TradingService;
use \DTS\eBaySDK\Trading\Types\GetSessionIDRequestType;

$service = new TradingService([
    'credentials' => [
        'appId'  => 'your-sandbox-app-id',
        'certId' => 'your-sandbox-cert-id',
        'devId'  => 'your-sandbox-dev-id'
    ],
    'siteId'      => '203',
    'apiVersion'  => '989',
    'sandbox'     => true
]);

$request = new GetSessionIDRequestType();
$request->RuName = '...';

$response = $service->getSessionID($request);

echo $response->SessionID;