Interacting with a soap API without WSDL in PHP

Viewed 8409

Recently got access to a soap API for a hotel management service. They have provided documentation which shows a basic example of a request:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">    
<soapenv:Header>       
  <Auth xmlns="http://xxxx/xxxxAPI">          
  <FromSystemId ID="1">CompanyName</FromSystemId>          
    <UserName>username</UserName>          
    <Password>password</Password>       
  </Auth>    
</soapenv:Header>    
<soapenv:Body>       
  <GetRegions Timestamp="2016-04-11" Version="1.0" Lang="en" 
     xmlns="http://xxxx/xxxxAPI">
  <Country Code="GB" />  
  </GetRegions>    
</soapenv:Body> 
</soapenv:Envelope>

They have also provided a list of functions in their documentation and the parameters required for each of the functions. But i am abit confused on how to perform a request as i have never used a soap API before. They also haven't provided a WSDL, does this matter?

Anyway, here is how i thought to try and perform a request

$soapURL = "http://xxxx/xxxxAPI" ;
$soapParameters = Array('login' => "username", 'password' => "password") ;
$soapFunction = "getRegions";
$soapFunctionParameters = Array('countrycode' => 'GB');

$soapClient = new SoapClient($soapURL, $soapParameters);

$soapResult = $soapClient->__soapCall($soapFunction, 
$soapFunctionParameters) ;

if(is_array($soapResult) && isset($soapResult['someFunctionResult'])) {
    // Process result.
} else {
    // Unexpected result
    if(function_exists("debug_message")) {
        debug_message("Unexpected soapResult for {$soapFunction}: ".print_r($soapResult, TRUE)) ;
    }
}

Am i going about this the right way? i am unable to test this right now as i haven't received my authentication but wanted to make a start on it now.

Any help would be great.

1 Answers

Here is a small example.

$opts = array(
    'location' => 'http://xxxx/xxxxAPI',
    'uri' => 'urn:http://test-uri/'
);

$client = new SOAPClient(null, $opts);

$headerData = array(
    'FromSystemId' => 'CompanyName',
    'UserName' => 'username',
    'Password' => 'password',
);

// Create Soap Header.
$header = new SOAPHeader('http://xxxx/xxxxAPI', 'Auth', $headerData);

// Set the Headers of Soap Client.
$client->__setSoapHeaders($header);


$result = $client->__soapCall('getRegions', array('GB'));

// $return = $client->__soapCall('getRegions', array(new SoapParam(new SoapVar('GB', XSD_STRING), 'countryCode')));

var_dump($result);

They also haven't provided a WSDL, does this matter?

To be able to add the HEADER attributes they must be mentioned in WSDL. If they not exist in WSDL they WILL NOT appear as attributes but rather <item><key/><value/></item> elements.

Tipp: If you know how the request has to be and you have no WSDL, then try to generate the HTTP header and XML body manually and execute the request with CURL or Guzzle.

Example with Guzzle:

$soapContent = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">    
<soapenv:Header>       
  <Auth xmlns="http://xxxx/xxxxAPI">          
  <FromSystemId ID="1">CompanyName</FromSystemId>          
    <UserName>username</UserName>          
    <Password>password</Password>       
  </Auth>    
</soapenv:Header>    
<soapenv:Body>       
  <GetRegions Timestamp="2016-04-11" Version="1.0" Lang="en" 
     xmlns="http://xxxx/xxxxAPI">
  <Country Code="GB" />  
  </GetRegions>    
</soapenv:Body> 
</soapenv:Envelope>';

$client = new GuzzleHttp\Client([
    'headers' => [ 'SOAPAction' => '"urn:http://xxxx/xxxxAPI/#getRegions"' ]
]);

$response = $client->post('http://xxxx/xxxxAPI',
    ['body' => $soapContent]
);

echo $response;
Related