WSDL
Web Services Description Language (WSDL) is an XML-based Language used to describe Web Services and how to access them. This document describes a Web service. It specifies the service location and the operations (or methods) provided by the Service ).
The main structure of a wsdl document is similar to this:
<definitions><types> definition of types........</types><message> definition of a message....</message><portType> definition of a port.......</portType><binding> definition of a binding....</binding></definitions>
The WSDL document can contain other elements, such as the extension element and a service element. This element can combine the definitions of several web services in a single WSDL document.
PHP generates WSDL
Class Code (SoapDiscovery. class. php ):
<?phpclass 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%s%s%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:scl="http://schemas.xmlsoap.org/disco/scl/">n<scl:contractRef ref="http://".$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['PHP_SELF']."?wsdl" />n</disco:discovery>"; }} ?>
Usage (server. php ):
<? Phpdefine ('wsdl _ url', 'Hello. wsdl '); // defines the WSDL file path ini_set ('soap. wsdl_cache_enabled ', '0'); // disable the WSDL cache // if (! File_exists (WSDL_URL) {require_once 'soapdiscovery. class. php '; $ disco = new SoapDiscovery ('helloworld', 'ieliwb _ helloworld'); $ str = $ disco-> getWSDL (); file_put_contents (WSDL_URL, $ str );} // when SOAP is enabled and receives the parameter response from the Client $ server = new SoapServer (WSDL_URL); $ server-> setClass ('helloworld'); $ server-> handle (); // test and define public class HelloWorld {private $ nombre = ''; public function _ construct ($ name = 'World') {$ this- > Name = $ name;} public function greet ($ name = '') {$ name = $ name? $ Name: $ this-> name; return 'Hello'. $ name. '.';} public function serverTimestamp () {return time () ;}}?>
Client. php:
<?php$client = new SoapClient("http://127.0.0.1/createsoap/hello.wsdl"); try { $result = $client->greet('ieliwb'); var_dump($result); echo "The answer isresult";}catch (SoapFault $f){ echo "Error Message: {$f->getMessage()}";}?>Create Webservice
1. Create A wsdl
- Non-standard webservice, which can only be accessed by PHP
- The standard webservice must use wsdl (webservice description language, which is to describe your service content using XML syntax standards, which I understand)
Here I will only introduce the standard webservice. How to Create A wsdl? For PHP, this is really not easy. Some people say that it is very convenient to create with zend studio. This is a method. But for those who do not like zend studio, they will feel that it is too difficult to create a webservice and install zend studio. I am, hey.
Here I will introduce a simple method to download SoapDiscovery online. class. php class, which has a public method: getWSDL. The return is used at the end of this method. So, you can modify this method. I did this:
// Return sprintf ('% s % s', $ headerWSDL, $ portTypeWSDL, $ bindingWSDL, $ serviceWSDL, $ messageWSDL, '</definitions>'); // generates the wsdl file, and comments $ fso = fopen ($ this-> class_name. ". wsdl "," w "); fwrite ($ fso, sprintf ('% s % s', $ headerWSDL, $ portTypeWSDL, $ bindingWSDL, $ serviceWSDL, $ messageWSDL, '</definitions> '));
Now the wsdl class is generated, SoapDiscovery. class. php.
I only need to prepare a service-providing class or function to create the wsdl. For example, I have a class: person and the file name is person. class. php.★There are two methods, one is say and the other is run. Very simple.
<?phpclass person { public function say() { return("i'm speaking."); } public function run() { return("i'm running,don't disturb me please."); } }?>
There are two classes: SoapDiscovery. class. php and person. class. php.
Start generating the wsdl: Create the file server. php. Copy the following content and run the command to generate a person. wsdl file.
<? Phpinclude ("person. class. php "); include (" SoapDiscovery. class. php "); // 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 ). $ Disco = new SoapDiscovery ('person ', 'person'); $ disco-> getWSDL ();?>
2. Create a webservice server program
Clear the content of the server. php file and copy the following code:
<? Php include ("person. class. php "); $ objSoapServer = new SoapServer (" person. wsdl "); // person. wsdl is the newly created wsdl file // $ objSoapServer = new SoapServer ("server. php? Wsdl "); // $ objSoapServer-> setClass (" person "); // register all methods of the person class $ objSoapServer-> handle (); // process the request?>
3. Create a webservice client program and test whether the webservice is valid. The file name is client. php.
<? Php $ client = new SoapClient ("person. wsdl"); // $ client = new SoapClient ("server. php? Wsdl "); // echo ($ client-> say (); echo" <br/> "; echo ($ client-> run ()); echo "<br/>" ;?>
OK .. NET, you only need to provide a url for it.
Method to obtain the url: You can first go to person. find <soap: address location = "http: // xxxxxxxxxxxxxxxxxxxx/server. php "/>, the url here (the specific url is determined based on your directory) is what you want to provide.. NET developer. But don't be too happy. You need to add "? Wsdl ", http: // xxxxxxxxxxxxxxxxxxxx/server. php? Wsdl is correct. If you do not believe it, you can copy the url to the address bar of the browser and check it.
. NET developers can add a service reference or web reference in their own projects after obtaining the url you have given them. Then, they can complete related operations as prompted. NET developers.