Category Archives: Lazy Web

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 :)

SVNKit + Subclipse Problems

If you’re experiencing difficulty getting Subclipse working with SVNKit, install the very latest. One of the dependencies pulled in will list an SVNKit beta. Install that one and it’ll suddenly show up as a provider.

This is true as of Eclipse Ganymede and Subclipse 1.6.2 (installed with CollabNet Desktop). Just thought to save someone else several hours of their life :)

Safari 4 Scaling Zoom

Finally, Safari has page scaling in the v4 Beta. Sadly though it appears to exhibit strange behaviour with GMail for instance — When I zoom in, it also scales the width of the entire page, which means I now have to scroll left and right :(

Now, whilst this may or may not be the best thing for scaling zoom to do, my question is rather about whether it’s the right thing to do…

Page scaling should in theory scale everything right? Thoughts anyone? Perhaps you’re familiar with the W3 recommendation on this matter…