Automatically Generating WSDL with PEAR::SOAP

PEAR::SOAP can also automatically create WSDL; however, it needs some assistance by the programmer. First, the constructor of the Web Service's class must set a __dispatch_map property that contains a list of input and output parameters. The __dispatch_map property is an array (therefore, several operations per Web Service are supported), and the name of the function is the key for the signature.

Also, you need some code that checks the URL. If ?wsdl is appended, the WSDL must be returned by the service. For this, the SOAP_DISCO_Server class is instantiated and then its method getWSDL() gets called.

A WSDL-enabled Web Service with NuSOAP

class ServiceClass {
  var $__dispatch_map = array();
  function ServiceClass() {
    $this->__dispatch_map['add'] = array(
      'in'  => array('a' => 'int', 'b' => 'int'),
      'out' => array('c' => 'int')
  function __dispatch($method) {
    if (isset($this->__dispatch_map[$method])) {
      return $this->__dispatch_map[$method];
    } else {
      return null;

The following contains the rest of the code for the server; as you would expect, calling the URL with ?wsdl at the end creates the service's WSDL description.

  error_reporting(E_ALL ^ E_NOTICE);
  require_once 'SOAP/Server.php';
  $soap = new SOAP_Server;
  $service = new ServiceClass();
  $soap->addObjectMap($service, 'urn:php-hoshmand-
  if (isset($_SERVER['REQUEST_METHOD']) &&
  } else {
    require_once 'SOAP/Disco.php';
    $disco = new SOAP_DISCO_Server($soap,
    if (isset($_SERVER['QUERY_STRING']) &&
      strpos($_SERVER['QUERY_STRING'], 'wsdl') ===
        0) {
      header('Content-type: text/xml');
      echo $disco->getWSDL();
  class ServiceClass {
    // ...

Consuming a Web Service with PEAR::SOAP

After the Web Service is created and does offer WSDL generation, a proxy class is very easy to get, thanks to PEAR::SOAP. Instantiate the SOAP_WSDL class with the WSDL description, call getProxy(), and then use the proxy. This is very short but does quite a lot of SOAP in the background.

Consuming the Web Service with PEAR::SOAP

  error_reporting(E_ALL ^ E_NOTICE);
  require_once 'SOAP/Client.php';
  $soap = new SOAP_WSDL('http://localhost/
  $proxy = $soap->getProxy();
  $result = $proxy->add(47, 11);
  if (PEAR::isError($result)) {
    echo $result->getMessage();
  } else {
    echo "47 + 11 = $result";

by BrainBellupdated