Categories
PHP

SOAP Web Service

How to create a SOAP service in PHP. You will write code in two files: a server file and a client file. The server file defines the functions that the service can perform, and the client file calls those functions and displays the results.

Enabling SOAP Extension in PHP

The SOAP extension can be used to write SOAP Servers and Clients. Before moving forward, you should make sure that the SOAP extension is enabled. To verify, use the phpinfo() function and look for the SOAP section in the output:

<?php
 echo phpinfo();

If the SOAP section is not available in the output, you need to enable the SOAP extension by following these steps:

  1. Locate the php.ini file in your PHP installation directory (for example, xampp/php/php.ini). You can use the phpinfo() function to find out where it is.
  2. Open the php.ini file with a code or text editor and search for the line contains ;extension=soap directive. Remove the semicolon (;) at the beginning of the line to uncomment it.
  3. Save the php.ini file and restart your web server to apply the changes.
  4. To verify that the soap extension is enabled, you can use the phpinfo() function again and look for the soap section in the output.

Creating a SOAP Server

The PHP soap extension provides SoapServer class to set up a SOAP web service with or without a WSDL (Web Service Description Language) description file.

To provide a SOAP Web Service, instantiate a class. The preceding code creates a Mathematics class for a simple Add, Subtract, and Multiply (we used the same class in the previous tutorial (Yet Another RPC Framework).

Example: Creating SOAP Web Service (nonWSDL Mode)

<?php
 // this page can be accessed by http://locahost/soap_server.php
 class Mathematics {
  public function sum($a, $b) {
   return $this->_sum($a, $b);
  }
  public function subtract($a, $b) {
   return $a - $b;
  }
  public function multiply($a, $b) {
   return $a * $b;
  }
  protected function _sum($a, $b) {
   return $a + $b;
  }
 }

 // Create SOAP server object
 $server = new SoapServer(null, array('uri'=>'http://localhost/soap_server.php'));

 // Register the Class with the server
 $server->setClass('Mathematics');
 
 // Handle the requests from the clients
 $server->handle();
  1. Create a PHP class that defines the methods and parameters of your web service. (we created the Mathematics class).
  2. $server = new SoapServer(null, $options);
    Next, create the SoapServer instance. In a nonWSDL mode, specify the following parameters:
    • Instantiate the SoapServer instance by passing it null for the $wsdl (first) parameter
    • And, specify a uri option (assign current script URL) under the $options (second) parameter.
  3. $server->setClass(‘Mathematics’);
    To handle incoming SOAP requests, use the setClass() instance method to set the class
  4. $server->handle();
    Use the handle() method of the SoapServer class to handle the incoming SOAP requests.

Creating SOAP Client

Now we have a SOAP server, let’s create a SOAP client. Creating a SOAP client in PHP is a relatively simple task, PHP soap extension provides SoapClient class to set up a SOAP client.

<?php
 /* this page can be accessed by http://locahost/soap_client.php */
 $options = array(
  'location' => 'http://localhost/soap_server.php',
  'uri' => 'http://localhost/soap_server.php',
  );
  
 $client = new SoapClient(NULL, $options);
 
 // Mathematics::sum
 echo $client->sum(49,51) . '<br>'; // 100
 
 // Mathematics::subtract
 echo $client->subtract(51,49) . '<br>'; //2
 
 // Mathematics::multiply
 echo $client->multiply(100, 10); // 100

The $client = new SoapClient(null, $options); creates the client instance. In a nonWSDL mode, specify the following parameters:

  • Instantiate the SoapClient instance by passing it null for the $wsdl (first) parameter.
  • Specify the SOAP server address in uri and Location options under the $options (second) parameter.

Executing the client code consumes the SOAP Mathematics class service.

Creating a SOAP Service Using WSDL

Creating Server

In a nonWSDL mode, you pass null for the $wsdl (first) parameter and only pass a ‘uri’ option under the $options (second) parameter, but in a WSDL mode, you just need to pass the WSDL file address.

See How to create WSDL file automatically.

Example: Creating WSDL Soap Server

<?php
 // this page can be accessed by http://locahost/soap_server.php
 require_once 'Mathematics.php'; // PHP Class
 $server = new SoapServer('Mathematics.wsdl');
 $server->setClass('Mathematics');
 $server->handle();

Creating Client

In WSDL mode, just pass the WSDL address to SoapClient class.

Example: Creating WSDL Soap Client

<?php
 /* this page can be accessed by http://locahost/soap_client.php */
  
 $client = new SoapClient('http://localhost/Mathematics.wsdl');
 
 // Mathematics::sum
 echo $client->sum(49,51) . '<br>'; // 100
 
 // Mathematics::subtract
 echo $client->subtract(51,49) . '<br>'; //2
 
 // Mathematics::multiply
 echo $client->multiply(100, 10); // 1000

Communicating with Servers: