PHP SOAP Extensions

Source: Internet
Author: User
Tags wso2
Brief introduction


The SOAP extension of PHP 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 they can write some clients to use a given Web services.

The purpose of this SOAP extension in PHP5 is to implement PHP support for Web services. Unlike other methods that implement PHP's support for Web services, SOAP extensions are written in C, so it has a speed advantage over other methods.

The SOAP extension supports the following specifications.

* SOAP 1.1
* SOAP 1.2
* WSDL 1.1

SOAP extensions are primarily used to handle Web services in the form of RPC. However, you can also use WSDL files in the form of text in conjunction with the server and client of the WSDL schema.

This extension uses the GNOME XML library to process XML.

Classes in the extension
This extension implements 6 classes. There are three advanced classes, and their methods are useful, and they are soapclient,soapserver and SoapFault. The other three classes have no other method except the constructor, these three are lower class, they are Soapheader,soapparam and Soapvar.

The

SoapClient class

is used by this class to use Web services. The SoapClient class can be a client for a given Web services.
It has two forms of operation:

* WSDL mode
* non-wsdl mode

in WSDL mode, the constructor can use the WSDL file name as a parameter and extract the information used by the service from the WSDL. Use parameters in the

non-wsdl mode to pass the information that you want to use. This class has a number of useful ways to use the service. where Soapclient::__soapcall () is the most important. This method can be used to invoke an operation in the service. The

SoapServer class

class can be used to provide Web services. Similar to SoapClient, SoapServer also has two modes of operation: WSDL mode and NON-WSDL mode. The meaning of these two patterns is the same as the two modes of soapclient. In WSDL mode, the service implements the interface provided by the WSDL, and in non-wsdl mode, the parameters are used to manage the behavior of the service.

Among the many methods of the SoapServer class, there are three methods that are more important. They are Soapserver::setclass (), Soapserver::addfunction (), and Soapserver::handle (). The

Soapserver::setclass () method sets the class that is used to implement the Web service. All public methods in the class set by Soapserver::setclass will be the operations of the Web Services (operation). The

Soapserver::addfunction () method is used to add one or more functions as Web services operations (operation). The

SoapServer:: Handle () method instructs the Web service script to begin processing incoming requests. A Web service script is an instance of one or more SoapServer objects written in a PHP script. Although you can have more than one SoapServer object, the usual habit is that a script has only one soapserver instance. Before the Soapserver::handle () method is called, the Web service script uses any information that is set on the SoapServer object instance to handle incoming requests and output accordingly.

The


SoapFault class

class inherits from the exception class and can be used to handle errors. The SoapFault instance can throw or get information about a SOAP fault and handle it as requested by the handler. The

SoapHeader class

class can be used to describe SOAP headers. It is just a data container that contains only constructor methods. The

Soapparam class

Soapparam is also a data container that contains only constructor methods. This method can be used to describe the parameters passed to the Web services operation. In non-wsdl mode, this is a useful class that can be used to pass parameter information in the desired format. The

Soapvar class

Soapvar is also a low-level class that contains only constructors, similar to the SoapHeader and Soapparam classes. This class can be used to pass encoding parameters to a Web services operation. This class is useful for passing type information in NON-WSDL.


WSDL VS. NON-WSDL mode

WEB Services has two implementation modes: contract first (contract first) mode and code First mode.

The contract-first pattern uses a WSDL file that is defined by the XML service interface. The WSDL file defines the interfaces that the service must implement or that the client must use. The WSDL pattern for SoapServer and SoapClient is based on this concept.

In code-ahead mode, first write the code that implements the service. Then in most cases, the code produces a contract, in other words, a WSDL. The client can then use the WSDL to obtain the interface of the service when using the service. However, the PHP5 extension does not export a WSDL rule from the code, and with this in mind, you can use SoapServer and soapclient in non-wsdl mode.
SOAP extension with Hello World

This section describes how to implement services and clients using the WSDL schema and the non-wsdl pattern. It is relatively easy to implement services and clients using the WSDL schema, assuming there is already a WSDL file that defines the interface. So this section first describes how to implement a Web Service using the WSDL schema.

There is an operation named greet in the service of this Hello World example. This operation has a string form name and returns a string form of greeting. The WSDL used is as follows:


