Simple web service implementation

Source: Internet
Author: User

First, the three elements of web service are

 

1. soap information in xml format. The header is sent over http. The sending format is similar to the following:

 

<! -- Http header information -->
POST or InStock HTTP/1.1
Host: www.example.org
Content-Type: application/soap + xml; charset = UTF-8
Content-Length: 1024

 

<? Xml version = "1.0"?>

 

<! -- Root element, indicating that the xml format data is soap information -->
<Soap: Envelope xmlns: soap = "http://www.w3.org/2001/12/soap-envelope" soap: encodingStyle = "http://www.w3.org/2001/12/soap-encoding">

 

// You can use soap: Header for verification. If the request address does not need to be verified, you do not need to add Header information -->

 

Body, indicating the method to request the soap server and the parameter -->
<Soap: Body>

 

<! -- Method name -->

 

<Methodname>

 

<! -- Parameter Name and Value -->
<Param> hello world <param>
<Methodname>
</Soap: Body>

</Soap: Envelope>

 

 

 

 

The response format is the corresponding http response header, and soap response information similar to the xml format sent (see http://www.w3school.com.cn/soap/soap_example.asp)

 

2. The web service description language (wsdl) that describes how the client accesses the server method is in xml format.

 

3. There is also a uddi

 

Since simple web services must be implemented locally, uddi is ignored.

 

A complete web service request is as follows:

 

========================================================== ======================================

 

Client-> request (http head + soap xml)-> wsdl-> server-> handle function-> response (http head + soap xml)-> client

 

========================================================== ======================================

 

Wsdl acts as a channel, receives request information, and directs the operation to the server.

 

The request end and service response end can be different platforms and different languages, as long as they can read the wsdl file.

 

To put it bluntly, web service uses xml as an information bridge between different development languages based on the situation that xml can be parsed in various languages.

 

The following is a simple Local web service php implementation. All three files are located in the http: // localhost/lake/soap/directory.

 

1. wsdl file: test. wsdl

 

 

 

<? Xml version = '1. 0' encoding = 'utf-8'?>
<Definitions name = 'sea'
TargetNamespace = 'HTTP: // localhost/lake/soap'
Xmlns: tns = 'HTTP: // localhost/lake/soap'
Xmlns: soap = 'HTTP: // schemas.xmlsoap.org/wsdl/soap /'
Xmlns: xsd = 'HTTP: // www.w3.org/2001/XMLSchema'
Xmlns: soapenc = 'HTTP: // schemas.xmlsoap.org/soap/encoding /'
Xmlns: wsdl = 'HTTP: // schemas.xmlsoap.org/wsdl /'
Xmlns = 'HTTP: // schemas.xmlsoap.org/wsdl/'>
 
<Message name = 'getrequest'>
<Part name = 'name' type = 'xsd: string'/>
<Part name = 'pass' type = 'xsd: string'/>
</Message>
<Message name = 'getresponse'>
<Part name = 'result' type = 'xsd: string'/>
</Message>
 
<PortType name = 'class'>
<Operation name = 'helloworld'>
<Input message = 'tns: getrequest'/>
<Output message = 'tns: getResponse '/>
</Operation>
</PortType>
 
<Binding name = 'class' type = 'tns: class'>
<Soap: binding style = 'rpc'
Transport = 'HTTP: // schemas.xmlsoap.org/soap/http'/>
<Operation name = 'helloworld'>
<Soap: operation soapAction = ''/>
<Input>
<Soap: body use = 'encoding' encodingStyle = 'HTTP: // schemas.xmlsoap.org/soap/encoding/'/>
</Input>
<Output>
<Soap: body use = 'encoding' encodingStyle = 'HTTP: // schemas.xmlsoap.org/soap/encoding/'/>
</Output>
</Operation>
</Binding>
 
<Service name = 'class'>
<Port name = 'goforsoap 'binding = 'class'>
<Soap: address location = 'HTTP: // localhost/lake/soap/soapserver. php'/>
 
</Port>
</Service>
</Definitions>

<PortType> is the core of the entire wsdl file, which is equivalent to the wsdl class. The method of defining the class below can be considered as <portType> is to define the service provided by the web service, similar to the above

 

<PortType name = 'class'>: I have a class called class.

 

<Opration name = 'helloword'>: there is a method named helloworld in my class.

 

<Input message = '...'>: The parameter description name used by the client to request the helloworld method from the server. It corresponds to the name of the preceding message label.

 

<Output message = '...'>: Description of the return value of the helloworld method by the server, corresponding to the name of the preceding message label.

 

Then, there are two <message> labels, namely the description, type, and Parameter Name of the input and output parameters.

 

<Binding> bind the soap protocol. For more information, see http://www.w3school.com.cn /.

 

Finally, define the target address of the web service. I will create a soapserver. php file under http: // localhost/lake/soap/as the web service file, so...

 

Note that the binding attribute in <port> must be set to the class defined previously.

 

After the wsdl is defined, the rest is simple.

 

2. soapclent. php

 

<? Php
$ SoapClient = new SoapClient ("test. wsdl ");
Echo $ soapClient-> helloworld ('Lake ', '123 ');
?>

3. soapserver. php

 

<? Php
Class serverClass
{
Function helloworld ($ name, $ pass)
{
Return "I got that you are {$ name}, and your password is {$ pass }";
}
}
$ SoapServer = new SoapServer ("test. wsdl ");
$ SoapServer-> setClass ('serverclass ');
$ SoapServer-> handle ();
?>

Open http: // localhost/lake/soap/soapclient. php In the browser, and a response string such as I got that you are lake, and your password is 1234567 appears.

 

Remember to disable the wsdl cache and enable the openssl extension, which can be set in php. ini.

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.