Online
 
Thursday, 20 November 2008
 
 

Creating Web Services | Print |  E-Mail
 

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();
?>
This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Jumbo Coklat
 
Top! Top!