Phpsoap details with simple cases

Source: Internet
Author: User
Tags wso2
Three types of soap are available in php: php5soap, pearsoap, and nusoap. They are written in c at a high speed. The last two types are written in php. For installation in windows, Open C: WINDOWSphp. ini. Extensionphp_openssl.dllextensionphp_soap.dll restart apache and check whether soap is supported. Install in linux,

Three types of soap are available in php: php5 soap, pear soap, and nusoap, which are written in c at a high speed. The last two types are written in php. For installation in WINDOWS, Open C:/WINDOWS/php. ini. Extension = php_openssl.dll extension = php_soap.dll restart apache and check whether soap is supported. Install in linux,

Three types of soap are available in php: php5 soap, pear soap, and nusoap, which are written in c at a high speed. The last two types are written in php.

Install

Open C:/WINDOWS/php. ini in the development window and modify it.

Extension = php_openssl.dll

Extension = php_soap.dll

Restart apache to check whether soap is supported.

Install it in linux and enable-soap is included when compiling php.

Use

Php soap extensions can be used to provide and use Web services. In other words, PHP developers can use this PHP extension to write their own Web services, or write some clients to use the given Web services.

SOAP Extensions support the following specifications.

* HTTP 1.1

* Http 1.2

* WSDL 1.1

SOAP extensions are mainly used to process RPC Web services. However, you can also use text-format WSDL files to work with the server and client in the WSDL mode.

This extension uses the gnome xml library to process XML.

Extended class

This extension implements six classes. There are three advanced classes. Their methods are very useful. They are SoapClient, SoapServer, and SoapFault. The other three classes have no other methods except the constructor. These three are low-level classes, which are SoapHeader, SoapParam, and SoapVar.

SoapClient class

This class is used to use Web services. The SoapClient class can be used as a client for a given Web services.

It has two types of operations:

* WSDL Mode

* Non-WSDL Mode

In the WSDL mode, the constructor can use the WSDL file name as a parameter and extract the information used by the Service from the WSDL.

In non-WSDL mode, parameters are used to transmit the information to be used. This class has many useful methods that can be used to use services. SoapClient ::__ soapCall () is the most important. This method can be used to call an operation in the service.

SoapServer class

This class can be used to provide Web services. Similar to SoapClient, SoapServer also has two operation modes: WSDL mode and non-WSDL mode. The two modes have the same meaning as the SoapClient two modes. In the WSDL mode, the Service implements the interface provided by the WSDL. In the non-WSDL mode, parameters are used to manage service behavior.

Among the many methods of the SoapServer class, three methods are important. They are

SoapServer: setClass (),

SoapServer: addFunction (),

SoapServer: handle ().

SoapServer: setClass () method set the class used to implement Web Service. SoapServer: all the public methods in the class set by setClass will be Web Services operations (operation ).

