簡體   English   中英

使用PHP調用SOAP API

[英]SOAP API call with PHP

現在為此工作了一個星期。 無法執行此代碼。 我想通過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的方式,還是我誤解了語法。 我將非常感謝任何和所有的幫助。

對標題使用對象而不是關聯數組:

$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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM