Working with Web Services using Soap and PHP

Web services are an important part of communication across the web. SOAP provides a standard to work with when interfacing with different applications. In this tutorial we look at creating a simple SOAP client and server using PHP and nusoap.php library. We also used SoapUI for testing which is a really great application for web services testing.

The first thing you need to do is download the nusoap.php library. You can find that here.

Once you do that you can create two folders one for the client and the other for the server. You can create index.php files in each and point your server to these paths.


Lets walk through the server code. You can also view it online here.

In the server/index.php file the first thing we need to do is require our nusoap library.

require_once "../vendors/lib/nusoap.php";

Next we have a function. This function represents a mock data call. We use this function to get information from a file, database or another source. For this example we just use an array.

function get_price($name)
    $products = [
        "book" => 20,
        "pen"=> 10,
        "pencil" => 5

   if(array_key_exists($name, $products)){
       return $products[$name];

Next we create our server instance.

$server = new nusoap_server();

We have to add some configurations.

$server->configureWSDL("Soap Demo","urn:soapdemo");

Now for the interesting part. We register the function we created earlier.

    "get_price", // name of function
    array("name"=>"xsd:string"),  // inputs
    array("return"=>"xsd:integer")   // outputs

Finally we call the service getting the full contents of the request using php://input.


That’s the end of the server application. Depending on how your server is setup navigate to it in the browser.

Next we create the client. We can find the code online here.

We require our library as per usual.

require_once "../vendors/lib/nusoap.php";

We create the client putting in our url for the server. The url will vary for you. The most important part is “?wsdl”. We it’s all important really :).

$client = new nusoap_client("");

Now we can call the service we would like to use. In this case the get_price function. We pass in the parameters to the function as an array.

$response = $client->call('get_price',array("name"=>'book'));

We then empty the response on the page.


You have just created a SOAP web service in PHP.

Lets try out SoapUI for testing. I use a linux OS so your version might look different.

Lets first download SoapUI. Head to this link. Once your are done we can start a new project.

Starting a new project

We enter the project name and the initial WSDL which is the URL of your SOAP service. Just for your information WSLD means Web Services Description Language.

Creating a new Project

Once that is done we see our project in the project explorer. We also have a view of the requests we can make.

SOAPUI project explorer

For our service we have the get_price and get_product_by_id calls that we can make.

When we drill down we can see the requests we can make.


So we click on Request 1 to generate the SOAP XML that executes the request.

Request Window

We could press the green play button to execute the request.

If we edit the value within get_price we can change the results of the request.

 <name xsi:type="xsd:string">pen</name> // request
 <return xsi:type="xsd:integer">10</return> // response

So we can test our service quite easily using SoapUI.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s