Automatically Generating WSDL with NuSOAP

As mentioned previously, current Web Services do almost always use WSDL. Writing WSDL manually is a real pain and very error-prone, but most serious Web Services implementations for PHP can create WSDL automatically. However, because PHP is no strongly typed language, they need some help.

To do so with NuSOAP, the code from the previous section must be expanded a bit. First, a method configureWSDL() must be called to provide the name and the namespace of the service. Then, the signature of the method must be provided (which parameters go in, which go out). Then, the server is started. However, this time whether $HTTP_RAW_POST_DATA is set is checked or not. This is because when it is not set, the user has made a GET request, so there might be a chance that he just wants the WSDL description.

A WSDL-enabled Web Service with NuSOAP (wsdl-nusoap-server.php)
<?php
  require_once ‘nusoap.php’;

  $soap = new soap_server;
  $soap->configureWSDL(’AddService’, ‘http://php.
    hoshmand.org/’);
  $soap->wsdl->schemaTargetNamespace = ‘http://
    soapinterop.org/xsd/’;
  $soap->register(
    ‘add’,
    array(’a’ => ‘xsd:int’, ‘b’ => ‘xsd:int’),
    array(’c’ => ‘xsd:int’),
    ‘http://soapinterop.org/’
  );
  $soap->service(isset($HTTP_RAW_POST_DATA) ?
    $HTTP_RAW_POST_DATA : ”);

  function add($a, $b) {
    return $a + $b;
  }
?>


Back to the WSDL: Figure 9.3 shows the Web Service in the browser when called using GET. A click on the link shows some information about the add() method. In Figure 9.4, you see what happens when you append ?WSDL to the URL of the script (or click on the WSDL link): The WSDL for the service is automatically generated. Imagine if you had to do this manually…

[View full size image]


Figure 9.4. This WSDL is generated by NuSOAP, not by the programmer.

[View full size image]


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

One Response to “Automatically Generating WSDL with NuSOAP”

  1. found your site on del.icio.us today and really liked it.. i bookmarked it and will be back to check it out some more later ..

Leave a Reply

Spam protection by WP Captcha-Free