This article describes how to use php to generate common wsdl files for webservice. For more information, see this article. Here we will introduce the method for directly generating zend studio9, you can also use php to generate a wsdl file.
In zend studio9, unlike the previous version, you need to install the wsdl plug-in separately to use it. The steps are as follows:
1. Open zendstudio9 and select help> welcome.
2. Find the WSDL Support in the list on the right, and select
3. Click Apply changes)
4. zde will be automatically installed, and then prompt to restart, click restart now (restart now)
After the installation is complete, we generate A wsdl file. The steps are as follows:
1. Select the project folder for saving the wsdl
2. Right-click-new (new)-> other (Others)
3. In the pop-up dialog box, select web services, select WSDL file from the lower menu, and click next to go to the next step.
4. Name your wsdl file and click next to go to the next step.
5. Modify the target namespace (the target namespace), and click finish.
A wsdl file is generated, as shown in:
Under TestSoapSoap, enter php webService such as soapservice. php.
Right-click NewOperation:
NewOperation: add method. The name of the method to be provided to others in WebService.
Input: Set the input parameter name and type.
Output: sets the return value.
Add part: If added in input, multiple parameters are added.
Set Type/set Element: set the input/output Type of the parameter to include common data types (int, string, boolean, float, time ...), Element is the type of custom Element.
. Use SoapDiscovery. class. php to generate
The Code is as follows: |
Copy code |
<? Php Class SoapDiscovery { Private $ class_name = ''; Private $ service_name = ''; /** * SoapDiscovery ::__ construct () SoapDiscovery class Constructor. * * @ Param string $ class_name * @ Param string $ service_name **/ Public function _ construct ($ class_name = '', $ service_name = ''){ $ This-> class_name = $ class_name; $ This-> service_name = $ service_name; } /** * SoapDiscovery: getWSDL () Returns the WSDL of a class if the class is instantiable. * * @ Return string **/ Public function getWSDL (){ If (empty ($ this-> service_name )){ Throw new Exception ('no service name .'); } $ HeaderWSDL = "<? Xml version = "1.0"?> N "; $ HeaderWSDL. = "<definitions name =" $ this-> service_name "targetNamespace =" urn: $ this-> service_name "xmlns: wsdl =" http://schemas.xmlsoap.org/wsdl/ "xmlns: soap =" http://schemas.xmlsoap.org/wsdl/soap/ "xmlns: tns = "urn: $ this-> service_name" xmlns: xsd = "http://www.w3.org/2001/XMLSchema" xmlns: SOAP-ENC = "http://schemas.xmlsoap.org/soap/encoding/" xmlns = "http://schemas.xmlsoap.org/wsdl/"> n "; $ HeaderWSDL. = "<types xmlns =" http://schemas.xmlsoap.org/wsdl/ "/> n "; If (empty ($ this-> class_name )){ Throw new Exception ('no class name .'); }
$ Class = new ReflectionClass ($ this-> class_name );
If (! $ Class-> isInstantiable ()){ Throw new Exception ('class is not instantiable .'); }
$ Methods = $ class-> getMethods ();
$ PortTypeWSDL = '<portType name = "'. $ this-> service_name. 'port"> '; $ BindingWSDL = '<binding name = "'. $ this-> service_name. 'binding "type =" tns :'. $ this-> service_name. "Port"> n <soap: binding style = "rpc" transport = "http://schemas.xmlsoap.org/soap/http"/> n "; $ ServiceWSDL = '<service name = "'. $ this-> service_name. ""> n <documentation/> n <port name = "". $ this-> service_name. 'port "binding =" tns :'. $ this-> service_name. "Binding"> <soap: address location = "http ://". $ _ SERVER ['server _ name']. ':'. $ _ SERVER ['server _ port']. $ _ SERVER ['php _ SELF ']. ""/> n </port> n </service> n "; $ MessageWSDL = ''; Foreach ($ methods as $ method ){ If ($ method-> isPublic ()&&! $ Method-> isConstructor ()){ $ PortTypeWSDL. = '<operation name = "'. $ method-> getName (). ""> n ". '<input message = "tns :'. $ method-> getName (). "Request"/> n <output message = "tns :". $ method-> getName (). "Response"/> n </operation> n "; $ BindingWSDL. = '<operation name = "'. $ method-> getName (). ""> n ". '<soap: operation soapAction = "urn :'. $ this-> service_name. '#'. $ this-> class_name. '#'. $ method-> getName (). ""/> n <input> <soap: body use = "encoded" namespace = "urn: $ this-> service_name "encodingStyle =" http://schemas.xmlsoap.org/soap/encoding/ "/> n </input> n <output> n <soap: body use =" encoded "namespace =" urn: $ this-> service_name "encodingStyle =" http://schemas.xmlsoap.org/soap/encoding/ "/> n </output> n </operation> n "; $ MessageWSDL. = '<message name = "'. $ method-> getName ()." Request "> n "; $ Parameters = $ method-> getParameters (); Foreach ($ parameters as $ parameter ){ $ MessageWSDL. = '<part name = "'. $ parameter-> getName ()." "type =" xsd: string "/> n "; } $ MessageWSDL. = "</message> n "; $ MessageWSDL. = '<message name = "'. $ method-> getName ()." Response "> n "; $ MessageWSDL. = '<part name = "'. $ method-> getName ()." "type =" xsd: string "/> n "; $ MessageWSDL. = "</message> n "; } } $ PortTypeWSDL. = "</portType> n "; $ BindingWSDL. = "</binding> n "; Return sprintf ('% s % s', $ headerWSDL, $ portTypeWSDL, $ bindingWSDL, $ serviceWSDL, $ messageWSDL, '</definitions> '); } /** * SoapDiscovery: getDiscovery () Returns discovery of WSDL. * * @ Return string **/ Public function getDiscovery (){ Return "<? Xml version = "1.0"?> N <disco: discovery xmlns: disco = "http://schemas.xmlsoap.org/disco/" xmlns: AQPs = "http://schemas.xmlsoap.org/disco/scl/"> n <scl: contractRef ref = "http ://". $ _ SERVER ['server _ name']. ':'. $ _ SERVER ['server _ port']. $ _ SERVER ['php _ SELF ']. "? Wsdl "/> n </disco: discovery> "; } } ?> |
Use Case:
The Code is as follows: |
Copy code |
<? Php Include ("Compute. php"); // file to be generated Include ("SoapDiscovery. class. php ");
$ Disco = new SoapDiscovery ('compute', 'compute'); // The first parameter is the class name (the generated wsdl file is named by it), that is, the person class, the second parameter is the service name (which can be written as needed ). $ Wsdl = $ disco-> getWSDL (); // $ Disco-> getDiscovery (); $ Fp = fopen ("Compute. wsdl", "w "); Fwrite ($ fp, $ wsdl ); ?> |
Method 2: the most important thing is our SoapDiscovery. class. php file. You can find this file on the Internet.