Making a SOAP Server

Making a SOAP Server

Most often, you'll want to create a server that will provide its clients with a useful service. This can be absolutely anything, ranging from book price quotes to employee information to domain name verification. In this section we create a server that we can connect to with a client to receive a short message. We also use a short WSDL document to describe how the two parts of our SOAP application can communicate. (We have already demonstrated a WSDL document in action, so we won't explain this one again in detail.)

This example receives a name string from the client and returns a message to the effect that we should be glad SOAP is so reliable. Open a new code file called soapserver.php:

   <?php    function sayHello($name){       $salutation = "You, $name, will be delighted to know I am working!";       return $salutation;    }    $server = new SoapServer ("greetings.wsdl");    $server->addFunction("sayHello");    $server->handle();    ?> 

First, although our example is actually a simple "hello world'' example, there is not much further to go in terms of making something really useful. For example, if you wanted to have a Web Service to return results from a database, you would merely need to substitute a couple of functions that returned the relevant results into your SOAP server file, add the functions to the server, modify the WSDL quickly to reflect these changes, and away you go.

The preceding code has a function entitled sayHello(), which is the only method our fledgling service provides. The server can then be created referencing the greetings.wsdl document (which we define in a moment), the sayHello() function added with the PHP5 addFunction() method (discussed earlier in the section "Soapserver Functions"), and then handle() doing the hard work of processing the request and responding. That's all you need from the server's perspective.

To make the service work nicely for a client, you just need to tell prospective clients how they can expect to communicate with the server via the WSDL document. We don't rehash the whole concept of WDSL here, but the following is a good example of what you can use with our simple Web Service. Naturally, it is entitled greetings.wsdl:

   <?xml version ='1.0' encoding ='UTF-8' ?>    <definitions name='greetings'      targetNamespace='http://myserver.co.za/sayHello'      xmlns:tns=' http://myserver.co.za/sayHello '      xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'      xmlns:xsd='http://www.w3.org/2001/XMLSchema'      xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'      xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'      xmlns='http://schemas.xmlsoap.org/wsdl/'>      <message name='sayHelloRequest'>        <part name='name' type='xsd:string'/>      </message>      <message name='sayHelloResponse'>        <part name='salutation' type='xsd:string'/>      </message>      <portType name='sayHelloPortType'>        <operation name='sayHello'>          <input message='tns:sayHelloRequest'/>          <output message='tns:sayHelloResponse'/>        </operation>      </portType>      <binding name='sayHelloBinding' type='tns:sayHelloPortType'>        <soap:binding style='rpc'          transport='http://schemas.xmlsoap.org/soap/http'/>        <operation name='sayHello'>          <soap:operation soapAction=''/>          <input>            <soap:body use='encoded' namespace=''              encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>          </input>          <output>            <soap:body use='encoded' namespace=''              encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>          </output>        </operation>      </binding>      <documentation>This is Wiley's SOAP server Example</documentation>      <service name='sayHelloService'>        <port name='sayHelloPort' binding='sayHelloBinding'>          <soap:address location='http://localhost/prophp5/Soapxml/           soapserver.php'/>        </port>      </service>    </definitions> 

With that, you're all set. Remember, though, that you need to set the address location URI to the correct value for your setup.

Now, all that remains is to make use of the new server. The following file, called soapclient.php, does that:

   <?php    $client = new SoapClient("greetings.wsdl");    print_r($client->sayHello("David"));    ?> 

Navigating to this file will give you the slightly terse, yet reasonably accurate results displayed in Figure 12-6, depending on which name you have substituted in.


Figure 12-6



Professional PHP5 (Programmer to Programmer Series)
Professional PHP5 (Programmer to Programmer Series)
ISBN: N/A
EAN: N/A
Year: 2003
Pages: 182
BUY ON AMAZON

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