Using 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

 

Related posts:

  1. Validating and sanitizing URLs, Emails, and other inputs with PHP’s filter_* functions
  2. Using Regular Expressions – Part 2 of 3 – Regex in PHP
  3. Using Regular Expressions – Part 3 of 3 – Examples
  4. Subdomain tracking update to the geekGa.js jQuery plugin for Google Analytics
  5. A simpler way to send Text or HTML emails with attachments in PHP
Twitter Digg Delicious Stumbleupon Technorati Facebook Email

3 Responses to “Using PHP and NuSOAP to connect to Web Services”

  1. 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.

  2. Hi All,

    Is it possible to authenticate (Using certificate) a .NET based web service using nuSoap.I am very new to Web Service and any help will be much appreciated.

    Thanks,
    Amardeep

Trackbacks/Pingbacks

  1. Renato Moya (renatomoya) « Using PHP and NuSOAP to connect to Web Services | Geekology « Chat Catcher - 09 Jun 2010

    [...] 2010-06-09T11:50:02  Using PHP and NuSOAP to connect to Web Services: [link to post] [...]

Afrigator