<result code="200"><item zip="12209" state="NY"
latitude ="42.64081" longitude ="-73.7856"/></result>
在实验的过程当中,使用了一个抓包工具Wireshark来分析报文。Wireshark很不错,在Filter处设置ip.addr == 208.109.78.12(208.109.78.12 是 www.webservicemart.com 的IP),然后启动监控,可以分析上述调用过程中HTTP包是什么样的。
实战SOAPII
用PHP建立SOAP服务
建立soap_server.php(虚拟路径为:http://172.16.0.24/php/soap/soap_server.php)
<?php
/**
* A simple math utility class
* @author John Coggeshall john@zend.com
*/
class math {
/**
* Add two integers together
*
* @param integer $a The first integer of the addition
* @param integer $b The second integer of the addition
* @return integer The sum of the provided integers
*/
public function add($a, $b) {
return $a + $b;
}
/**
* Subtract two integers from each other
*
* @param integer $a The first integer of the subtraction
* @param integer $b The second integer of the subtraction
* @return integer The difference of the provided integers
*/
public function sub($a, $b) {
return $a - $b;
}
/**
* Div two integers from each other
*
* @param integer $a The first integer of the subtraction
* @param integer $b The second integer of the subtraction
* @return double The difference of the provided integers
*/
public function div($a, $b) {
if($b == 0) {
throw new SoapFault(-1, "Cannot divide by zero!");
}
return $a / $b;
}
}
$server = new SoapServer('math.wsdl', array('soap_version' => SOAP_1_2));
$server->setClass("math");
$server->handle();
?>
注意几点:
math类是即将公开的webservice.
注$server→setClass,不是$server→addClass
用PHP客户端访问刚建立SOAP服务
<?php
//$client = new SoapClient('http://localhost/php/soap/math.wsdl');
$client = new SoapClient("http://localhost/php/soap/soap_server.php?WSDL");
try {
$result = $client->div(8, 2); // will cause a Soap Fault if divide by zero
print "The answer is: $result";
} catch(SoapFault $e) {
print "Sorry an error was caught executing your request: {$e->getMessage()}";
}
?>