Building a SOAP Server with Zend_Soap_Server and Returning Arrays of ComplexTypes

ZendFramework is awesome but suffers from a peculiar lack of thorough examples and documentation.

To save you all some time scouring the internet fruitlessly, I will document how to use Zend_Soap_Server, and Zend_Soap_AutoDiscover in combination with Zend_Soap_Wsdl_Strategy_ArrayOfTypeComplex (try saying that really fast a few times!).

A Complete Example

No one ever put together a full example in the documentation, their blog posts, the official Zend samples or anywhere else I searched yesterday. It’s always snippets here and there, and they don’t fit together well. So let me save you some trouble. I figure your reaching this page means you’ve already passed the first test which is to know the classes you need, which is why the search brought you here :)

I’m binding my webservice to a class, as that would be the logical choice in most cases.

/* these are required for it all to work */
require("Zend/Soap/Server.php");
require("Zend/Soap/Wsdl.php");
require("Zend/Soap/Wsdl/Strategy/ArrayOfTypeComplex.php");
require("Zend/Soap/AutoDiscover.php");


/* my system makes use of a global prefix, feel free to do as needed */
$serviceURL = WWWPREFIX . '/webservices/myservice';


public class MyService {
     /**
     *
     * @param integer $UserID
     * @return array
     */
    function GetCoupons($UserID) {
        // do some work here
        $coupons = array(new Coupon(), new Coupon());
    }
}

// Generate WSDL relevant to code
if (isset($_GET['wsdl'])){
    $autodiscover = new Zend_Soap_AutoDiscover('Zend_Soap_Wsdl_Strategy_ArrayOfTypeComplex');
    $autodiscover->setClass('MyService');
    $autodiscover->handle();
} else {
    $server = new Zend_Soap_Server($serviceURL . "?wsdl");
    $server->setClass('MyService');
    $server->setObject(new MyService());
    $server->handle();
}

There are a few points of interest in the code block above (which is a completely functional service btw):

  • Notice the PHPDoc block above the function? If you don’t get that right, it won’t work at all
  • The return type of array because we’re returning an array of objects
  • The Zend_Soap_AutoDiscover bit at the bottom? That’ll auto-generate your WSDL for you

There, you’re done. It should all just work, complete with WSDL auto-generation. If you’re looking to make use of an existing WSDL file, you can use the secondary method of binding to methods/functions as outlined over at the official documentation on Zend_Soap_Server.

Say thank you Uncle Kevin :)

4 thoughts on “Building a SOAP Server with Zend_Soap_Server and Returning Arrays of ComplexTypes

  1. Maaaan, you’ve gone this far, why not include the paths and other pieces needed to get a rudimentary service up? Assume people know nothing and are blindly going into these projects.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>