概述(SOAP和XMLPRC比较)

概述(SOAP和XML-PRC比较)

在Web服务发展的初期,XML格式化消息的第一个主要用途是,应用于XML-RPC协议,其中RPC代表远程过程调用。在XML远程过程调用
(XML-RPC)中,客户端发送一条特定消息,该消息中必须包括名称、运行服务的程序以及输入参数。 

XML-RPC只能使用有限的数据类型种类和一些简单的数据结构。人们认为这个协议还不够强大,于是就出现了SOAP——其最初的定义是简单对象访 问协议。之后,大家逐渐意识到SOAP其实并不简单,而且也不需要必须使用面向对象语言,所以,现在人们只是沿用SOAP这个名称而已。

XML-RPC只有简单的数据类型集,取而代之,SOAP是通过利用XML
Schema的不断发展来定义数据类型的。同时,SOAP也能够利用XML
命名空间,这是XML-RPC所不需要的。如此一来,SOAP消息的开头部分就可以是任何类型的XML命名空间声明,其代价是在系统之间增加了更多的复杂性和不兼容性。

随着计算机行业的觉醒,人们发现了基于XML的Web服务的商业潜力,于是,各家公司开始不断地发掘想法、观点、论据以及标准化尝试。W3C曾经设法以“Web服务活动”的名义来组织成果展,其中也包括实际做出SOAP的XML协议工作组(XML
Protocol Working
Group)。与Web服务有关的标准化成果(从某种程度上说与SOAP相关或者依赖于SOAP)的数量已经倍增了到了令人惊讶的程度。

最初,SOAP是作为XML-RPC的扩展而发展起来的,它主要强调的是,通过从WSDL文件中所获得的方法和变量名来进行远程过程调用。现在,通 过不断进步,人们发现了更多的使用SOAP的方式,而不仅仅是采用“文件”方式——基本上是使用一个SOAP信封来传送XML格式化文件。无论如何,要掌 握SOAP,了解WSDL所扮演的角色是最根本的。

SOAP数据包结构解析

SOAP的消息被称为一个SOAP
Envelope,包括SOAP Header和SOAP Body。其中,SOAP Header可以方便的插入各种其它消息来扩充Web
Service的功能,比如Security(采用证书访问Web Service),SOAP Body则是具体的消息正文,也就是Marshall后的信息。

