Posts by Tag "SOAP"

Subscribe to posts of this tag

Using Zend_Soap Server and Autodiscover in a Controller

I am in a dilemma. I have condemned the usage of Zend_Soap_Server (or any other webservice server handler) inside a Model-View-Controller application before. Still I get questions about my old Zend_Soap_Server and Zend_Soap_AutoDiscover example not working in the MVC scenario. The following example provides you with a working Soap server inside a Zend_Controller_Action, although I discourage the use of it and would suggest using a dedicated script outside the dispatching process to gain multitudes of performance, which webservices often require.

require_once "/path/to/HelloWorldService.php";
 
class MyDiscouragedSoapServerController extends Zend_Controller_Action
{
    public function serverAction()
    {
        $server = new Zend_Soap_Server("http://example.com/pathto/wsdl");
        $server->setClass('HelloWorldService');
        $server->handle();
    }
 
    public function wsdlAction()
    {
        $wsdl = new Zend_Soap_AutoDiscover();
        $wsdl->setUri('http://example.com/pathto/server');
        $wsdl->setClass('HelloWorldService');
        $wsdl->handle();
    }
}

Now all you have to do is create two routes, one that makes http://example.com/pathto/server point to MyDiscouragedSoapServerController::serverAction and the other route that makes http://example.com/pathto/wsdl point to MyDiscouragedSoapServerController::wsdlAction. The wrong version (Version Mismatch) error comes from sending a request for the WSDL file to the actual Soap Server, which he doesn't like.

What will be new in ZF 1.8

In Februar or March 2009 the 1.8 version of the Zend Framework is schedulded to be released. I have contributed some stuff already regarding ZendX_JQuery and Zend_Soap.

Both components have seen numerous bugfixes and I managed to get the JQuery helper down to zero open issues. I have also taken over the Zend_Json_Expr proposal, which will be a huge benefit to everything JSON that can be done with ZF. Foremost it is an integral part for the jQuery component which heavily relies on javascript callbacks.

The Soap Autodiscover and WSDL classes compability with Java and .NET has been optimized due to great user feedback, aswell as some bugfixes to the newly added WSDL type detection strategies.

Additionally I went on another bug killing spree and fixed around 20-30 old bugs in a wide range of different components.

ZF 1.7: jQuery is in! Zend_Soap with lots of Bugfixes.

The next version of Zend Framework will appear quite soon (16th November) and there will be lots of buzz about Zend Amf, the new component sponsored by Adobe to support Adobe Flash (or other tools) to PHP communications.

Besides the buzz there is some stuff from me getting into the Zend Framework. jQuery View and Form Helpers will allow you to throw the Dojo stuff away and use the great jQuery. This is of course inheritly subjective, but still different opportunities are always great.

Additionally I have been fixing quite some bugs on the Zend Soap component and will manage to fix further stuff until the 1.7 release I hope. This will make especially Wsdl and AutoDiscover produce valid WSDL XML plus adds setObject() support to Zend_Soap_Server.

You can now choose between several strategies to evaluate complex types in WSDL Autodiscovering. Array of Datatypes (simple or complex) via type[] syntax and complex objects can be parsed differently now based on settings.

First impressions on Zend_Soap and a basic implementation

The Zend Framework release candidate for version 1.6 includes a new component for SOAP operations. Zend_Soap_Server/Client extend the PHP functionality of the SOAPClient and SOAPServer objects, which by itself is trivial. The more important functionality it ads to the package is the AutoDiscovery Component.

Generally you can use SOAP in the so called "non-wsdl" mode, that is if you specify the correct options like the soap service location and uri, you don't need any description of the service for the clients. This is only useful if you're using the SOAP Service internally since you then know about all the available functions and methods. If you want to offer the Service for external users you want to use the WSDL-mode: Generate a WSDL that describes the services available methods, their parameters and return types is an important task.

Using Zend_Soap_AutoDiscover you can generate your WSDL file automatically by reflecting on the given Service Class methods. This works as follows. We setup a simple service:

class HelloWorldService
{
    /**
     * @return string
     */
    public function helloWorld()
    {
        return "Hello World!";
    }
 
    /**
     * @return array
     */
    public function getFruits()
    {
        return array('apple', 'orange', 'banana');
    }
}

It is important that you specify the Doc Comments @param and @return otherwise the AutoDiscovery of the correct parameter and return types cannot be resolved. We will now setup a simple SOAP Server access point, that will also generate our WSDL file for description of this HelloWorld service:

require_once "HelloWorldService.php";
require_once "Zend/Soap/Server.php";
require_once "Zend/Soap/AutoDiscover.php";
 
if(isset($_GET['wsdl'])) {
    $autodiscover = new Zend_Soap_AutoDiscover();
    $autodiscover->setClass('HelloWorldService');
    $autodiscover->handle();
} else {
 
    $soap = new Zend_Soap_Server("http://localhost/soapserver.php?wsdl"); // this current file here
    $soap->setClass('HelloWorldService');
    $soap->handle();
}

Now we have our SOAP Service up and running and any client can access the HelloWorldService class from a remote or local location with just this simple lines:

require_once "Zend/Exception.php";
require_once "Zend/Soap/Client.php";
 
try {
    $client = new Zend_Soap_Client("http://localhost/soapserver.php?wsdl"); // Servers WSDL Location
    $string =  $client->helloWorld();
    $fruits = $client->getFruits();
 
    var_dump($string);
    var_dump($fruits);
} catch(Zend_Exception $e) {
    echo $e->getMessage();
}

This is easy. Additionally Zend_Soap offers a WSDL class to generate your own WSDL file based on your special preferences, which is a nice feature. I guess only some people can write a correct WSDL XML specification file on their own from scratch, so using a powerful helper is reasonable.

The usage of Zend_Soap is quite simple and straightforward as is PHP5's internal SOAP Service and might therefore gain widespread use. I have never tested PEAR's WSDL Autodiscovery, so i cannot draw comparissons.