- class FruitQuoteService
- {
- public $__dispatch_map = array();
- public $__typedef = array();
- public function FruitQuoteService()
- {
- $this->__dispatch_map['getQuote'] = array(
- "in" => array("category" => "string"),
- "out" => array("quote" => "int")
- );
- $this->__dispatch_map['getFruit'] = array(
- "in" => array(),
- "out" => array("fruitSummary" => "{urn:FruitQuoteService}fruitStruct")
- );
-
- $this->__typedef['fruitStruct'] = array(
- 'category'=>'string', 'amount' => 'int'
- );
- }
-
- public function getQuote($category)
- {
- switch ($category)
- {
- case 'apple':
- $quote = 10;
- break;
- case 'orange':
- $quote = 12;
- break;
- case 'banana':
- $quote = 20;
- break;
- default:
- $quote = 0;
- break;
- }
- return $quote;
- }//end funtion
-
- public function getFruit()
- {
- $list = array(
- array("apple", 100),
- array("orange", 500),
- array("banana", 260)
- );
- return $list;
- }//end funtion
- }//end class
- ?>
第二步:创建server.php 这个程序将接收并处理客户端的请求
- require_once("FruitQuoteService.php");
- require_once("SOAP/Server.php");
- $fruitQuote = new FruitQuoteService();
- $server = new Soap_Server();
- $server->addObjectMap($fruitQuote, "http://www.xxx.com");
- if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD']=='POST')
- {
- $server->service($GLOBALS['HTTP_RAW_POST_DATA']);
- } else
- {
- require_once 'SOAP/Disco.php';
- $disco = new SOAP_DISCO_Server($server,'FruitQuoteService');
- header("Content-type: text/xml");
- if (isset($_SERVER['QUERY_STRING']) && strcasecmp($_SERVER['QUERY_STRING'],'wsdl')==0) {
- echo $disco->getWSDL();
- } else {
- echo $disco->getDISCO();
- }
- }
- exit;
- ?>
-
现在可以通过http://www.shangyong.com/ws/server.php?wsdl 查看wsdl文档。
DISCO:一项微软用来发布和发现Web服务的技术,定义了一个从给定的url获取web服务描述的简单的HTTP GET机制
第三步:创建web服务客户端代码
- require_once('SOAP/Client.php');
- //这个名称空间必须和server.php中定义的一致
- $options = array('namespace' => 'http://www.xxx.com',
- 'trace' => 1); //为1表示可以通过__get_wire获取soap消息,默认是0
- $client = new SOAP_client("http://www.shangyong.com/ws/server.php");
- $params = array();
- $response = $client->call("getFruit", $params, $options);
- //print_r($client->__get_wire()); //输出 soap消息
- if (PEAR::isError($response)) {
- echo 'Error: ' . $response->getMessage() . "
\n"; - } else {
- print_r($response) . "\n";
- }
- $params = array("name" => "orange");
- $response = $client->call("getQuote", $params, $options);
- if (PEAR::isError($response)) {
- echo 'Error: ' . $response->getMessage() . "
\n"; - } else {
- echo $response . "\n\n";
- }
- exit;
- ?>
客户端代码2
- require_once('SOAP/Client.php');
- /**
- * 所有的服务内容,如:命名空间、UEL, 参数名等都可以从wsdl文件获取
- */
- $wsdl = new SOAP_WSDL("http://www.shangyong.com/ws/server.php?wsdl");
- /**
- * 从wsdl生成一个proxy对象,这个对象包含wsdl文档中定义的所有操作的方法。
- * 可以通过proxy对象直接调用函数
- * 优点:易于用户使用
- */
- $client = $wsdl->getProxy();
- $response = $client->getQuote("apple");
- if (PEAR::isError($response)) {
- echo 'Error: ' . $response->getMessage() . "
\n"; - } else {
- echo $response . "\n\n";
- }
- $response = $client->getFruit();
- if (PEAR::isError($response)) {
- echo 'Error: ' . $response->getMessage() . "
\n"; - } else {
- print_r($response) . "\n";
- }
- exit;
- ?>
|