我已经为此工作了一个星期,但在执行这段代码时遇到了困难。我想通过 SOAP 检索数据并在 PHP 中使用它。我的问题是,我无法发送
RequesterCredentials
。
我将显示 XML 代码,以便大家可以看到我尝试发送的信息,然后是我正在使用的 PHP 代码。
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>
</soap:Body>
</soap:Envelope>
$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 wasn’t having problems.
我不确定问题是我发送 SoapHeaders 的方式还是我误解了语法。我该如何解决它?
使用对象代替标题的关联数组:
$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));