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.
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).
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 ) ) ) )
I use NuSOAP to generate a WSDL so that I can access the webservice using other SOAP tookits such as the MS Soap 3.0 Toolkit.
How am I able to get this associative array structure across?
Posted by: Michael Phipps on September 6, 2003 6:38 PMThe object passe well but I can pass an associative array from nuSOAP to the new flah MX 2004. There must be an array problem in nuSOAP.
If you know the solution please mail me
My NuSOAP and WSDL article describes how to use structured data with tools that consume WSDL.
Posted by: Stepan on September 16, 2003 3:51 PMCan you send a complete source code (server side) of book web services, because i don’t know how to register and define data type like example what you write above
Best Regard’s
P
In a more recent post, I go into more detauls with source.
However, all this was written quite some time ago, so I’m not sure how much of it still applies…
Posted by: stepan on December 19, 2006 9:25 AM