PHPSOAP extension details

Source: Internet
Author: User
Tags soap client
PHPSOAP extension SOAP is called Simple Object Access Protocol (SimpleObjectAccessProtocol ). It is an XML-based, scalable communication protocol. SOAP provides a standard that allows applications running on different platforms and written in different programming languages to communicate with each other. Scalability of SOAP and platform unavailable

Php soap extension SOAP is called Simple Object Access Protocol ). It is an XML-based, scalable communication protocol. SOAP provides a standard that allows applications running on different platforms and written in different programming languages to communicate with each other. Scalability of SOAP and platform unavailable

PHPSOAP Extension
SOAP is called Simple Object Access Protocol ). It is an XML-based, scalable communication protocol. SOAP provides a standard for running in differentPlatformAnd use different programmingLanguageCompiledApplicationProgramCan communicate with each other. The scalability and platform independence of SOAP make it widely usedWeb ServiceCommunication protocol.
Java provides good support for SOAP. Generally, Web Service-based applications are written in Java. For the majority of PHP programmers, there may be a little bit of dissatisfaction-earlier versions of PHP do not directly support SOAP, but only through PEAR (the PHP Extension and Application Repository) the SOAP library or third-party product NuSOAP inDevelopmentWeb service. However, the latest version has changed this situation. The built-in SOAP extension (ext/soap) has been added since PHP 5, so we do not need to download additional extension libraries orCodeTo develop SOAP-based applications. Next, let's take a look at what is included in the SOAP extension.

The SOAP extension (ext/soap) in PHP 5 is already provided in PHP 5 5.0.0, the earliest release, but the extension was experimental in the PHP manual at that time). In fact, the version at that time has been quite completeFunctionAnd there is no need to worry about it. At present, this extension is still being improved. Most bugs in earlier versions have been corrected. Currently, the latest version (5.3.0) provides complete support for SOAP, we have reason to believe that future versions will be better.
SOAP extension library structure
Ext/soap contains six predefined classes. Through these classes, we can create a Web server (SoapServer class), a client (SoapClient class), and process SOAP requests and responses (SoapHeader, soapParam, SoapVar class), and SoapFault class ). The relationship between these classes 1 is shown in:

Figure 1. SOAP Extension Structure

SOAP service SoapServer
The SoapServer class is used to develop Web server applications. This class containsFunction. You can add operations to the Web service in either of the following ways ). One way is to add a defined function directly, and the other way is to add a defined class to add the public member function of the class to the Web service.
Another feature to be noted is that PHP supports two types of Web ServicesMode: The WSDL mode and the non-WSDL mode Start with the two implementation modes of Web Services for ease of understanding.
Two Web Service Modes in PHP: WSDL mode and non-WSDL Mode
For Web services, there are two main implementation modes: Contract First mode and Code First mode.
In the implementation of the contract first mode, the first task is to define the WSDL (Web Services Description Language, Web Service Description Language) for the Web service)File. The WSDL file describes the Web service location, available operation sets, and other attributes. The WSDL file is the "contract" of the Web service ". After the "contract" is concluded, the server and client applications are developed accordingly. This mode corresponds to the WSDL mode mentioned in the previous section. The example we will introduce later is implemented using this mode.
Unlike the contract first mode, in the code first mode, the first step is to implement the Web server, and then generate the WSDL file in some way (automatically generated or manually written) based on the implementation of the server. However, since PHP does not provide a method to generate a WSDL file from the Web service implementation code, it is necessary to connect to the server in non-WSDL mode, that is, to create SoapServer and SoapClient examples without using the WSDL file, instead, it directly transmits necessary parameters to the constructor. Of course, there are other solutions for the code first mode, some integrated PHP developmentTools(For exampleZendStudio) provides the function to generate a WSDL file based on the Web service code.


SoapClient
The SOAP client class SoapClient is used to develop client programs for Web Services. Available member functions include creating client instances, calling available operations, querying available operations and data types. In addition, the function that can be used for program debugging-obtains the SOAP data of the last request and response.
SOAP parameter class SoapHeader, SoapParam, SoapVar
SoapParam and SoapVar are mainly used to encapsulate data used in SOAP requests. They are mainly used in non-WSDL mode. In fact, in the WSDL mode, the parameters of the SOAP request can be packaged in arrays. The SOAP extension will convert the array into the data part of the SOAP request according to the WSDL file, therefore, these two classes are not required. In non-WSDL mode, since the WSDL file is not provided, the two classes must be used for packaging.
The SoapHeader class is used to construct a SOAP header. the SOAP header can be used to expand the capabilities of SOAP. One of the main functions of the SOAP header is simple identity authentication, which will be illustrated in the following example.
SoapFault
This class is inherited from the PHP Exception class and can be used to implement the Exception Handling Mechanism in SOAP, Which is thrown by the SOAP server. The SOAP client can receive instances of this type for obtaining useful debugging information.

Install the SOAP extension to use the SOAP extension, we need to install it on the Web server. There are several factors to consider.

  • Prerequisites for installation: You can find them in the official user manual. ext/soap extensions use the gnome xml library, therefore, you need to install the Library (2.5.4 or later) before installing the SOAP extension ).
  • PHP installed or not:
    • If you want to add the SOAP extension while installing PHP, it is no longer easy. If you are downloading PHP source code, you only need to add the option -- enable-soap in the configure command at compilation. If you directly use a binary file for installation (usually only for Windows), this extension is included in the installation package without additional installation.
    • If you need to add a SOAP extension to the already installed PHP, you need to do more work. Before compiling the source code of the SOAP extension, you must use the phpize command to set the compiling environment, then use the configure command, and then compile and install the SOAP extension.

After compiling and installing the SOAP extension, we also need to modify the PHP configuration file so that the SOAP extension can be correctly loaded by PHP. For Linux, the following code must be added to php. ini:

Extension = php_soap.so [/pre]


For Windows platforms, the code to be added is:

Extension = php_soap.dll [/pre]


In addition, you may also need to set the location of the extension library. This information is saved in the extension_dir domain of php. ini, for example:

Extension_dir = "/usr/local/php/lib/" [/pre]


After completing the preceding work, you must note that the SOAP extension has an independent code segment in the configuration file:

Listing 1. SOAP extension settings in php. ini

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 = "C:/xampp/tmp"; (time to live) Sets the number of second while cached file will be used; instead of original one. soap. wsdl_cache_ttl = 86400 [/pre]


The three settings are mainly used to specify the cache behavior when PHP processes the WSDL file. These three settings respectively indicate whether to enable the cache, the cache file path, and the cache survival time. Enabling the cache will speed up PHP's processing of the WSDL file, but it is best to disable the cache when debugging the code to avoid some problems caused by cache behavior.

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.