AutoDiscovery

AutoDiscovery Introduction

SOAP functionality implemented within Zend Framework is intended to make all steps required for SOAP communications more simple.

SOAP is language independent protocol. So it may be used not only for PHP-to-PHP communications.

There are three configurations for SOAP applications where Zend Framework may be utilized:

  1. SOAP server PHP application <---> SOAP client PHP application
  2. SOAP server non-PHP application <---> SOAP client PHP application
  3. SOAP server PHP application <---> SOAP client non-PHP application

We always have to know, which functionality is provided by SOAP server to operate with it. » WSDL is used to describe network service API in details.

WSDL language is complex enough (see » http://www.w3.org/TR/wsdl for the details). So it's difficult to prepare correct WSDL description.

Another problem is synchronizing changes in network service API with already existing WSDL.

Both these problem may be solved by WSDL autogeneration. A prerequisite for this is a SOAP server autodiscovery. It constructs object similar to object used in SOAP server application, extracts necessary information and generates correct WSDL using this information.

There are two ways for using Zend Framework for SOAP server application:

  • Use separated class.

  • Use set of functions

Both methods are supported by Zend Framework Autodiscovery functionality.

TheZend_Soap_AutoDiscover class also supports datatypes mapping from PHP to » XSD types.

Here is an example of common usage of the autodiscovery functionality. The handle() function generates the WSDL file and posts it to the browser.

  1. class My_SoapServer_Class {
  2. ...
  3. }
  4.  
  5. $autodiscover = new Zend_Soap_AutoDiscover();
  6. $autodiscover->setClass('My_SoapServer_Class');
  7. $autodiscover->handle();

If you need access to the generated WSDL file either to save it to a file or as an XML string you can use the dump($filename) or toXml() functions the AutoDiscover class provides.

Note: Zend_Soap_Autodiscover is not a Soap Server
It is very important to note, that the class Zend_Soap_AutoDiscover does not act as a SOAP Server on its own. It only generates the WSDL and serves it to anyone accessing the url it is listening on.
As the SOAP Endpoint Uri is uses the default 'http://' .$_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'], but this can be changed with the setUri() function or the Constructor parameter of Zend_Soap_AutoDiscover class. The endpoint has to provide a Zend_Soap_Server that listens to requests.

  1. if(isset($_GET['wsdl'])) {
  2.     $autodiscover = new Zend_Soap_AutoDiscover();
  3.     $autodiscover->setClass('HelloWorldService');
  4.     $autodiscover->handle();
  5. } else {
  6.     // pointing to the current file here
  7.     $soap = new Zend_Soap_Server("http://example.com/soap.php?wsdl");
  8.     $soap->setClass('HelloWorldService');
  9.     $soap->handle();
  10. }

Class autodiscovering

If class is used to provide SOAP server functionality, then the same class should be provided to Zend_Soap_AutoDiscover for WSDL generation:

  1. $autodiscover = new Zend_Soap_AutoDiscover();
  2. $autodiscover->setClass('My_SoapServer_Class');
  3. $autodiscover->handle();

The following rules are used while WSDL generation:

  • Generated WSDL describes an RPC style Web Service.

  • Class name is used as a name of the Web Service being described.

  • 'http://' .$_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] is used as an URI where the WSDL is available by default but can be overwritten via setUri() method.

    It's also used as a target namespace for all service related names (including described complex types).

  • Class methods are joined into one » Port Type.

    $className . 'Port' is used as Port Type name.

  • Each class method is registered as a corresponding port operation.

  • Each method prototype generates corresponding Request/Response messages.

    Method may have several prototypes if some method parameters are optional.

Note: Important!
WSDL autodiscovery utilizes the PHP docblocks provided by the developer to determine the parameter and return types. In fact, for scalar types, this is the only way to determine the parameter types, and for return types, this is the only way to determine them.
That means, providing correct and fully detailed docblocks is not only best practice, but is required for discovered class.

Functions autodiscovering

If set of functions are used to provide SOAP server functionality, then the same set should be provided to Zend_Soap_AutoDiscovery for WSDL generation:

  1. $autodiscover = new Zend_Soap_AutoDiscover();
  2. $autodiscover->addFunction('function1');
  3. $autodiscover->addFunction('function2');
  4. $autodiscover->addFunction('function3');
  5. ...
  6. $autodiscover->handle();

The following rules are used while WSDL generation:

  • Generated WSDL describes an RPC style Web Service.

  • Current script name is used as a name of the Web Service being described.

  • 'http://' .$_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] is used as an URI where the WSDL is available.

    It's also used as a target namespace for all service related names (including described complex types).

  • Functions are joined into one » Port Type.

    $functionName . 'Port' is used as Port Type name.

  • Each function is registered as a corresponding port operation.

  • Each function prototype generates corresponding Request/Response messages.

    Function may have several prototypes if some method parameters are optional.

Note: Important!
WSDL autodiscovery utilizes the PHP docblocks provided by the developer to determine the parameter and return types. In fact, for scalar types, this is the only way to determine the parameter types, and for return types, this is the only way to determine them.
That means, providing correct and fully detailed docblocks is not only best practice, but is required for discovered class.

Autodiscovering Datatypes

Input/output datatypes are converted into network service types using the following mapping:

  • PHP strings <-> xsd:string.

  • PHP integers <-> xsd:int.

  • PHP floats and doubles <-> xsd:float.

  • PHP booleans <-> xsd:boolean.

  • PHP arrays <-> soap-enc:Array.

  • PHP object <-> xsd:struct.

  • PHP class <-> based on complex type strategy (See: this section) [1] Zend_Soap_AutoDiscoverZend_Soap_Wsdl_Strategy_DefaultComplexTypeZend_Soap_Wsdl_Strategy_Interface$extractComplexTypeZend_Soap_WsdlZend_Soap_Wsdl manual on adding complex .

  • type[] or object[] (ie. int[]) <-> based on complex type strategy

  • PHP void <-> empty type.

  • If type is not matched to any of these types by some reason, then xsd:anyType is used.

Where xsd: is "http://www.w3.org/2001/XMLSchema" namespace, soap-enc: is a "http://schemas.xmlsoap.org/soap/encoding/" namespace, tns: is a "target namespace" for a service.

WSDL Binding Styles

WSDL offers different transport mechanisms and styles. This affects the soap:binding and soap:body tags within the Binding section of WSDL. Different clients have different requirements as to what options really work. Therefore you can set the styles before you call any setClass or addFunction method on the AutoDiscover class.

  1. $autodiscover = new Zend_Soap_AutoDiscover();
  2. // Default is 'use' => 'encoded' and
  3. // 'encodingStyle' => 'http://schemas.xmlsoap.org/soap/encoding/'
  4. $autodiscover->setOperationBodyStyle(
  5.                     array('use' => 'literal',
  6.                           'namespace' => 'http://framework.zend.com')
  7.                 );
  8.  
  9. // Default is 'style' => 'rpc' and
  10. // 'transport' => 'http://schemas.xmlsoap.org/soap/http'
  11. $autodiscover->setBindingStyle(
  12.                     array('style' => 'document',
  13.                           'transport' => 'http://framework.zend.com')
  14.                 );
  15. ...
  16. $autodiscover->addFunction('myfunc1');
  17. $autodiscover->handle();
[1] will be created with the class as detection algorithm for complex types. The first parameter of the AutoDiscover constructor takes any complex type strategy implementing or a string with the name of the class. For backwards compatibility with boolean variables are parsed exactly like in . See the types for more information.
blog comments powered by Disqus