How to use SOAP client in PHP

This tutorial will helps you to understand the way of invoking SOAP Web Service from PHP SOAP Client.

Before Start using PHP Soap client, You need to verify that Whether SOAP Client is enabled in your PHP Web Server. To verify that create new php file with the following code.

[php]
<?php
phpinfo();
?>
[/php]

Now run it, You will get the list of configuration details of your PHP server. In that verify whether SOAP section available and SOAP client is enabled. If SOAP section is enabled in your PHP server, its look like this.

PHP SOAP Client

PHP SOAP Client

If SOAP client is enabled you can proceed the steps further, Else you need to enable the PHP SOAP client in your PHP Web Server.

Note: If you are using Shared hosting and you will not be able to enable / change the PHP Server configurations. So you can use another alternative PHP SOAP client is nusoap.

Steps to invoke on SOAP Web Service in PHP:

To invoke SOAP Web Service, you need to aware about the following items:

1) WSDL/WADL location of your Web Service to invoke from PHP.

2) Actions available in the respective Web Service.

3) Parameters are required to invoke Web Service with the respective actions in it.

4) Optional, If its have any header information like authentication / namespace of Web Service.

Simple Web Service Method:

[php]

$soapClient = new SoapClient("https://test.soapws.com/globalweather.php?wsdl");
<pre>// Setup the parameters of the action
$requestParams = array(
    ‘CityName’ => ‘Chennai’,
    ‘CountryName’ => ‘India’
);

// Call Soap action
$soapResponse = $soapClient->GetWeather($requestParams);</pre>

// Soap Response
print_r($soapResponse);

[/php]

As the result of invoking the Web Service, you will get response as stdClass Object. Now you can easily iterate the result-sets and display it however you like.

Headers with Web Service Method:

[php]

$soapClient = new SoapClient("https://test.soapws.com/globalweather.php?wsdl");

// Setup SoapHeader parameters
$headerParams = array(
            ‘Username’    =>    ‘username’,
            ‘Password’    =>    ‘password’);
$soapHeaders = new SoapHeader(‘https://test.soapws.com/weather’, ‘UserCredentials’, $headerpParams);

// Prepare Soap Client with headers
$soapClient->__setSoapHeaders(array($soapHeaders));

// Setup the parameters of the action
$requestParams = array(
    ‘CityName’ => ‘Chennai’,
    ‘CountryName’ => ‘India’
);

// Call Soap action
$soapResponse = $soapClient->GetWeather($requestParams);

// Soap Response
print_r($soapResponse);

[/php]

As the result of invoking Web Service, you will get the response from the Web Service as stdClass Object.

To verify the SOAP Client response you can use SOAPUI tool.

Permanent link to this article: https://blog.openshell.in/2014/06/how-to-use-soap-client-in-php/

Leave a Reply

Your email address will not be published.