15.3.1. ProblemYou want your SOAP method to accept parameters. 15.3.2. SolutionUpdate the method prototype to include arguments: <?php class pc_SOAP_return_time { public function return_time($tz = '') { // set the time zone based on the input if ($tz) { $my_tz = date_default_timezone_set($tz); } // get the new timestamp $date = date('Ymd\THis'); // reset the time zone to default if ($tz) { date_default_timezone_set(ini_get('date.timezone')); } // return the timestamp return $date; } } $server = new SOAPServer(null,array('uri'=>'urn:pc_SOAP_return_time')); $server->setClass('pc_SOAP_return_time'); $server->handle(); ?> 15.3.3. DiscussionThe basics of serving SOAP requests are covered in Recipe 15.2. This recipe extends that example to demonstrate how to accept method arguments. Read in parameters by altering the method prototype to include parameter names. Then modify the client request to include data for the additional arguments. Example 15-7 modifies the SOAP procedure to accept an optional time zone argument. Processing SOAP methods with parameters
The SOAP client can now pass in a tz option. Here it's Europe/Oslo: <?php $opts = array('location' => 'http://api.example.org/getTime', 'uri' => 'urn:pc_SOAP_return_time'); $client = new SOAPClient(null, $opts); $result = $client->__soapCall('return_time', array('tz' => 'Europe/Oslo')); print "The local time is $result.\n"; ?> With the new setting, the server returns a time nine hours ahead of the previous one: The local time is 20060816T173225. You can pass strings, numbers, arrays, and objects to a SOAP method. The ext/soap extension converts them from XML to native PHP data types. 15.3.4. See AlsoRecipe 15.2 for processing SOAP requests without parameters. |