Php learning path: the use instance of soap in php and the generation of the WSDL file provide a class library that automatically generates the WSDL file ?? SoapDiscovery. class. php class

Source: Internet
Author: User
Php learning path: the use instance of soap in php and the generation of the WSDL file provide a class library that automatically generates the WSDL file ?? SoapDiscovery. class. php class 1. web service popularity: my opinion on the differences between Webservice soap and wsdl

Web Service: Web Service is the one that really "handles" and provides a Service interface collectively.
WSDL provides "document description of what can be done": a description format of the service to be provided. I want to help you, but I want to tell you what I can do and the parameter types required to do these tasks.
SOAP provides the "request" specification: the format of the request transmitted to the service interface, including methods and parameters. If you want people to do things, you have to tell them what you want to do. SOAP defines the "request" format, according to the "request" format defined by SOAP, "write" requests can ensure that the Web Service can correctly interpret what you want it to do and what parameters you provide for it. The main problems you need to describe in this request are: Which Web Service sends the request, the request parameter type, parameter value, and return value type. The SOAP messages that comply with the SOAP specifications are completed.

Although wsdl and soap are two standards of web service, they are not necessarily related and can be used independently.
Wsdl provides a unified interface and has become an internationally recognized standard, different types of resources (such as java, c #, C, C ++) can be accessed through the interface provided by the wsdl, because the wsdl is based on xml and has nothing to do with the language platform. In addition, wsdl provides binding and service elements to bind interfaces to specific services and implement separation between interfaces and implementations.

Soap (Simple Object Access Protocol) is an http-based transmission protocol used to access remote services.

The relationship between wsdl and soap is that the used protocol can be set when the wsdl is bound to a service. The protocol can be soap, http, smtp, ftp, or any other transmission protocol, in addition, the wsdl can be bound to jms, ejb, and local java. However, the binding and service elements must be extended, and the server functions must be extended to support such extensions.

Soap is a request and response protocol specification, while http is a web transmission protocol. soap transmission can be based on http, but it can also be based on other transmission protocols, such as ftp and smtp.

Simple Object Access Protocol (SOAP) is a Note organized by W3C. it describes a lightweight protocol for exchanging information in a distributed or distributed environment. SOAP is an XML-based protocol. it consists of three parts: SOAP encapsulation (Envelop), which defines a description of the content in a message and who sent the message, framework for who should accept and process it and how to process it; the SOAP Encoding rule (Encoding Rules) is used to represent the data type instances required by the application; RPC Representation indicates the protocol for remote process calls and responses. SOAP can be bound to multiple transmission protocols and exchange information using the underlying protocol. This document only defines how SOAP is bound to HTTP and HTTP extensions.

SOAP is a communication protocol. based on the HTTP protocol, SOAP writes the REQUEST parameters in XML and submits the processing of web service servers (SERVLET, ASP or something) on the http body, the results are also written in XML as RESPONSE and sent back to the user end. to make the user end and web service correspond to each other, you can use WSDL as the description file for this communication method, the WSDL tool can be used to automatically generate WS and user-end framework files. SOAP can be used to serialize complex objects and bind them to XML.

The predecessor of SOAP is RPC, which is the remote call processing protocol. this protocol is not secure. most firewalls block RPC communication packets, while SOAP uses HTTP as the basic protocol, port 80 enables SOAP to implement RPC through the firewall.

Like HTTP, the SOAP protocol is the underlying communication protocol, but the request package format is different. the SOAP package is in XML format, now we do not need to understand SOAP in depth when writing WEB services. If the SERVICE and CLIENT use SOAP in the same environment, generally there are tools to automatically generate the SOAP program framework, so it doesn't matter if you don't know the details. however, if the environment of the CLIENT and SERVICE is different, such as the JAVA Client and.. net service, or the java service under TOMCAT and the vb client, it is better to know some details. in particular, neither WSDL nor UDDI is a standard. if it is not used, you have to manually prepare the soap message.

2. use the class "SoapDiscovery. class. php" to generate the wsdl document!

SOAP interface
Unrelated programming language, platform, and good scalability

There are two methods to implement a SOAP interface: one is the WSDL file method and the other is the non-WSDL file method!
For those who love Research, the first method can give you a clear understanding of how PHP creates a Web Service! However, the first type is difficult for beginners to create a WSDL file in XML format. First, you should be familiar with what XML is! Learn XML syntax! But for someone eager to solve the problem! Not so much time to get familiar with it! So this is an annoyance! But don't worry. as mentioned above, there is another method that does not require a WSDL file! In addition, this tutorial also provides a class for automatically generating the WSDL file!

Before proceeding, configure the soap environment support for PHP:
Find the php. ini file
; Extension = php_soap.dll
Delete ";" and restart the apache server.

I. WSDL file method
Here we will first 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 web service and install zend studio.
Here is a simple method to download SoapDiscovery online. class. php class (I downloaded it locally), which has a public method: getWSDL (). The return statement is used at the end of this method. then, modify this method:
// Return sprintf ('% s % s', $ headerWSDL, $ portTypeWSDL, $ bindingWSDL, $ serviceWSDL, $ messageWSDL ,'');
// Generate the wsdl file and remove the preceding return comment.
$ 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 (the source code is at the end ).
Prepare a Service. php file or function to create the wsdl!
Class Service {

Public function HelloWorld (){
Return "Hello ";
}

Public function Add ($ a, $ B ){
Return $ a + $ B;
}

}
?>

Create the creat_wsdl.php
Include ("Service. php ");
Include ("SoapDiscovery. class. php ");

$ Disc = new SoapDiscovery ('service', 'Soap '); // The first parameter is the class name (the generated wsdl file is named by it, for example: the following Service. wsdl), that is, the Service class. The second parameter is the Service name (which can be written as needed)
$ Disc-> getWSDL ();
# Description: View "SoapDiscovery. class. php "file source code discovery, this member function if the return statement is enabled, will return a string in xml format, if $ strXML = $ disc-> getWSDL (); echo $ strXML; the xml wsdl file cannot be displayed in the browser. Because the browser uses "html/text" by default to return stdout content, it must be used in the header of the php script:
Header ("Content-type: text/html; charset = utf-8"); in this way, the browser can parse the output as html rather than xml!
Run the create_wsdl.php file in web access mode. a Service. wsdl file is generated. This file will be used below!
Note: The wsdl file generated in the preceding method is normal in windows, but in the "location = XXX" section of the wsdl file in Linux, there is indeed no server IP address. Therefore, in this code, $ _ SERVER ['server _ name'] is $ _ SERVER ['server _ host'], because in linux (nginx, the former variable is empty so that the correct wsdl document can be generated!


Add some code to the Service. php file.
Class Service {

Public function HelloWorld (){
Return "Hello ";
}

Public function Add ($ a, $ B ){
Return $ a + $ B;
}

}

$ Server = new SoapServer ('service. wsdl ', array ('soap _ version' => SOAP_1_2); # the Service. wsdl file is generated above.
$ Server-> setClass ("Service"); // register all methods of the Service class
$ Server-> handle (); // process the request
?>

Create a webservice client program and test whether the webservice is valid. the file name is client. php.
Copy the following content:
Ini_set ('soap. wsdl_cache_enabled ', "0"); // disable the wsdl cache.
$ Soap = new SoapClient ('http: // localhost/Dragon/soap/Service. php? Wsdl ');
Echo $ soap-> Add (28, 2 );
Echo $ soap->__ soapCall ('Add', array (28, 2) // or call
# Echo $ soap->__ Call ('Add', array (28, 2 ));

?>
OK! Test passed!

  

II. no WSDL file mode
Server
Class Service
{
Public function HelloWorld ()
{
Return "Hello ";
}
Public function Add ($ a, $ B)
{
Return $ a + $ B;
}
}
$ Server = new SoapServer (null, array ('uri '=> "abcd "));
$ Server-> setClass ("Service ");
$ Server-> handle ();
?>
Client
Try {
$ Soap = new SoapClient (null, array (
"Location" => "http: // localhost/Dragon/soap/Service. php ",
"Uri" => "abcd", // The Resource Descriptor server and client must correspond
"Style" => SOAP_RPC,
"Use" => SOAP_ENCODED
));

Echo $ soap-> Add (12, 2 );
} Catch (Exction $ e ){
Echo print_r ($ e-> getMessage (), true );
}
?>

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.