SOAP1.SimpleObjectAccessProtocol Simple Object Access Protocol mainly includes the following four parts: a) SOAP encapsulation: used to transmit the content of the data, the sender message, receiver information and processing methods are encapsulated to prepare for data transmission. b) SOAP encoding rules... syntaxHighligh
SOAP
1. Simple Object Access Protocol
It mainly includes the following four parts:
A) SOAP encapsulation: it is used to encapsulate the content of transmitted data, the message at the sending end, the information at the receiving end, and the processing method to prepare for data transmission.
B) SOAP encoding rules: used to indicate the data types and other information in the transmitted data.
C) SOAP remote process call protocol: protocol used for remote process call and response
D) SOAP Binding Protocol: underlying protocol used to represent information exchange
SOAP application and configuration in PHP5
========================================================== ======================================
1. added the built-in SOAP extension in PHP 5, which is called ext/soap. It is provided as part of PHP, so you do not need to download, install, and manage separate packages.
2. ext/soap may have been compiled but not loaded. Therefore, you need to update the PHP configuration to load ext/soap. Edit php. ini and find the Dynamic Extensions section. here, add a line of code to automatically load the extension. On Windows, the code line is extension = php_soap.dll.
If no optional extensions have been loaded before, you may also need to set the extension_dir command to point it to the directory containing the extension Library (including php_soap:
Extension_dir = "C:/php/ext/" (use a forward slash on Windows)
3. after the configuration is complete, ext/soap is displayed:
[Soap]
; Enables or disables WSDL caching feature.
Soap. wsdl_cache_enabled = 1
; Sets the directory name where SOAP extension will put cache files.
Soap. wsdl_cache_dir = "/tmp"
; (Time to live) Sets the number of second while cached file will be used
; Instead of original one.
Soap. wsdl_cache_ttl = 86400
This configuration controls the WSDL cache feature of the SOAP extension. By default, the WSDL description file is cached in the/tmp directory within 24 hours (86400 seconds. Now you need to set soap. wsdl_cache_enabled = 0. Otherwise, you may encounter some inexplicable behavior during code development. After the development, remember to open the WSDL cache to make the code run faster.
========================================================== ======================================
SOAP extension Library (mainly including three objects)
1. SoapServer: used to define the functions that can be called and return response data when creating the PHP server page.
Format: $ soap = new SoapServer ($ wsdl, $ array );
Note: $ wsdl is the WSDL file used by SOAP. it is a standard format used to describe WebService. if it is set to NULL, it indicates that the WSDL mode is not used. if you want to use it, you can use ZED to generate it.
$ Array is the property information of SoapServer and an array.
Note: $ array contains a uri and encoding. if it is a client program, it also contains a location.
The 'uri 'option is required, but its value can be left blank ("), but cannot be null.
'Encoding' is required for processing Chinese characters. generally, it is set to 'gb2312' (otherwise, an error occurs because the default value is 'utf-8 ′).
The addFunction method of this object is used to declare which function can be called by the client,
Syntax format: $ soap-> addFunction ($ function_name );
The handle method of this object is used to process user input, call the corresponding function, and finally return the processed result to the client,
Syntax format: $ soap-> handle ([$ soap_request]);
Description: $ soap_request is an optional parameter used to indicate the user's request information. If this parameter is not specified, the server will receive all user requests.
2. SoapClient: used to call the SoapServer page on the remote server and call the corresponding function.
Syntax format: $ soap = new SoapClient ($ wsdl, $ array );
Note: For the soap client, the 'location' and 'URL' options are required. the 'location' must be the URL of the soap server and must be accurate, but the uri may be left empty, but it is better to be consistent with the uri in the soap server. 'encoding' is required for Chinese users.
After this object is created, calling the function on the server page is equivalent to calling the SoapClient method.
Syntax format: $ soap-> user_function ($ params );
Note: user_function is one or more functions defined on the server side that can be called.
3. SoapFault: used to generate possible errors during SOAP access.
Syntax format: $ fault = new SoapFault ($ faultcode, $ faultstring );
Note: $ faultcode is the custom error code. if not, it should be HTTP (in the impression) and $ faultstring is the custom error message.
This object is automatically generated when an error occurs on the server page, or you can create a SoatFault object to obtain the corresponding error information.
After capturing the SoapFault object on the client, you can use the following code to obtain the error message of the error code.
$ Fault-> faultcode; // error code
$ Fault-> faultstring; // error message
The following example:
Soapserver. php
View source
Print
? 01. 02.
03. function reserve ($ arr ){
04. return http_build_query ($ arr );
05 .}
06.
07. $ soap = new SoapServer (NULL, array ('uri '=> 'http: // www.xhbin.com '));
08.
09. // add a user-defined function for the object
10. $ soap-> addFunction ('reserv ');
11.
12. $ soap-> handle ();
13.?>
Soapclient. php
View source
Print
? 01. 02.
03. try {
04.
05. $ client = new SoapClient (NULL, array ('location' => 'http: // localhost/exercise/soap/soapserver. php ', 'uri' => 'http: // www.xhbin.com '));
06.
07. $ arr = array ('id' => 3, "tag" => "php mysql", "search" => "soap ");
08.
09. echo $ string = $ client-> reserve ($ arr );
10.
11.} catch (SoapFault $ fault ){
12.
13. echo "FAULT! Code: ". $ fault-> faultcode." String: ". $ fault-> faultstring;
14.
15 .}
16.
17.?>
The output result is: id = 3 & tag = PHP + MYSQL & search = soap.
Success...
From sunny Sida