How to use PHP and NuSOAP to connect to web services
NuSOAP is a rewrite of SOAP contained in a set of PHP classes that don't require a PHP Extension (in contrast, SOAP requires that PHP be compiled with the "–enable-soap" option).
The examples below use the NuSOAP library to connect to a WSDL-based (Web Services Description Language) web service hosted on an IIS server:
To perform a function call without parameters or user authentication:
<?php
//include the NuSOAP class file:
require 'lib/nusoap.php';
//instantiate the NuSOAP class and define the web service URL:
$client = new nusoap_client('http://www.someserver.com/service.svc?wsdl', 'WSDL');
//check if there were any instantiation errors, and if so stop execution with an error message:
$error = $client->getError();
if ($error) {
die("client construction error: {$error}\n");
}
//perform a function call without parameters:
$answer = $client->call('TestFunction');
//check if there were any call errors, and if so stop execution with some error messages:
$error = $client->getError();
if ($error) {
print_r($client->response);
print_r($client->getDebug());
die();
}
//output the response (in the form of a multidimensional array) from the function call:
print_r($answer);
?>
To perform a function call without parameters but with user authentication:
<?php
//include the NuSOAP class file:
require 'lib/nusoap.php';
//instantiate the NuSOAP class and define the web service URL:
$client = new nusoap_client('http://www.someserver.com/service.svc?wsdl', 'WSDL');
//check if there were any instantiation errors, and if so stop execution with an error message:
$error = $client->getError();
if ($error) {
die("client construction error: {$error}\n");
}
//authenticate to the service:
$client->setCredentials('usern', 'passw', 'digest');
//perform a function call without parameters:
$answer = $client->call('TestFunction');
//check if there were any call errors, and if so stop execution with some error messages:
$error = $client->getError();
if ($error) {
print_r($client->response);
print_r($client->getDebug());
die();
}
//output the response (in the form of a multidimensional array) from the function call:
print_r($answer);
?>
To perform a function call with parameters and user authentication:
<?php
//include the NuSOAP class file:
require 'lib/nusoap.php';
//instantiate the NuSOAP class and define the web service URL:
$client = new nusoap_client('http://www.someserver.com/service.svc?wsdl', 'WSDL');
//check if there were any instantiation errors, and if so stop execution with an error message:
$error = $client->getError();
if ($error) {
die("client construction error: {$error}\n");
}
//authenticate to the service:
$client->setCredentials('usern', 'passw', 'digest');
//perform a function call with parameters:
$param = array('UserName' => 'usern',
'Password' => 'passw',
'GUID' => 'AAAAA-BBB-CCCCC-DD0',
);
$answer = $client->call('TestFunction', array('parameters' => $param), '', '', false, true);
//check if there were any call errors, and if so stop execution with some error messages:
$error = $client->getError();
if ($error) {
print_r($client->response);
print_r($client->getDebug());
die();
}
//output the response (in the form of a multidimensional array) from the function call:
print_r($answer);
?>
After a function call has been made by any of the above examples, they will output the result as a multidimensional array:
Array
(
[TestFunctionResult] => Array
(
[Client] => Array
(
[0] => Array
(
[_id] => 1
[_name] => Jack
)
[1] => Array
(
[_id] => 2
[_name] => Jill
)
)
)
)
The sample files bundled with NuSOAP include several examples of how the library can be used with different clients, including:
- MIME
- SSL
- WSDL