SOAP调用的时候,也就是向一个URL(比如 http://api.google.com/search/beta2 )发送HTTP
Post报文(根据SOAP规范,HTTP Get报文也可被支持),调用方法的名字在HTTP Request Header
SOAP-Action中给出,接下来就是SOAP Envelope了。服务端接到请求,执行计算,将返回结果Marshall成XML,用HTTP返回给客户端。

以下[是移动MISC接入]Wap1.6业务订购数据包样例
MISC1.6的业务订购关系同步的请求包

Java代码 复制代码
  1. <?xml version=“1.0″ encoding=“utf-8″ ?>
  2. <SOAP-ENV:Envelope xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  3. xmlns:xsd=“http://www.w3.org/2001/XMLSchema”
  4. xmlns:SOAP-ENV=“http://schemas.xmlsoap.org/soap/envelope/”
  5. xmlns:SOAP-ENC=“http://schemas.xmlsoap.org/soap/encoding/”>
  6. <SOAP-ENV:Header>
  7. <TransactionID xmlns=“http://10.1.2.122/misc/dsmp.xsd”>
  8. 00110100037392</TransactionID>
  9. </SOAP-ENV:Header>
  10. <SOAP-ENV:Body>
  11. <SyncOrderRelationReq xmlns=“http://10.1.2.122/misc/dsmp.xsd”>
  12. <Version>1.5.0</Version>
  13. <MsgType>SyncOrderRelationReq</MsgType>
  14. <Send_Address>
  15. <DeviceType>0</DeviceType>
  16. <DeviceID>0011</DeviceID>
  17. </Send_Address>
  18. <Dest_Address>
  19. <DeviceType>400</DeviceType>
  20. <DeviceID>0</DeviceID>
  21. </Dest_Address>
  22. <FeeUser_ID>
  23. <UserIDType>2</UserIDType>
  24. <MSISDN />
  25. <PseudoCode>00116000000286</PseudoCode>
  26. </FeeUser_ID>
  27. <DestUser_ID>
  28. <UserIDType>2</UserIDType>
  29. <MSISDN />
  30. <PseudoCode>00116000000286</PseudoCode>
  31. </DestUser_ID>
  32. <LinkID>SP</LinkID>
  33. <ActionID>1</ActionID>
  34. <ActionReasonID>1</ActionReasonID>
  35. <SPID>919102</SPID>
  36. <SPServiceID>0000000064</SPServiceID>
  37. <AccessMode>2</AccessMode>
  38. <FeatureStr />
  39. </SyncOrderRelationReq>
  40. </SOAP-ENV:Body>
  41. </SOAP-ENV:Envelope>
<?xml version="1.0" encoding="utf-8" ?>
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                   xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                   xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Header>
    <TransactionID xmlns="http://10.1.2.122/misc/dsmp.xsd">
00110100037392</TransactionID>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <SyncOrderRelationReq xmlns="http://10.1.2.122/misc/dsmp.xsd">
      <Version>1.5.0</Version>
      <MsgType>SyncOrderRelationReq</MsgType>
      <Send_Address>
        <DeviceType>0</DeviceType>
        <DeviceID>0011</DeviceID>
      </Send_Address>
      <Dest_Address>
        <DeviceType>400</DeviceType>
        <DeviceID>0</DeviceID>
      </Dest_Address>
      <FeeUser_ID>
        <UserIDType>2</UserIDType>
        <MSISDN />
        <PseudoCode>00116000000286</PseudoCode>
      </FeeUser_ID>
      <DestUser_ID>
        <UserIDType>2</UserIDType>
        <MSISDN />
        <PseudoCode>00116000000286</PseudoCode>
      </DestUser_ID>
      <LinkID>SP</LinkID>
      <ActionID>1</ActionID>
      <ActionReasonID>1</ActionReasonID>
      <SPID>919102</SPID>
      <SPServiceID>0000000064</SPServiceID>
      <AccessMode>2</AccessMode>
      <FeatureStr />
    </SyncOrderRelationReq>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

MISC1.6的业务订购关系同步的响应包:

Java代码 复制代码
  1. <?xml version=“1.0″ encoding=“utf-8″?>
  2. <SOAP-ENV:Envelope xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  3. xmlns:xsd=“http://www.w3.org/2001/XMLSchema”
  4. xmlns:SOAP-ENV=“http://schemas.xmlsoap.org/soap/envelope/”
  5. xmlns:SOAP-ENC=“http://schemas.xmlsoap.org/soap/encoding/”>
  6. <SOAP-ENV:Header>
  7. <TransactionID xmlns=“http://www.monternet.com/dsmp/schemas/”>
  8. 00110100037392</TransactionID>
  9. </SOAP-ENV:Header>
  10. <SOAP-ENV:Body>
  11. <SyncOrderRelationResp xmlns=“http://www.monternet.com/dsmp/schemas/”>
  12. <Version>1.5.0</Version>
  13. <MsgType>SyncOrderRelationResp</MsgType>
  14. <hRet>0</hRet>
  15. </SyncOrderRelationResp>
  16. </SOAP-ENV:Body>
  17. </SOAP-ENV:Envelope>
<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema"
         xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
         xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Header>
    <TransactionID xmlns="http://www.monternet.com/dsmp/schemas/">
00110100037392</TransactionID>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <SyncOrderRelationResp xmlns="http://www.monternet.com/dsmp/schemas/">
      <Version>1.5.0</Version>
      <MsgType>SyncOrderRelationResp</MsgType>
      <hRet>0</hRet>
    </SyncOrderRelationResp>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

实战SOAPI

现在做SOAP开发一般有三种方式选择

* PEAR自带的soap扩展,
* PHP自带的SOAP扩展,
* NuSOAP(纯PHP,似乎已经过时)

注:还有WSO2.org(关于WSO2.org可以考虑尝试一下)

PHP 5 中新增了内置的 SOAP 扩展,我们称之为
ext/soap。它是作为 PHP 的一部分提供的,因此不需要下载、安装和管理单独的包。这是第一个用 C 而不是 PHP 为 PHP 编写的 SOAP
实现,因此作者声称它的速度要快得多。

因为新的扩展是 PHP 的完整组成部分之一,相关文档包含在 PHP 手册的 Function
Reference 部分(php_soap.dll)。

SOAP 参考是以一个重要的免责声明开始的:

警告:该扩展是试验性的(EXPERIMENTAL)。本扩展的行为,包括关于本扩展的函数名和其他内容,在以后的 PHP
版本中随时可能改变,不另行通知。使用该扩展的风险自负。

警告看起来有点让人担心,但实际上这个扩展似乎得到了很好的支持。和任何新代码一样,该扩展也存在缺陷,但是报告的问题通常很快就能得到修正。在 PHP
站点上可以看到缺陷列表。我们估计,在将来的 PHP 版本中,该扩展将从试验性功能转为主流功能

一个访问.NET
WEB服务的客户端例子

Java代码 复制代码
  1. <?php
  2. $objSoapClient = new SoapClient(“http://www.webservicemart.com/uszip.asmx?WSDL”);
  3. $param=array(“ZipCode”=>’12209′);
  4. $out=$objSoapClient->ValidateZip($param);
  5. $data=$out->ValidateZipResult;
  6. echo $data;
  7. ?>
<?php

$objSoapClient = new SoapClient("http://www.webservicemart.com/uszip.asmx?WSDL");

$param=array("ZipCode"=>'12209');
$out=$objSoapClient->ValidateZip($param);
$data=$out->ValidateZipResult;
echo $data;
?>

运行后输出

Java代码 复制代码
  1. <result code=“200″><item zip=“12209″ state=“NY”
  2. latitude =“42.64081″ longitude =“-73.7856″/></result>
<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包是什么样的。
概述(SOAP和XML-PRC比较)  - lxc521wjh@126 - 我的flex网上学习家园

实战SOAPII
用PHP建立SOAP服务

建立soap_server.php(虚拟路径为:http://172.16.0.24/php/soap/soap_server.php)

Java代码 复制代码
  1. <?php
  2. /**
  3. * A simple math utility class
  4. * @author John Coggeshall john@zend.com
  5. */
  6. class math {
  7. /**
  8. * Add two integers together
  9. *
  10. * @param integer $a The first integer of the addition
  11. * @param integer $b The second integer of the addition
  12. * @return integer The sum of the provided integers
  13. */
  14. public function add($a, $b) {
  15. return $a + $b;
  16. }
  17. /**
  18. * Subtract two integers from each other
  19. *
  20. * @param integer $a The first integer of the subtraction
  21. * @param integer $b The second integer of the subtraction
  22. * @return integer The difference of the provided integers
  23. */
  24. public function sub($a, $b) {
  25. return $a - $b;
  26. }
  27. /**
  28. * Div two integers from each other
  29. *
  30. * @param integer $a The first integer of the subtraction
  31. * @param integer $b The second integer of the subtraction
  32. * @return double The difference of the provided integers
  33. */
  34. public function div($a, $b) {
  35. if($b == 0) {
  36. throw new SoapFault(-1, “Cannot divide by zero!”);
  37. }
  38. return $a / $b;
  39. }
  40. }
  41. $server = new SoapServer(‘math.wsdl’, array(‘soap_version’ => SOAP_1_2));
  42. $server->setClass(“math”);
  43. $server->handle();
  44. ?>
<?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();

?>

注意几点:

     
  1. math类是即将公开的webservice.
  2. 注$server→setClass,不是$server→addClass

用PHP客户端访问刚建立SOAP服务

Java代码 复制代码
  1. <?php
  2. //$client = new SoapClient(‘http://localhost/php/soap/math.wsdl’);
  3. $client = new SoapClient(“http://localhost/php/soap/soap_server.php?WSDL”);
  4. try {
  5. $result = $client->div(8, 2); // will cause a Soap Fault if divide by zero
  6. print “The answer is: $result”;
  7. } catch(SoapFault $e) {
  8. print “Sorry an error was caught executing your request: {$e->getMessage()}”;
  9. }
  10. ?>
<?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()}";
  }
?>

本质上,http://localhost/php/soap/soap_server.php?WSDL就是要访问到注释行所指的 wsdl描述文件,所以这个WSDL文件必须事先生成。而对于其他语言如Java则可以动态生成。我目前的学习发现对于php自带的SOAP扩展要求这个 WSDL文件必须事先生成好。

可以用ZendStudio生成静态的WSDL文件,此时用到math类的phpdoc作为生成WSDL的元数据。
用ZendStudio生成wsdl文件时,必须正确说明web服务目标地址,片断如下:

Java代码 复制代码
  1. <service name=“mathService”>
  2. <port name=“mathPort” binding=“typens:mathBinding”>
  3. <soap:address location=“http://localhost/php/soap/soap_server.php”/>
  4. </port>
  5. </service>
  6. ….
原文地址:https://www.cnblogs.com/lexus/p/2329221.html