SOAP Lab

Summary

SOAP or Simple Object Access Protocol is simple and versatile solution for making calls from programs running on MachineA to programs running on MachineB. SOAP allows the creation of Web Services, which are standardized computing resources that take input from across a network and respond in an advertised way. SOAP is a specification for extensible markup language (XML) messages that travel between endpoints. In terms of networking, SOAP messages travel on top of HTTP or simple mail transport protocol (SMTP). A function can be exposed through a SOAP server, meaning it can accept SOAP messages from elsewhere on the network and return results in a response. These functions, when exposed, become Web Services -- computing resources that may be accessed over the network via web protocols.

In this lab we will be create and register a simple SOAP server with several functions exposed as Web Services. Then we will create a simple SOAP client that will use the new Web Services.

The instructions on this page will show you how to:

Note: You must have completed the Google lab before completing this lab!

Create and register a simple SOAP Server

  1. We will create a simple SOAP server with NuSOAP classes. If you have not already installed NuSOAP, please read the instructions from the Google lab.

  2. Using Notepad2 or your favorite editor, create a PHP file called soapserver.php.

    <?php
    
    require_once('nusoap/lib/nusoap.php');
    
    $s = new soap_server;
    $s->register('hello');
    $s->service($HTTP_RAW_POST_DATA);
    

    This code uses the NuSoap library to instantiate a new soap_server object. Next the SOAP server registers a named function with the register() function. Finally, it directs the incoming HTTP data to the soap_server object using the service() function.

  3. Next we will create the simple function that we have registered.

    function hello($name) {
      return "Czesc $name !
    (Czesc is 'hi' in Polish)"; } ?>

    This function is named hello, the same name as the one we registered above. It takes an string argument called $name and it returns a greeting string back in the response.

Create a simple SOAP Client

Now that we have created our SOAP server, we need to create a SOAP client that uses our registered Web Service.

  1. Use Notepad2 to create a PHP file called soapclient.php.
  2. Add an simple form input box so that the user can enter their name.
    <html>
      <head><title>Simple Hello Soap Client</title></head>
      <body>
    
    <h2>Simple Hello Soap Client</h2>
    
    <p>Enter your name and the program will say hello.</p>
    
    <form action="<?PHP $PHP_SELF; ?>" method="get">
    <br />Your name is: <input type="text" name="name" />
    </form>
    
    
      </body>
    </html>
    

    This form allows the user to enter some text into the input box and calls itself as an action. It uses the GET method.

  3. Now, we'll create a new soap client and provide as parameters an associative array with a key label 'name' and a value from the form. Next the registered function 'hello' is called.

    <?php
    
      require_once('./nusoap/lib/nusoap.php');
      $soapclient = new soapclient("http://localhost/php_examples/soapserver.php");
      $parameters = array('name' => $_GET['name']);
      echo $soapclient->call('hello', $parameters);
    
    ?>
    

    Note: You will need to provide the URL of the SOAP server you just created.

  4. We don't want to run this SOAP client everytime the page is load, only every time a user types something into the text box. Lets add an if statement to check if the form variable $_GET['name'] is empty or not.

    <?php
    
    if ($_GET['name'] != "") {
      require_once('./nusoap/lib/nusoap.php');
      $soapclient = new soapclient("http://students.washington.edu/suzka/classes/in\
    fo344/php/soapserver.php");
      $parameters = array('name' => $_GET['name']);
      echo $soapclient->call('hello', $parameters);
    } else {
    
    ?>
    <form action="<?PHP $_SERVER['PHPSELF'] ?>" method="get">
    <br />Your name is: <input type="text" name="name" />
    </form>
    
    <?php
    }
    ?>
    

    Your new form should look something like this:
    a soap client

  5. You should try to run your new SOAP client now! When it works you should see something like this:
    a soap response

Experiment with SOAP Web Services

Create your own function and register it as a SOAP service. Create a simple SOAP client that queries it and displays the response.

  1. Using Notepad2 edit your file called soapservice.php. Create a new function.
  2. Don't forget to register this function with the soap_server object.
  3. Update the list of parameters needed for the SOAP Web Service.
  4. Create a simple SOAP client that takes user input and queries the Web Service.

Resources