This section describes a tutorial example of a complete SOAP application with both server and client programs.
Okay. Now let's build our first SOAP server. I want the first server to perform a very simple
function of return a greeting string based on the input name. Here is my version called, HelloServer.php:
<?php # HelloServer.php
# Copyright (c) 2005 by Dr. Herong Yang
#
function hello($someone) {
return "Hello " . $someone . "!";
}
$server = new SoapServer(null,
array('uri' => "urn://www.herong.home/res"));
$server->addFunction("hello");
$server->handle();
?>
The sever application is ready. Note that:
This SOAP server application can not be used as a standalone SOAP server. It needs a HTTP server
to receive SOAP requests, and a PHP runtime to act as a CGI to feed SOAP requests.
The "uri" value is just an unique identification, used as the namespace for the response message.
The HTTP server I will be using is the MS IIS (Internet Information Services). It has already configured
correctly to interface with PHP CGI. For details, see my other book: "Herong's Tutorial Notes on PHP".
All I have to do is to move my server application to IIS document directory:
>copy HelloServer.php \Inetpub\wwwroot
To test my server application, I wrote this client application, HelloClient.php:
<?php # HelloClient.php
# Copyright (c) 2005 by Dr. Herong Yang
#
$client = new SoapClient(null, array(
'location' => "http://localhost/HelloServer.php",
'uri' => "urn://www.herong.home/req",
'trace' => 1 ));
$return = $client->__soapCall("hello",array("world"));
echo("\nReturning value of __soapCall() call: ".$return);
echo("\nDumping request headers:\n"
.$client->__getLastRequestHeaders());
echo("\nDumping request:\n".$client->__getLastRequest());
echo("\nDumping response headers:\n"
.$client->__getLastResponseHeaders());
echo("\nDumping response:\n".$client->__getLastResponse());
?>
Check your IIS server to make sure it is running. Then run HelloClient.php. You will get: