> Zend Framework中文手册 > 40.4. AutoDiscovery

40.4. AutoDiscovery

40.4.1. 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.

Zend_Soap_AutoDiscovery 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.

class My_SoapServer_Class {
...
}

$autodiscover = new Zend_Soap_AutoDiscover();
$autodiscover->setClass('My_SoapServer_Class');
$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.

40.4. AutoDiscovery 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.

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

40.4.2. Class autodiscovering

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

$autodiscover = new Zend_Soap_AutoDiscover();
$autodiscover->setClass('My_SoapServer_Class');
$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.

40.4. AutoDiscovery 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.

40.4.3. 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:

$autodiscover = new Zend_Soap_AutoDiscover();
$autodiscover->addFunction('function1');
$autodiscover->addFunction('function2');
$autodiscover->addFunction('function3');
...
$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.

40.4. AutoDiscovery 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.

40.4.4. 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: 第 40.3.10.2 节 “Adding complex type information.”) [21].

  • 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.

40.4.5. 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.

$autodiscover = new Zend_Soap_AutoDiscover();
// Default is 'use' => 'encoded' and
// 'encodingStyle' => 'http://schemas.xmlsoap.org/soap/encoding/'
$autodiscover->setOperationBodyStyle(
                    array('use' => 'literal',
                          'namespace' => 'http://framework.zend.com')
                );

// Default is 'style' => 'rpc' and
// 'transport' => 'http://schemas.xmlsoap.org/soap/http'
$autodiscover->setBindingStyle(
                    array('style' => 'document',
                          'transport' => 'http://framework.zend.com')
                );
...
$autodiscover->addFunction('myfunc1');
$autodiscover->handle();

            

40.4.6. Compatibility with WSDL Client Generators

Up to version 1.7 the WSDL files were generated with a response message part that could not be detected correctly by Java or .NET clients for example. From version 1.8 the function setResponseMessageReturnNameCompability() was included to enable compability for this cases.



[21] Zend_Soap_AutoDiscover will be created with the Zend_Soap_Wsdl_Strategy_DefaultComplexType class as detection algorithm for complex types. The first parameter of the AutoDiscover constructor takes any complex type strategy implementing Zend_Soap_Wsdl_Strategy_Interface or a string with the name of the class. For backwards compatibility with $extractComplexType boolean variables are parsed exactly like in Zend_Soap_Wsdl. See the Zend_Soap_Wsdl manual on adding complex types for more information.