PHP2WSDL feature of the WSF/PHP allows you to generate the WSDL for your service when you access the URL formed by adding “?wsdl” to the service URL ( or you can use service URL + “?wsdl2” to access the wsdl version 2.0). It will generate the schema types for the wsdl from the classes you use to build the request message + the annotations you provided describing the types of the variables.
Sometime you may need to generate schema types with different names to the corresponding PHP classes. May be you need to have a ‘-‘ in the schema type which is not valid in PHP class syntax or you like to have a different naming convention for PHP and wsdl. Similarly you may need to have the operation names different in the WSDL and the PHP code.
Here is how you do it with WSF/PHP.
Different Names for Operations
/** * Service logic for the echo operation * @namespace http://ws.dimuthu.org/php/myecho/operations * @param object testObject $param * @return object testObject $return */ function echoMe($param) { return $param; } /* The Service operation name to PHP function name map */ $operations = array("echo" => "echoMe");
There I want my actual operation name to be echo. But I can’t declare a function name echo since PHP already has a library function echo (Yea, the one you regularly use to echo output). So I don’t have option other than declaring a function with different name (here it is “echoMe”) and mapped it in to echo operation in the operation map. We are going to feed this $operation variable at the WSService creation as its constructor argument.
Lets see how different names are used in schema types and corresponding php class names.
Different names for Types and Classes
/** * @namespace http://ws.dimuthu.org/php/myecho/types */ class testObject { /** * @var integer aint */ public $aint; /** * @var string astring */ public $astring; } /* The mapping of schema types to PHP class */ $classmap = array("test-object" => "testObject");
It is similar how we mapped operation in the previous section. Just use $classmap variable which map the schema type name to the php class name.
Here is the type section and the interface section of the WSDL 2.0 generated using above codes. Observe the operation names and the types names are formed the way we expected.
<types> <xsd:schema xmlns:ns0="http://ws.dimuthu.org/php/myecho/types" xmlns:ns1="http://ws.dimuthu.org/php/myecho/types" elementFormDefault="qualified" targetNamespace="http://ws.dimuthu.org/php/myecho/operations/xsd"> <xsd:import namespace="http://ws.dimuthu.org/php/myecho/types"/> <xsd:element name="param" type="ns0:test-object"/> <xsd:element name="return" type="ns1:test-object"/> </xsd:schema> <xsd:schema elementFormDefault="qualified" targetNamespace="http://ws.dimuthu.org/php/myecho/types"> <xsd:complexType name="test-object"> <xsd:sequence> <xsd:element name="aint" type="xsd:integer"/> <xsd:element name="astring" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema> </types> <interface name="myEchoPortType"> <operation name="echo" pattern="http://www.w3.org/ns/wsdl/in-out"> <input element="tnx:param"/> <output element="tnx:return"/> </operation> </interface>
Here is the complete code (Combining all the previously mentioned code segments). You can check the complete wsdl generation (wsdl 1.1 or wsdl 2.0 as your preference) by copying and pasting this code to the online php2wsdl generator at the WSF/PHP Demo Site.
<?php /** * Service logic for the echo operation * @namespace http://ws.dimuthu.org/php/myecho/operations * @param object testObject $param * @return object testObject $return */ function echoMe($param) { return $param; } /** * @namespace http://ws.dimuthu.org/php/myecho/types */ class testObject { /** * @var integer aint */ public $aint; /** * @var string astring */ public $astring; } /* The Service operation name to PHP function name map */ $operations = array("echo" => "echoMe"); /* Telling that we input MIX types for the parameters */ $opParams = array("echo" => "MIXED"); /* The mapping of schema types to PHP class */ $classmap = array("test-object" => "testObject"); /* Creating the WSService and serving the Request */ $service = new WSService(array( "operations" => $operations, "classmap" => $classmap, "opParams" => $opParams, "serviceName" => "myEcho")); $service->reply(); ?>