PHP consume a SOAP service (no-WSDL mode)

By kenneth, Wed, 11/01/2017 - 23:00

Authored on

Image
rails

There is a documentation on official PHP website about how to consume a SOAP service by using "SoapClient" class. I faced a no-WSDL mode integration, here I will share the way I achieved it.

Setup parameters

Firstly I setup all parameters as an array of "SoapParam" objects where I defined into their constructors as first parameter a "SoapVar" object with its value and type, (A list with a predefined constants will be find here) also setup the SoapParam name as second parameter.

// Setup Soap parameters
$params = [
    new SoapParam(
        new SoapVar($name, XSD_STRING),
        'name'
    ),
    new SoapParam(
        new SoapVar($amount, XSD_INT),
        'amount'
    ),
];

 

Setup SoapClient

Then I create a SoapClient with some options parameters to define the way how it will works, where $location and $uri are basically the web service endpoint URL, at least, It worked for me, however as documentation said those values are:

The location is the URL of the SOAP server to send the request to, 
and uri is the target namespace of the SOAP service. 

Then It depends on web service definition, it would be good to contact who is on charge of that service to be sure of those values.

// Define SOAP Client options.
$options = [
    'location'     => $location,
    'uri'          => $uri,
    'trace'        => true,
    'cache_wsdl'   => WSDL_CACHE_NONE,
    'soap_version' => SOAP_1_2,
    'ssl_method'   => SOAP_SSL_METHOD_SSLv23,
];
// Init variables response and client.
$response = [];
$client = new SoapClient(null, $options);

 

Consume WebService method

At the end, I execute command to consume a web service method I need to, I added a try-catch statement to be sure I will be able to catch SoapFault exception if any,

// Consume "webServiceMethodName" method.
try {
    $response = $client->__soapCall('webServiceMethodName', $data);
} catch (SoapFault $exception) {
    // Catch an exception.
}

 

Those are all steps I did in order to consume a SOAP Web service what is defined as no-WSDL mode.

Hopefully it helps to anyone who wants/needs to achieve it,

Comments1

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

hanghtedan (not verified)

04/02/2020 - 00:32:17

Hi cool post.