-
- 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 . "nn";
- }
- exit;
- ?>
复制代码
客户端代码2
-
- require_once('SOAP/Client.php');
- /**
- * All service contents, such as namespace, UEL, parameter names, etc., can be obtained from wsdl files
- */
- $wsdl = new SOAP_WSDL("http://www.shangyong.com/ws/server.php?wsdl");
- /**
- * Generate a proxy object from wsdl. This object contains all operation methods defined in the wsdl document.
- * Functions can be called directly through proxy objects
- * Advantages: easy for users to use
- */
- $client = $wsdl->getProxy();
- $response = $client->getQuote("apple");
- if (PEAR::isError($response)) {
- echo 'Error: ' . $response->getMessage() . "
n";
- } else {
- echo $response . "nn";
- }
- $response = $client->getFruit();
- if (PEAR::isError($response)) {
- echo 'Error: ' . $response->getMessage() . "
n";
- } else {
- print_r($response) . "n";
- }
- exit;
- ?>
复制代码
|