SoapServer: addFunction () method is used to add one or more functions as Web Services operations (operation.

SoapServer: handle () method indicates that the Web Service script starts to process incoming requests. Web Service scripts are instances of one or more SoapServer objects written in PHP scripts. Although you can have more than one SoapServer object, a script usually has only one SoapServer instance. Before calling the SoapServer: handle () method, the Web Service script uses any information set on the SoapServer object instance to process incoming requests and outputs.

SoapFault class

This class is inherited from the Exception class and can be used to handle errors. The SoapFault instance can throw or obtain information about a Soap error and process it as a programmer's request.

SoapHeader class

This class can be used to describe SOAP headers. It is just a data container that contains only the constructor method.

SoapParam class

SoapParam is a data container that only contains the constructor method. This method can be used to describe the parameters passed to the Web services operation. In the non-WSDL mode, this is a very useful class that can be used to pass the parameter information in the expected format.

SoapVar class

SoapVar is also a low-level class that only contains the constructor. It is similar to SoapHeader and SoapParam class. This class can be used to pass encoding parameters to a Web services operation. This class is very useful for passing type information in non-WSDL.

Wsdl vs. non-WSDL Mode

Web Services has two implementation modes: Contract first mode and Code first mode.

The contract first mode uses a WSDL file for the Service Interface defined in XML. The WSDL file defines the interfaces that must be implemented by the service or used by the client. The WSDL mode of SoapServer and SoapClient is based on this concept.

In the code first mode, you must first write the code that implements the service. Then, in most cases, the code will generate a contract, in other words, a WSDL. Then the client can use the WSDL to obtain the service interface when using the service. However, the PHP5 extension does not output a wsdl from the Code. In this case, SoapServer and SoapClient can be used in non-WSDL mode.

SOAP extension and Hello World

This section describes how to use the WSDL mode and non-WSDL mode to implement services and clients. It is relatively easy to use the WSDL mode to implement services and clients. Suppose there is already a WSDL file defining interfaces. This section describes how to use the WSDL mode to implement a Web Service.

In the Hello World example, there is an operation named greet. This operation has a string name and returns a string form of greeting.

WSDL mode service

The following is the SOAP extension API code used by the Service in the WSDL mode:

Plain textphp:

Function greet ($ param ){

$ Retval = 'Hello'. $ param;

$ Result = array ('greetreturn '=> $ retval );

Return $ result;

}

$ Server = new SoapServer ('hello. wsdl ');

$ Server-> addFunction ('greet ');

$ Server-> handle ();

?>

In the implementation process of this service, the function implements the service operation greet defined by the WSDL. The greet operation has a parameter specified by the WSDL, according to the semantics of the greet operation, this parameter is a user name. Finally, handle calls the service object that triggers the Processing request.

WSDL mode Client

The client code is as follows:

Plain textphp:

Try {

$ Client = new SoapClient ('hello. wsdl ');

$ Result = $ client->__ soapCall ('greet ', array ('name' => 'Sam ')));

Printf ("Result = % s/n", $ result-> greetReturn );

} Catch (Exception $ e ){

Printf ("Message = % s/n", $ e->__ toString ());

}

?>

In the client code, first create a SoapClient instance that uses the WSDL file as the parameter. Then, the _ soapCall () call is used as a parameter to pass in its operation, that is, greet and the parameters of the input operation.

Requests and responses

When you place the above PHP script in the document under your web server directory and call the script using a WEB browser or the command line of the PHP parser, the client sends a SOAP request to the server script, and the server sends a SOAP response to the client to respond to the client's request.

The preceding SOAP messages are obtained by the server and client in the WSDL mode. You can also use the non-WSDL mode server and client to generate the same SOAP message as above. However, the PHP code must be slightly changed. The next section describes how to use the non-WSDL mode.

Non-WSDL Server

Plain textphp:

Function greet ($ param ){

$ Retval = 'Hello'. $ param;

Return new SoapParam ($ retval, 'greetreturn ');

}

$ Server = new SoapServer (null, array ('uri '=> 'HTTP: // wso2.org/wsf/php/helloService '));

$ Server-> addFunction ('greet ');

$ Server-> handle ();

?>

In the non-WSDL mode, the greet function is first implemented in the same way as the WSDL mode, but the function implementation method is slightly different from the WSDL mode. In non-WSDL mode, we must return a SoapParam object as the response, rather than an array. When a service is created, the first parameter is set to null, indicating that the WSDL is not provided. Then, an option is passed as a parameter, which is the URI of the service. Finally, call the remaining methods as in the WSDL mode.

Non-WSDL Client

Plain textphp:

Try {

$ Client = new SoapClient (null,

Array ('location' => 'HTTP: // localhost/hello/hello_service_nonwsdl.php ',

'Url' => 'HTTP: // wso2.org/wsf/php/helloService '));

$ Result = $ client->__ soapCall ('greet ', array (new SoapParam ('Sam', 'name '))); printf ("Result = % s/n", $ result );

} Catch (Exception $ e ){

Printf ("Message = % s/n", $ e->__ toString ());

}

?>

In non-WShDL mode, because p does not use WSDL, a parameter array containing the service location and service URI is passed as the parameter. Then the _ soapCall () method is called as in the WSDL mode, but the SoapParam class is used to package parameters in the specified format. The returned result will get the response in greet.

Conclusion

This article introduces the SOAP extension, which can be used in PHP to provide and use Web Services. PHP extensions are simple and fast. Using the SOAP extension written in C to run the server and client is very simple. Although SOAP extensions are useful for processing some simple Web Services, they are limited when used to process all Web Services. Wso wsf/PHP is developed to make up for the defects of PHP extensions. It is open-source and can implement similar SOAP functions and support MTOM, WS-Addressing, WS-Security and WS-RelaiableMessaging. WSO2 WSF/PHP supports APIs similar to SOAP extensions.

Related Article

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.