4

现在为此工作了一周。执行此代码时遇到问题。我想通过 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 的方式还是我误解了语法。我将不胜感激任何和所有的帮助。

4

1 回答 1

8

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

$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));
于 2012-11-08T18:14:13.867 回答