Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SOAP API call with PHP

Tags:

php

soap

call

api

I have been working on this for a week now and have trouble executing this code. I want to retrieve data via SOAP and work with it in PHP. My trouble is, that I am having trouble sending the 'RequesterCredentials'.

I will show the XML code so you all can see the information I am trying to send, and then the PHP code I am using.

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>
  </soap:Body>
</soap:Envelope>

PHP code that I am using to make 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>';

The result I get

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.

I’m not sure if the problem is the way I’m sending the SoapHeaders or if I am misunderstanding the syntax. How can I fix it?

like image 469
Shawn Abramson Avatar asked Feb 18 '23 14:02

Shawn Abramson


1 Answers

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 might face will be at 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));
like image 99
dev-null-dweller Avatar answered Feb 26 '23 20:02

dev-null-dweller