【问题标题】:Ebay API returns "Service operation is unknown" and "Missing SOA operation name header" errors when using PHPEbay API 在使用 PHP 时返回“服务操作未知”和“缺少 SOA 操作名称标头”错误
【发布时间】:2023-03-28 12:03:01
【问题描述】:

我想对 eBay api 进行肥皂调用并按关键字查找产品。在eBay documentation和其他在线资源的帮助下,我想出了这段代码:

$client = new \SoapClient('http://developer.ebay.com/webservices/finding/latest/FindingService.wsdl');
$soap_headers = array(
    new \SoapHeader('X-EBAY-SOA-OPERATION-NAME', 'findItemsByKeywords'),
    new \SoapHeader('X-EBAY-SOA-SERVICE-VERSION', '1.3.0'),
    new \SoapHeader('X-EBAY-SOA-REQUEST-DATA-FORMAT', 'XML'),
    new \SoapHeader('X-EBAY-SOA-GLOBAL-ID', 'EBAY-US'),
    new \SoapHeader('X-EBAY-SOA-SECURITY-APPNAME', '<there would be the key>'),
);
$client->__setSoapHeaders($soap_headers);

// Call wsdl function
$result = $client->__soapCall("findItemsByKeywords", array("keywords" => "Potter"));

但是,此代码会导致错误: "服务操作未知, 500 内部服务器错误 - SoapFault"

我试着把第一行改成这样(不知道为什么会有所不同,但我在某处看到过):

$client = new \SoapClient(NULL, array(
"location" => 'http://svcs.sandbox.ebay.com/services/search/FindingService/v1', 
'uri' => 'http://svcs.sandbox.ebay.com/services/search/FindingService/v1')
);

现在这会导致这个错误:缺少 SOA 操作名称标头, 500 内部服务器错误 - SoapFault

有人知道导致这些错误发生的原因以及如何解决这些错误吗?

谢谢你,迈克!

【问题讨论】:

  • @Cray 很抱歉,我拒绝了您的编辑。我以为是审计。我为此道歉。您的编辑根本不是垃圾邮件或破坏行为。以后我会尽量减少对审计的敏感度。

标签: php soap ebay-api


【解决方案1】:

以下代码有效。

define('APP_ID', '*** Your App ID ***');
$wsdl = 'http://developer.ebay.com/webservices/finding/latest/FindingService.wsdl';
// For sandbox
$endpoint_uri = 'http://svcs.sandbox.ebay.com/services/search/FindingService/v1';
// For production
//$endpoint_uri = 'http://svcs.ebay.com/services/search/FindingService/v1';

// We'll use this namespace explicitly for the 'keywords' tag,
// because the SoapClient doesn't apply it automatically.
$ns = 'http://www.ebay.com/marketplace/search/v1/services';
// The SOAP function
$operation = 'findItemsByKeywords';

$http_headers = implode(PHP_EOL, [
  "X-EBAY-SOA-OPERATION-NAME: $operation",
  "X-EBAY-SOA-SECURITY-APPNAME: " . APP_ID,
]);

$options = [
  'trace' => true,
  'cache' => WSDL_CACHE_NONE,
  'exceptions' => true,
  'location' => $endpoint_uri,
  //'uri' => 'ns1',
  'stream_context' => stream_context_create([
    'http' => [
      'method' => 'POST',
      'header' => $http_headers,
    ]
  ]),
];

$client = new \SoapClient($wsdl, $options);

try {
  $wrapper = new StdClass;
  $wrapper->keywords = new SoapVar('harry potter', XSD_STRING,
    null, null, null, $ns);

  $result = $client->$operation(new SoapVar($wrapper, SOAP_ENC_OBJECT));
  var_dump($result);
} catch (Exception $e) {
  echo $e->getMessage();
}

如前所述,问题之一是您将X-EBAY-SOA-* 值作为SOAP 标头传递。 service expects 它们作为 HTTP 标头:

HTTP 标头或 URL 参数——每个 Find API 调用都需要特定的 HTTP 标头或 URL 参数。例如,您必须指定您的 X-EBAY-SOA-SECURITY-APPNAME 标头或 SECURITY-APPNAME 中的 AppID 网址参数。同样,您必须始终在 X-EBAY-SOA-OPERATION-NAME 标头或OPERATION-NAME URL 参数。 其他标头是可选的或有条件要求的。

第二个问题是没有指定SoapClientlocation选项。它应该包含一个指向service endpoints 之一的URI。否则,API 返回Service operation is unknown 错误。

最后,SoapClient 没有将keywords 放入 Ebay 的命名空间:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.ebay.com/marketplace/search/v1/services">
  <SOAP-ENV:Body>
    <ns1:findItemsByKeywordsRequest>
      <keywords>harry potter</keywords>
    </ns1:findItemsByKeywordsRequest>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

因此,API 返回错误:Keywords value required.。所以我明确指定了keywords 标签的命名空间。

还要注意沙盒和生产环境使用不同的端点 URI。

【讨论】:

  • 感谢您的代码和解释!终于可以让它运行了!
【解决方案2】:

问题是,服务需要 HTTP 标头(或查询字符串参数,显然,通过阅读他们的指南)。

使用__setSoapHeaders,您可以设置 SOAP 标头。

例如试试这个

$wsdl = 'http://developer.ebay.com/webservices/finding/latest/FindingService.wsdl';

$appID = 'YOUR KEY/ID';

$headers = implode(PHP_EOL, [
  "X-EBAY-SOA-OPERATION-NAME: findItemsByKeywords",
  "X-EBAY-SOA-SECURITY-APPNAME: $appID"
]);

$options = [
  'trace' => true,
  'cache' => WSDL_CACHE_NONE,
  'exceptions' => true,
  'stream_context' => stream_context_create([
    'http' => [
      'header' => $headers
    ]
  ]),
];

$client = new SoapClient($wsdl, $options);

$response = $client->findItemsByKeywords([
  'keywords' => 'Potter'
]);

【讨论】:

  • 嗨亚历哈德罗。我试过了,代码验证失败,所以我发现有必要在标题之间添加逗号。无论如何,现在出现与以前相同的错误:“服务操作 findItemsByKeywords,未知”,并且在 _soapCall(..) 方法上失败。
  • @Mike 是的,因为逗号,服务现在正在尝试查找findItemsByKeywords,。去掉逗号。标题应由换行符分隔。查看更新的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-22
  • 1970-01-01
  • 2020-09-04
  • 2021-09-29
  • 1970-01-01
  • 2022-01-04
相关资源
最近更新 更多