class pc_SOAP_return_time {
var $method_namespace = 'urn:pc_SOAP_return_time';
function return_time( ) {
return date('Ymd\THis');
}
}
$rt = new pc_SOAP_return_time( );
Once the class is defined, you create an instance of the class to
link methods with the SOAP server object. Before mapping the
procedures to the class methods, however, you first must instantiate
a SOAP_Server object:
$server = new SOAP_Server;
$server->addObjectMap($rt);
$server->service($GLOBALS['HTTP_RAW_POST_DATA']);
To call this procedure using a PEAR SOAP client, use this code:
require 'SOAP/Client.php';
$soapclient = new SOAP_Client('http://clock.example.com/time-soap.php');
$result = $soapclient->call('return_time', array( ),
array('namespace' => 'urn:pc_SOAP_return_time'));
print "The local time is $result.\n";
This prints:
The local time is 20020821T132615.
To extend the method to read in parameters, you need to alter the
method prototype to include parameter names and then modify the
client request to include data for the additional arguments. This
example modifies the SOAP procedure to accept an optional time zone
argument:
class pc_SOAP_return_time {
var $method_namespace = 'urn:pc_SOAP_return_time';
function return_time($tz='') {
if ($tz) { putenv("TZ=$tz"); }
$date = date('Ymd\THis');
if ($tz) { putenv('TZ=EST5EDT'); } // change EST5EDT to your server's zone
return $date
}
}
The second parameter in the client's call now takes
a tz option:
$result = $soapclient->call('return_time', array('tz' => 'PST8PDT'),
array('namespace' => 'urn:pc_SOAP_return_time'));
With the new settings, the server returns a time three hours behind
the previous one:
20020821T202615