SOAP API call with PHP

Work on this for a week. Failed to execute this code. I want to get data through SOAP and work with it in PHP. My problem is that I am having trouble sending "RequesterCredentials".

I will show the xml code so that you can see the information I'm trying to send, and then the PHP code that I use.

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> 

The php code I use to create the call

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

RESULT OBTAINED

 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. 

I'm not sure if the problem is how Im sending SoapHeaders or I don't understand the syntax. I would really appreciate any help.

+4
source share
1 answer

Use an object instead of an associative array for headers:

 $obj = new stdClass(); $obj->AppKey = $appkey; $obj->DevKey = $devkey; $header = new SoapHeader('GunBrokerAPI_V2','RequesterCredentials',$obj,0); 

And the next problem you may encounter will be in the GetItem call, you also need an object wrapped in an associative array:

 $item = new stdClass; $item->ItemID = '312343077'; $item->ItemDetail = 'Std'; $result = $client->GetItem(array('GetItemRequest' => $item)); 
+8
source

Source: https://habr.com/ru/post/1444681/


All Articles