Using PHP and NuSOAP to connect to Web Services
Author: willem In: coding, php, tools, web developmentNuSOAP 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
Related posts:
- Using Regular Expressions - Part 3 of 3 - Examples
- Validating and sanitizing URLs, Emails, and other inputs with PHP’s filter_* functions
- A simpler way to send Text or HTML emails with attachments in PHP
- Using Regular Expressions - Part 2 of 3 - Regex in PHP
- Subdomain tracking update to the geekGa.js jQuery plugin for Google Analytics
Like this post? Subscribe to the Geekology RSS 2.0 feed!













Charl van Niekerk
October 5th, 2009 at 01:28
Looks like a nice class, pity it doesn’t throw exceptions so you have to keep checking what “getError” returns.
Most servers I’ve seen do have the SOAP extension enabled though.