Apache

Creating the SOAP server with NuSOAP

Creating the SOAP server

Create a file called food.php and copy the and paste the below code into it.

Now lets break this down!

  • The first section of the above code is requiring the NuSOAP library.
  • The second part is a simple PHP class with one method called getFood
  • Next we initialise the soap_server class, configure the WDSL and register the class method.

Okay, so what are all those register parameters?

  • food.getFood is the className.methodName
  • array(“type” => “xsd:string”) defines the input argument to getFood and its data type i.e string, number, boolean
  • array(“return” => “xsd:string”) defines the method’s return value and its data type i.e string, number, boolean
  • http://www.greenacorn-websolutions.com/foodservice defines the namespace. Using a URI to identify a namespace, rather than a simple string (such as “food:webservice”), reduces the probability of different namespaces using duplicate identifiers.
  • http://www.greenacorn-websolutions.com/foodservice#getFood defines the SOAP action
  • rpc defines the type of call (this could be either rpc or document)
  • encoded defines the value for the attribute (encoded or literal could be used)
  • The last parameter is a documentation string that describes what the getFood method does

The WSDL

Now navigate to your web application via the browser. In my case I navigated to http://soap-test.dev/food.php?wsdl. Just be sure to include the ?wsdl. You should see an xml document like the one below. Save a copy of this in you web root and call it food.wsdl.

Creating the SOAP client

Create a file called foodclient.php and copy the and paste the below code into it.

Now lets break this down too!

  • Initialise nusoap_client and tell it to accept a WSDL file. We could also do this: $client = new nusoap_client(“http://soap-test.dev/food.php”); However, using a WSDL file is considered best practice.
  • The getError method checks if their were any errors whilst creating the client.
  • The call method defines the RPC method and arguments and sends the SOAP request.
  • Errors are again checked and the results are outputted.
  • The last few lines of code are used to show the SOAP request and response (ideal for debugging)

Now let’s test

Navigate to your food client, in my case http://soap-test.dev/foodclient.php. You should now see results similar to the below:

soap-test2

Working with a proxy class

A proxy class is code that looks exactly like the class it meant to represent; however the proxy class doesn’t contain any of the application logic. Instead, the proxy class contains marshalling and transport logic.

To use a proxy class with the food soap client. Simple modify foodclient.php to look like the below:

Due to the fact our methods are a part of the food class, we will need to prefix the proxy methods with food__.

Leave a Reply

Your email address will not be published. Required fields are marked *