Xmlns:impl= ' Http://wso2.org/wsf/php/helloService '
xmlns:intf= ' Http://wso2.org/wsf/php/helloService '
Xmlns:wsdl= ' http://schemas.xmlsoap.org/wsdl/'
xmlns:wsdlsoap= ' http://schemas.xmlsoap.org/wsdl/soap/'
Xmlns:xsd= ' Http://www.w3.org/2001/XMLSchema '
Targetnamespace= ' Http://wso2.org/wsf/php/helloService ' >


Xmlns:impl= ' Http://wso2.org/wsf/php/helloService '
xmlns:intf= ' Http://wso2.org/wsf/php/helloService '
Xmlns:wsdl= ' http://schemas.xmlsoap.org/wsdl/'
Xmlns= "Http://www.w3.org/2001/XMLSchema"
Targetnamespace= ' Http://wso2.org/wsf/php/helloService ' >













































WSDL Mode service

The following is the SOAP extension API code used by the WSDL schema service:

function greet ($param) {
$retval = ' Hello '. $param->name;
$result = Array (' greetreturn ' = = $retval);
return $result;
}

$server = new SoapServer (' hello.wsdl ');
$server->addfunction (' greet ');
$server->handle ();
?>

In the implementation of this service, the function implements the service operation defined by the WSDL Greet,greet operation has a wsdl specified parameter, according to the semantics of the greet operation, this parameter is a user's name. Finally, handle invokes the service object that triggered the processing request.


WSDL Mode Client

The client code is as follows

try {
$client = new SoapClient (' hello.wsdl ');
$result = $client->__soapcall (' Greet ', Array (' name ' = ' Sam '));
printf ("Result =%s", $result->greetreturn);
} catch (Exception $e) {
printf ("Message =%s", $e->__tostring ());
}
?>

In the client code, first create a soapclient instance that uses the WSDL file as a parameter. The __soapcall () then invokes the operation that passed in as a parameter, that is, the parameters of the greet and incoming operations.
Requests and responses

When you place the above PHP script in a document in your Web server directory and use a Web browser or a command line in the PHP parser to invoke the script, the client sends a SOAP request to the server-side script, and the server sends a SOAP response to the client to respond to the client's request.

The following is a SOAP request sent by the client:



xmlns:ns1= "Http://wso2.org/wsf/php/helloService" >


Sam


The following is the SOAP response sent by the service side in response to an appeal request:



xmlns:ns1= "Http://wso2.org/wsf/php/helloService" >


Hello Sam


The SOAP messages above are obtained using the WSDL schema of the service side and the client. You can also use the service side and client of the NON-WSDL mode to generate the same SOAP message as above. However, the PHP code must change a little. The next section explains how to use the NON-WSDL mode.

NON-WSDL Mode service Side

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 non-wsdl mode, the function of the greet function is implemented as a WSDL pattern, but the function is implemented in a slightly different way than the WSDL schema. In non-wsdl mode, we must return a Soapparam object as a response, not an array. When you create a service, the first parameter is set to NULL, the WSDL is not provided, and then an option is passed as the parameter, which is the URI of the service. Finally, the remaining methods are called like the WSDL schema.

NON-WSDL Mode Client

try {
$client = new SoapClient (NULL,
Array (' location ' = ' http://localhost/hello/hello_service_nonwsdl.php ',
' uri ' = ' http://wso2.org/wsf/php/helloService '));
$result = $client->__soapcall (' Greet ', Array (new Soapparam (' Sam ', ' name ')); printf ("Result =%s", $result);
} catch (Exception $e) {
printf ("Message =%s", $e->__tostring ());
}
?>

In non-wsdl mode, because WSDL is not used, a parameter array containing the location and service URI of the service is passed as a parameter. The __soapcall () method is then called as in WSDL mode, but the Soapparam class is used to package the parameters in the specified format. The returned result gets the response in greet.


Conclusion

This article describes the SOAP extension, which can be used in PHP to provide and use Web Services. The strength of PHP extensions is its simplicity and speed. It is very simple to use SOAP extensions written in C to run the server and client. While SOAP extensions are useful when dealing with some simple Web services, it is limited when it comes to handling all Web services. WSO wsf/php is developed to compensate for the shortcomings of PHP extensions, which are open source, can implement soap-like functionality and support mtom,ws-addressing,ws-security and ws-relaiablemessaging. The WSO2 wsf/php supports APIs similar to SOAP extensions. We are planning to package the API to provide the same API as the SOAP extension, which will be written in C.

  • 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.