使用PHP调用SOAP API

时间:2012-11-07 23:20:57

标签: php api soap call

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

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

XML SAMPLE CODE

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的方式,还是我误解了语法。我非常感谢任何帮助。

1 个答案:

答案 0 :(得分: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));