Note: The NuSOAP and WSDL article describes how to use a more recent version of NuSOAP to pass data with tool that consume WSDL (Microsoft’s SOAP Toolkit, etc.).

NuSOAP is a library that makes implementing basic web services in PHP a snap. The documentation is a bit sparse, but this tutorial takes you through the steps of implementing a basic SOAP server and client with the NuSOAP library. One thing I haven’t seen mentioned is how to pass complex data types as parameters or return values.

Complex Types

NuSOAP serializes basic PHP types and (associative) arrays as expected. However, if your want to pass structures, or arrays of structures, you must wrap them into classes. NuSOAP will then serialize the member variables of the object (or array of objects).

Example

Let’s say I have a web service that returns an array of books, each of which is a record of author, title, numpages and toc (table of contents). The table of contents is an array of section and page.

Since I’m using two different structures, I’ll create two classes, Book and Chapter. For convenience, I also give them constructors that assign the member variables:

class Chapter {
     var $title, $page;
     function Chapter($title, $page) {
          $this->title = $title;
          $this->page = $page;
      }
}
 
class Book {
     var $author, $title, $numpages, $toc;
     function Book($author, $title, $numpages, $toc) {
          $this->author = $author;
          $this->title = $title;
          $this->numpages = $numpages;
          $this->toc = $toc;
      }
}

My web service getBooks will return the structured array of books like so:

function getBooks() {
     // Here you could query your database, etc.
     // But we're just going to return two books.
  
     // Initialize books array
     $books = array();
  
     // Create toc for first book
     $toc = array();
     $toc[] = new Chapter("The Little House", 1);
     $toc[] = new Chapter("The Storm", 23);
  
     // Add first book
     $books[] = new Book("John Smith", "Short Stories", 40, $toc);
  
     // Create toc for second book
     $toc = array();
     $toc[] = new Chapter("Pond", 1);
     $toc[] = new Chapter("Creek", 5);
     $toc[] = new Chapter("River", 7);
  
     // Add second book
     $books[] = new Book("Jane Doe", "Poems", 13, $toc);
  
     // Return array
     return $books;
}

When the getBooks web service is called, it will return the following datate:

Array
(
    [0] => Array
        (
            [author] => John Smith
            [title] => Short Stories
            [numpages] => 40
            [toc] => Array
                (
                    [0] => Array
                        (
                            [title] => The Little House
                            [page] => 1
                        )
                    [1] => Array
                        (
                            [title] => The Storm
                            [page] => 23
                        )
                )
        )
 
    [1] => Array
        (
            [author] => Jane Doe
            [title] => Poems
            [numpages] => 13
            [toc] => Array
                (
                    [0] => Array
                        (
                            [title] => Pond
                            [page] => 1
                        )
                    [1] => Array
                        (
                            [title] => Creek
                            [page] => 5
                        )
                    [2] => Array
                        (
                            [title] => River
                            [page] => 7
                        )
                )
        )
)