Team LiB
Previous Section Next Section

Creating Web Services

To create your own Web service, you first need a WSDL description. This is a rather unpleasant task, so the best thing is to take the WSDL description of a Web service that does the same (or at least whose methods have a similar signature). By the way, this is how the WSDL description in Listing 18.1 was developed, too. If you have a look at the description, you see the URL of the Web service:

<soap:address location='http://localhost/php/guid-server.php'/>

Change this according to your setup.

After this is done, the service itself can be written. As you may have guessed, the sample Web service will create a GUID, a Globally Unique Identifier with a prefix provided as a parameter. As you might know, PHP offers this functionality in the uniqid() function. In reality, this Web service is a wrapper for uniqid(), but for this sample this is good enough. Following is the code for the getGuid() function:

function getGuid($prefix) {
  return uniqid($prefix);
}

Now you have only three more stepsor lines of codeto do:

1.
Create a SOAP Server, providing the WSDL description.

2.
Add the getGuid() function to the server.

3.
Let the server handle the rest.

These three commands do the trick:

$soap = new SoapServer("guid.wsdl");
$soap->addFunction("getGuid");
$soap->handle();

Listing 18.2 contains the complete code for this example, including disabling the WSDL cache.

Listing 18.2. The SOAP Server for the getGuid() Method
<?php
  ini_set('soap.wsdl_cache_enabled', 'Off');

  function getGuid($prefix) {
    return uniqid($prefix);
  }

  $soap = new SoapServer('guid.wsdl');
  $soap->addFunction('getGuid');
  $soap->handle();
?>

    Team LiB
    Previous Section Next Section