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.     phrasebook.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...

Figure 9.3. Now NuSOAP automatically generates an info page for the service.


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





PHP Phrasebook
PHP Phrasebook
ISBN: 0672328178
EAN: 2147483647
Year: 2005
Pages: 193

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net