【问题标题】:SOAP API call with PHP使用 PHP 调用 SOAP API
【发布时间】:2012-10-28 02:46:22
【问题描述】:

现在已经为此工作了一周。执行此代码时遇到问题。我想通过 SOAP 检索数据并在 PHP 中使用它。我的问题是,我无法发送“RequesterCredentials”。

我将显示 xml 代码,以便大家可以看到我尝试发送的信息,然后是我正在使用的 PHP 代码。

XML 示例代码

POST /AuctionService.asmx HTTP/1.1
Host: apiv2.gunbroker.com
Content-Type: text/xml; charset=utf-8
Content-Length: 200
SOAPAction: "GunBrokerAPI_V2/GetItem"

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
 <soap:Header>
   <RequesterCredentials xmlns="GunBrokerAPI_V2">
     <DevKey>devkey</DevKey>
     <AppKey>appkey</AppKey>
   </RequesterCredentials>
 </soap:Header>
 <soap:Body>
<GetItem xmlns="GunBrokerAPI_V2">
  <GetItemRequest>
    <ItemID>312007942</ItemID>
    <ItemDetail>Std</ItemDetail>
  </GetItemRequest>
</GetItem>

我用来拨打电话的 PHP 代码

$client = new SoapClient("http://apiv2.gunbroker.com/AuctionService.asmx?WSDL");

$appkey = 'XXXXXX-XXXXXX-XXXXXX';
$devkey = 'XXXXXX-XXXXXX-XXXXXX';

$header = new SoapHeader('GunBrokerAPI_V2','RequesterCredentials',array('DevKey' => $devkey,'AppKey' => $appkey),0);
$client->__setSoapHeaders(array($header));

$result = $client->GetItem('312343077');

echo '<pre>',print_r($result,true),'</pre>';

我得到的结果

stdClass Object
(
[GetItemResult] => stdClass Object
    (
        [Timestamp] => 2012-11-07T18:17:31.9032903-05:00
        [Ack] => Failure
        [Errors] => stdClass Object
            (
                [ShortMessage] => GunBrokerAPI_V2 Error Message : [GetItem]
You must fill in the 'RequesterCredentialsValue' SOAP header for this Web Service method.
                [ErrorCode] => 1
            )
// the rest if just an array of empty fields that I could retrieve if i wasnt havng problems.

我不确定问题是我发送 SoapHeaders 的方式还是我误解了语法。我将不胜感激。

【问题讨论】:

  • Code 之前大喊大叫非常令人不安。
  • 您正在发送 RequesterCredentials,而 ws 正在请求 RequesterCredentialsValue
  • 只是想评论完全相同的事情。阅读错误消息,它包含一些信息 RequesterCredentialsValue is missing 并且来自您的代码:未设置。所以错误信息在我看来很好。
  • 不幸的是,更改为 RequesterCredentialsValue 会得到相同的结果。似乎它没有随通话一起发送
  • 也许我在 SoapHeader 的第一个参数中使用的 URL 没有意义?

标签: php api soap call


【解决方案1】:

标题使用对象而不是关联数组:

$obj = new stdClass();

$obj->AppKey = $appkey;
$obj->DevKey = $devkey;

$header = new SoapHeader('GunBrokerAPI_V2','RequesterCredentials',$obj,0);

你可能面临的下一个问题是GetItem 调用,你还需要对象,包裹在关联数组中:

$item = new stdClass;
$item->ItemID = '312343077';
$item->ItemDetail = 'Std';

$result = $client->GetItem(array('GetItemRequest' => $item));

【讨论】:

  • 做得很好。我把这段代码放进去,它就像一个魅力。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
相关资源
最近更新 更多