The ability to provide data and functions for other Internet-based Web applications through Web Services is rapidly becoming an indispensable part of major development. Although Oracle provides many ways to host Web services, this is never the most effective method, especially when PHP is used to develop Web applications. In this manual, I will guide you through using PHP to gradually develop SOAP clients and servers, and use Oracle as the data backend.
To really understand the answer to this question, you need to understand the lifecycle of PHP script execution and the impact of Web servers on this lifecycle. This manual will start from then on.
Required Components
For the purpose of this article, you will use a very simple database backend that stores some basic information about published books in a table, which is represented by the following CREATE statement:
CREATE TABLE books(isbn VARCHAR(32) PRIMARY KEY,author VARCHAR(50),title VARCHAR(50),price FLOAT); |
The table serves as the data source of the SOAP server, and the data source provides the data to one or more SOAP clients as needed. Although your database may be complex in actual applications, the method described here still applies.
After you create a database (preferably place some virtual data in it), you can now get a deeper understanding of the content involved in developing a SOAP server using PHP.
How the SOAP Service works in PHP
There are multiple options for developing SOAP services using PHP. All methods involve the SoapServer PHP class. This class is the core part of all PHP-based SOAP services. Its syntax is as follows:
$server = new SoapServer($wsdl [, $options]); |
$ Wsdl is the location of the Web Service Description Language (WSDL) document describing the managed service; $ options is a set of key/value pairs, it includes all the settings that need to be considered when creating a service. Later, you will learn more about the WSDL document. Now, let's take a look at the options available when creating a new SOAP service:
◆ Soap_version: the SOAP Protocol version used for communication with the client. Possible options are the constant SOAP_1_1 for SOAP 1.1 or SOAP_1_2 for SOAP 1.2.
◆ Encoding: character encoding (character string ISO-8859-1) used for the SOAP service ).
◆ Actor: Role URI of the SOAP service.
◆ Classmap: map the WSDL data type to a group of key/value pairs of class names in PHP. If this option is used, PHP presents these classes to the connection client based on the types defined in the WSDL.
Therefore, to create a SOAP service using the SOAP v1.2 protocol using the wsdl file named bookman. WSDL, you should build the server as follows:
$server = new SoapServer(“bookman.wsdl”, array(‘soap_version’ => SOAP_1_2)); |
The next step in this process is to create a service method. In PHP, this can be done using two main methods. The first (and most flexible) method is to manually specify each function to be hosted in the service using the addFunction () method, and pass the function name to this method to make it public to the client:
function add($a, $b) {return $a + $b;}$server->addFunction(‘add’); |
You can also add multiple functions by providing a group of function names:
function add($a, $b) {return $a + $b;}function sub($a, $b) {return $a - $b;}$server->addFunction(array(‘add’, ‘sub’)); |
Finally, you can export all the defined functions by passing the special constant SOAP_FUNCTIONS_ALL instead of the function name, as shown below:
function add($a, $b) {return $a + $b;}function sub($a, $b) {return $a - $b;}$server->addFunction(SOAP_FUNCTIONS_ALL); |
1