Chapter 2. Make a SOAP client using PHP and NuSoap.

Table of Contents
2.1. Little interoperability demo: consume Java WSDP with NuSoap.
2.1.1. Consume a remote procedure returning simple type (document/literal).
2.1.2. Complex type handled by my document/literal web service
2.1.3. Simple NuSoap call using WSDL with my rpc/encoded web service.
2.2. More advanced interoperability demo using NuSoap: consume a .NET web service

Making a client with NuSoap is very easy, so I am just going to show you the code I used for my test page (nusoapclient.php): Use of NuSoap as a client for the RCX web service in a PHP page. You will easily be able to use this code as a template for your own program.

To consume my document/literal web service (http://www.pascalbotte.be/rcx-ws/rcx) you will need to specify the "type namespace" you will find in the WSDL (http://www.pascalbotte.be/rcx-ws/rcx?WSDL). You can use my WSDL parser at http://www.pascalbotte.be/service.php (check here if on-line) to consume another document/literal web service returning simple types. At the end of the little report generated by my parser, you can use the Type namespace value right in your client code.

2.1. Little interoperability demo: consume Java WSDP with NuSoap.

2.1.2. Complex type handled by my document/literal web service

Consume a remote procedure returning an object is still very easy. The code below is for the status() which return my RcxResponse object (see here for a description of this object).

$paramnull = array();

$responseRR = $client->call('status', $paramnull, $mynamespace);

echo $responseRR["status"];
echo $responseRR["battery"];
echo $responseRR["memory"];
echo $responseRR["currentTime"];

Next example we will see the code consuming the collInt() and collPos() remote procedure returning repectively an array of simple type (int[]), and an array of object (PosCol[]). The class PosCol is in fact a java class containing two integer value member: XPos and YPos featuring coordinates for a position.

  $responseCI = $client->call('collInt', $paramnull, $mynamespace);
  $responseCP = $client->call('collPos', $paramnull, $mynamespace);

  echo "Array of integer:<br>";

  foreach ($responseCI as $c) {
    echo "val: ".$c."<br>";
  }

  echo "<br>Array of object:<br>";

  foreach ($responseCP as $d) {
    echo "object: x-> ".$d["XPos"].", y-> ".$d["YPos"]."<br>";
  }

Now let's see how to send complex type (my RcxQuery object) to my document/literal web service. We are going to consume the queryRcx() remote procedure.

So, no real difficulties for a NuSoap client to access a Java WSDP document/literal web service. Next page it won't be the same story, you will see a way, not a real workaround, to handle the issues encountered by a NuSoap client trying to consume a more advanced document/literal .NET web service interface.

2.1.3. Simple NuSoap call using WSDL with my rpc/encoded web service.

I mention here the code using the WSDL from a NuSoap client. Note here my JWSDP web service need named params, so we have to specify the name: "String_1" for the param.

<?php
	require_once('nusoap.php');

	/* Initialize parameter */
	$namep = "your message or e-mail";
	$param = array('String_1' => $namep);  

	/* create the client for my rpc/encoded web service */
	$wsdl = "http://www.pascalbotte.be/rcx-ws-rpc/rcx?WSDL";
	// $mynamespace = "http://phonedirlux.homeip.net/types"; no more need of this...
	$client = new soapclient($wsdl, true);

	/* call readLS */
        // Let NuSoap extract the correct target namespace from the WSDL!
	$response = $client->call('readLS', $param);
?>
html code...
<?=$response?>
html code...

Even more simple: use the Proxy object. This way no need to create an array defining the param name.

<?php
	require_once('nusoap.php');

	/* Initialize parameter */
	$namep = "your message or e-mail";

	/* create the client for my rpc/encoded web service */
	$wsdl = "http://www.pascalbotte.be/rcx-ws-rpc/rcx?WSDL";
	$client = new soapclient($wsdl, true);

        /* create the proxy object */
        $proxy = $client->getProxy();

	/* call readLS */
        // Let NuSoap extract the correct target namespace
        // and name param from the WSDL 
        $response = $proxy->readLS($namep);
?>
html code...
<?=$response?>
html code...

Now if you need to send an object as a parameter, still for rpc/encoded web service, here is how to make the client using WSDL and Proxy object. In this sample I will consume my rcxQuery() remote procedure (taking and returning a RcxQuery object containing two integer type named: x and y). The SOAP procedure will echo the same object for demo purpose.

It is a very simple call, but we see here NuSoap accessing the WSDL to extract the targetnamespace, parameter name and type, and endpoint address. If you have to consume a rpc/encoded web service, the best way is to let NuSoap read the WSDL using the code above. For the time being, you still have to make a part of the job manually for consuming a document/literal web service.