The ability to provide data and functionality to other Internet-based web applications through web services is quickly becoming an integral part of any major development. Although Oracle provides many ways to host web services, doing so is not always the most efficient approach, especially if you are already using PHP to develop web applications. In this manual, I'll walk you through the step-by-step development of a SOAP client and server using PHP, using Oracle as the backend for the data.
To really understand the answer to this question, you need to understand the life cycle of PHP script execution and the impact of the web server on this life cycle. This manual will start to discuss it step by step.
Required Components
For the purposes of this article, you will be using a very simple database backend that will store the published information in a table Some basic information about books, the table is represented by the following CREATE statement:
CREATE TABLE books(isbn VARCHAR(32) PRIMARY KEY,author VARCHAR(50),title VARCHAR(50),price FLOAT); Copy after login |
This table will act as a data source for the SOAP server, and the data source will provide data to one or more SOAPs as needed. client. Although in a real application, your database may be more complex, the approach described here will still apply.
After setting up your database (ideally placing some dummy data in it), you can now dive into what is involved in developing a SOAP server in PHP.
How SOAP services work in PHP
There are several options for developing SOAP services in PHP, all involving the SoapServer PHP class. This class is the core part of all PHP-based SOAP services, and its syntax is as follows:
$server = new SoapServer($wsdl [, $options]); Copy after login |
where $wsdl is the location of the Web Services Description Language (WSDL) document describing the hosted service; $options is a A group of key/value pairs that contains all the setting options that need to be considered when creating the service. You'll learn more about WSDL documents later; now let's look at the options available when creating a new SOAP service:
◆soap_version: The SOAP protocol version to use when communicating with clients . Possible options are the constant SOAP_1_1 for SOAP version 1.1 or SOAP_1_2 for SOAP version 1.2.
◆encoding: The character encoding used for this SOAP service (i.e. the string ISO-8859-1).
◆actor: The role URI of this SOAP service.
◆classmap: A set of key/value pairs themselves that map WSDL data types to class names in PHP. If this option is used, PHP will render these classes to connecting clients based on the types defined in the WSDL.
So, to create a SOAP service using the SOAP v1.2 protocol using a WSDL document named bookman.wsdl, you should build the server as follows:
$server = new SoapServer(“bookman.wsdl”, array(‘soap_version’ => SOAP_1_2)); Copy after login |
The process The next step is to create the 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, passing the function name to the method to expose to the client:
function add($a, $b) {return $a + $b;}$server->addFunction(‘add’); Copy after login |
You can also add multiple functions by providing a set of function names:
function add($a, $b) {return $a + $b;}function sub($a, $b) {return $a - $b;}$server->addFunction(array(‘add’, ‘sub’)); Copy after login |
Finally, you can export all defined functions by passing the special constant SOAP_FUNCTIONS_ALL instead of the function name, like this:
function add($a, $b) {return $a + $b;}function sub($a, $b) {return $a - $b;}$server->addFunction(SOAP_FUNCTIONS_ALL); Copy after login |
1