| A Site For And By Web Developers



NuSoap, SOAP, and PHP Web Services Tutorial – Part 1

Recently I had a need to create some PHP code that would contact a remote site for information. This is something I have not done in quite some time and have very little experience with. After looking at some of the options, I chose to do this with SOAP and more specifically NuSoap. This is a beginner level tutorial based on some of the things I played around with and eventually got to work as I wanted them to. This post will cover initial planning and the setting up of the server portion of the project.


The first thing I did for this project was to get an idea of exactly what I wanted to happen. My primary goal was to expand my knowledge and to give myself a good base to build off of to create something that was actually useful. I decided I would first build the server portion of the project. This would be housed at one of my websites (public.) The second part of the project would be to create a client that would reside in another location (url/server.) While it did take some tinkering to make this work with the information I had available, I was able to create a working model.

The Server

To setup the server, the first thing I did was download the latest NuSoap Code. Installation is a matter of simply extracting the PHP files that are needed and putting them somewhere accessible. It is useful to place the class files in a place within your PHP path. The main NuSoap file (nusoap.php) can be placed somewhere more convenient. Using NuSoap, creating a web service in PHP is very simple. To begin, I created a file called server.php to house the server portion of my project. The following is the end result of my tinkering.

$server = new nusoap_server;
$server->configureWSDL('server', 'urn:server');
$server->wsdl->schemaTargetNamespace = 'urn:server';
			array('value' => 'xsd:string'),
			array('return' => 'xsd:string'),
function pollServer($value){
        if($value['value'] == 'Good'){
	     return "The value of the server poll resulted in good information";
             return "The value of the server poll showed poor information";

Let’s take a look at some of the important lines of code here and what they do.

  • Line 2 – We include the nusoap.php file. The path to the file itself needs to be used here. This will give us access to all of the NuSoap features we will be using.
  • Line 4 – This is where we create an instance of a nusoap_server. This is the class that essentially makes working with PHP’s implementation of SOAP easier
  • Line 6 – Here we are using the configureWSDL function of the nusoap_server class to internally configure the WSDL file. Nusoap is really nice in that it is able to generate this for you
  • Line 8 – In this line we add the schema namespace to the WSDL information. This could be done in the function call of line 6 but I find this to be easier to follow
  • Line 10-14 – Here we are registering our function. We are also defining some of our values because PHP is loosely typed. The name of our exposed function will be pollServer. This is our first parameter. It will accept an array of values, currently one will be called value and will contain a string. This is our 2nd parameter. It will return an array of values, currently one will be called return and contain a string. This is our third parameter. Our fourth parameter is the namespace. The fifth is SOAP action for the exposed function. I chose not to include some of the other parameters in this example.
  • Line 16-26 – This is the function that we will be exposing. Pretty simple here, but you can see where we could poll a database or look in an array to take different courses of action (return different values).
  • Line 28 – Here we set the variable we will be sending to the service or make sure it is null so that it is at least defined
  • Line 30 – This is the line of code that sets up executing any incoming requests

Once that file was created, I uploaded it to a convenient place on my server. By navigating to that place within a web browser I was able to verify that my server was working. It looked something like this:

By clicking on the pollServer link I was able to see some information about the method I had exposed:

By clicking on the WSDL link, I was able to view the WSDL (XML) that NuSoap was generating for me. It’s code is as follows:

<definitions targetNamespace="urn:server"><types><xsd:schema targetNamespace="urn:server">
<xsd:import namespace=""/>
<xsd:import namespace=""/>
</types><message name="pollServerRequest">
<part name="value" type="xsd:string"/>
</message><message name="pollServerResponse">
<part name="return" type="xsd:string"/>
</message><portType name="serverPortType"><operation name="pollServer">
<input message="tns:pollServerRequest"/>
<output message="tns:pollServerResponse"/>
</portType><binding name="serverBinding" type="tns:serverPortType">
<soap:binding style="rpc" transport=""/><operation name="pollServer">
<soap:operation soapAction="urn:server#pollServer" style="rpc"/><input>
<soap:body use="encoded" namespace="urn:server" encodingStyle=""/>
<soap:body use="encoded" namespace="urn:server" encodingStyle=""/>
</binding><service name="server"><port name="serverPort" binding="tns:serverBinding">
<soap:address location=""/>

That is all that it took to create a fully functional web services server. In my next post I will go over setting up a client. It is also amazingly easy!

· · · · ·


  • JC · May 11, 2010 at 10:29 am

    This is great, when is part 2 coming out? :)

  • Author comment by johng · May 12, 2010 at 2:11 am

    I should be getting to it sometime in the next week, trying to finish up a big CiviCRM project at the moment. Stay tuned and thanks for dropping by!

  • NuSoap, SOAP, and PHP Web Services Tutorial - Client | · May 20, 2010 at 12:27 pm

    [...] I stated in Part One, in this tutorial my aim was to create a PHP based web services client/server. SOAP and NuSoap were [...]

  • Farshid Alavi Moghadam · December 22, 2010 at 6:35 am

    That was great man, I will be waiting for part 2….

Leave a Reply



Theme Design by