How to use PHP to implement a WebService 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:
definition of types........
definition of a message....
definition of a port.......
definition of a binding....
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 ):
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 = "
n"; $headerWSDL.= "
n"; $headerWSDL.= "
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 = '
'; $bindingWSDL = '
n
n"; $serviceWSDL = '
n
n
service_name.'Port" binding="tns:'.$this->service_name."Binding">
n
n
n"; $messageWSDL = ''; foreach ($methods as $method) { if ($method->isPublic() && !$method->isConstructor()) { $portTypeWSDL.= '
n".'
n
getName()."Response" />n
n"; $bindingWSDL.= '
n".'
n
nn
n
nn
n"; $messageWSDL.= '
n"; $parameters = $method->getParameters(); foreach ($parameters as $parameter) { $messageWSDL.= '
n"; } $messageWSDL.= "
n"; $messageWSDL.= '
n"; $messageWSDL.= '
n"; $messageWSDL.= "
n"; } } $portTypeWSDL.= "
n"; $bindingWSDL.= "n"; return sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '
'); } /** * SoapDiscovery::getDiscovery() Returns discovery of WSDL. * * @return string **/ public function getDiscovery() { return "
n
n
n
"; }} ?>
Usage (server. php ):
GetWSDL (); file_put_contents (WSDL_URL, $ str);} // enable SOAP and receive 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:
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 ,''); // Generate the wsdl file and annotate the above return comments $ fso = fopen ($ this-> class_name. ". wsdl "," w "); fwrite ($ fso, sprintf ('% s % s', $ headerWSDL, $ portTypeWSDL, $ bindingWSDL, $ serviceWSDL, $ messageWSDL ,''));
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.
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.
GetWSDL ();?>
2. create a webservice server program
Clear the content of the server. php file and copy the following code:
SetClass ("person"); // all methods for registering 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.
Say (); echo"
"; Echo ($ client-> run (); echo"
";?>
OK .. NET, you only need to provide a url for it.
Method to obtain the url: you can search for the url in the person. wsdl file first. The url here (the specific url is determined based on your Directory) is what you want to provide to. NET developers. 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.