2009年5月11日

PHP Web Services

PEAR SOAP

//-- HelloService.php --
<?php
class HelloService {
public $__dispatch_map = array();

public function HelloService() {
$this->__dispatch_map['sayHello'] = array(
'in' => array('name' => 'string'),
'out' => array('sayHelloOut' => 'string'),
);
}

public function sayHello($name) {
return 'Hello '.$name;
}
}

require_once('SOAP/Server.php');
$server = new SOAP_Server();

$server->addObjectMap(new HelloService(), 'urn:HelloService');
if ($_SERVER['REQUEST_METHOD']=='POST') {
$server->service($HTTP_RAW_POST_DATA);
} else {
require_once('SOAP/Disco.php');
$disco = new SOAP_DISCO_Server($server,'HelloService');
header("Content-type: text/xml");
if ($_SERVER['QUERY_STRING']=='wsdl') {
echo $disco->getWSDL();
} else {
echo $disco->getDISCO();
}
}
?>

//-- HelloClient.php --
<?php
require_once('SOAP/Client.php');

$client = new SOAP_Client('http://localhost/HelloService.php?wsdl');
$params = array('name'=>'World');
$response = $client->call('sayHello',$params);
echo $response;
?>

沒有留言:

網誌存檔