As I stated in Part One, in this tutorial my aim was to create a PHP based web services client/server. SOAP and NuSoap were the tools I chose to use after looking into my options. This tutorial is beginner level as my experience with SOAP and NuSoap prior to this was very little. This tutorial post will cover client setup and testing.
The Client
The web services server was setup and working, and so now it was time to work on the client. NuSoap installation is the same for the client site. First, download the latest NuSoap Code. Install NuSoap by 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. As with setting up a server using NuSoap, setting up a client is also very simple. The following is what I came up with and named client.php.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php require_once('nusoap.php'); $url = "http://tcwebdevelopment.com/service/server.php?wsdl"; $client = new nusoap_client($url); $err = $client->getError(); if ($err) { echo '<p><b>Error: ' . $err . '</b></p>'; } $args = array('value' => 'Good'); $return = $client->call('pollServer', array($args)); echo "<p>Value returned from the server is: " . $return . "</p>"; ?> |
Let’s take a look at some of the important lines of code here and what they do.
- Line 3 – 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 5 – Here we set a variable with the URL of our server. Note that we add ?wsdl to the end of the URL.
- Line 7 – We create a new nusoap client passing in the URL as an argument.
- Line 9 – Line 9 is where we see if there were any errors setting up the client using the nusoap client’s getError() method.
- Line 11-14 – If there were any errors let’s notify the user.
- Line 15 – The server we setup in part one of the tutorial is expecting an array with a key of value containing the value. We create the array here, giving it a value of ‘Good’ to begin.
- Line 17 – In this line we call the server using the nusoap client’s call method. The first parameter we pass to the method is the name of the exposed function we wish to use. The second is an array of the params we wish to pass to the server.
- Line 19 – Let’s print out what is returned.
After saving this file I uploaded it to another one of my websites for testing. By navigating to the location in my browser I saw the following:
As expected, the return showed “good data” being returned. Just to make sure the server is working as expected I changed line 15 to the following:
1 | $args = array('value' => 'Bad'); |
After uploading and refreshing the client location I saw the following:
It seemed that my server was returning data as expected! That’s how easy it is to create a working client/server using NuSoap and PHP for your web services. That’s all for this beginner level tutorial. There are a lot of other useful things you can do using SOAP, NuSoap, and PHP with ease!
4 Comments for NuSoap, SOAP, and PHP Web Services Tutorial – Part 2
Ricardo | May 21, 2010 at 8:25 pm
Greg | August 19, 2010 at 10:58 am
Its really great script, this i tested and $value['value'] is not giving “Good”
its giving just A and if i return $value, its giving Array.
can you please help me.







I congratulate them on the library NuSOAP there are very few tutorials and examples of use. Please clarify several doubts about all that with PHP 5.3 occurred several errors.