设为首页 收藏本站
查看: 801|回复: 0

[经验分享] PHP 中使用 SOAP(2)

[复制链接]

尚未签到

发表于 2017-3-24 10:10:33 | 显示全部楼层 |阅读模式
PHP 中还实现了通过 WSDL 对 Web 服务的发布。

    WSDL 是一种用于描述Web服务的语法规范,针对每个Web服务来说,它是一个说明文档,对web服务的位置,协议和接口进行详细的说明.由web服务的开发者提供。

    WSDL文件包括5部分:types, Message,PortType,Binding和Service五部分.

    1 Types定义: 类型定义,独立于语言.对应于SOAP消息中要传输的元素信息的定义
    2 Message: 每个web方法对应两个message定义in和out.而message的定义包含了头和体
    3 PortType: 每个web service对应一个PortType,该PortType中又包含了对其发布的方法, operation(操作)
    4 Bindings: 指定每porttype中每个操作(类以及方法)的绑定信息,包含input和output的消息的格式.
    5 Service: 每个web service绑定的port信息

    Web 服务除过按照前述的示例形式发布外,还可以通过 WSDL 文档来发布。

    示例:

    要发布的类,文件 myservice.php:
    <?php
      class service
      {
        public function HelloWorld()
        {
          return "Hello";
        }
        public function Add($a,$b)
        {
          return $a+$b;
        }
      }

      $server=new SoapServer('TestSoap.wsdl',array('soap_version' => SOAP_1_2));
      $server->setClass("service");
      $server->handle();
    ?>

    WSDL 描述文档,文件 TestSoap.wsdl:

    <?xml version='1.0' encoding='UTF-8'?>
    <definitions name="TestSoap"
      targetNamespace="urn:TestSoap"
      xmlns:typens="urn:TestSoap"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
      xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
      xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
      xmlns="http://schemas.xmlsoap.org/wsdl/">

      <message name="Add">
        <part name="a"/>
        <part name="b"/>
      </message>

      <message name="AddResponse">
        <part name="AddReturn"/>
      </message>

      <message name="HelloWorld"/>
      <message name="HelloWorldResponse">

        <part name="HelloWorldReturn"/>
      </message>

      <message name="Sub">
        <part name="a"/>
      </message>

      <message name="SubResponse">
        <part name="SubReturn"/>
      </message>

      <portType name="servicePortType">
        <operation name="Add">
          <input message="typens:Add"/>
          <output message="typens:AddResponse"/>
        </operation>
        <operation name="HelloWorld">
          <input message="typens:HelloWorld"/>
          <output message="typens:HelloWorldResponse"/>
        </operation>
        <operation name="Sub">
          <input message="typens:Sub"/>
          <output message="typens:SubResponse"/>
        </operation>
      </portType>

      <binding name="serviceBinding" type="typens:servicePortType">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="Add">
          <soap:operation soapAction="urn:serviceAction"/>
          <input>
            <soap:body namespace="urn:TestSoap" use="literal"/>
          </input>
          <output>
            <soap:body namespace="urn:TestSoap" use="literal"/>
          </output>
        </operation>
        <operation name="HelloWorld">
          <soap:operation soapAction="urn:serviceAction"/>
          <input>
            <soap:body namespace="urn:TestSoap" use="literal"/>
          </input>
          <output>
            <soap:body namespace="urn:TestSoap" use="literal"/>
          </output>
        </operation>
        <operation name="Sub">
          <soap:operation soapAction="urn:serviceAction"/>
          <input>
            <soap:body namespace="urn:TestSoap" use="literal"/>
          </input>
          <output>
            <soap:body namespace="urn:TestSoap" use="literal"/>
          </output>
        </operation>
      </binding>

      <service name="TestSoapService">
        <port name="servicePort" binding="typens:serviceBinding">
          <soap:address location="http://localhost:8080/_myPHP5/soap/Wsdl/myservice.php"/>
        </port>
      </service>

    </definitions>

    调用代码,文件 Client.php:

    <?php
      error_reporting(7);

      $client = new SoapClient("http://localhost:8080/_myPHP5/soap/Wsdl/TestSoap.wsdl");
      echo $client->HelloWorld();
      echo("<br>");
      echo $client->Add(10, 20);
    ?>

    然而,WSDL 文档的编写是一件很麻烦的事情,无聊又容易出错。很多人认为那玩意儿不是人写的,但是,如果有好的软件工具,那玩意儿又是不需要人写的。Zend 公司的 ZED 5.0 系列和 Zend Studio for eclipse 6.0 原来是很好支持 WDSL 的可视化编辑和类的发布的(按照一个类文件智能生成),但 Zend studio 7.0 之后,这方面功能有所减弱。但基于 Eclipse 构建的 Zend studio 7.x,还是有一个 WSDL 的可视化编辑器,功能也还够用,生成的 WSDL 文件与以前有细微的变化。需要程序员必须对 WSDL 文档里的标签和元素很熟悉。

    附录:关于 PHP 开发 Soap 的一些错误

    1、开发的时候一定要关闭 php soap 的缓存,服务器和客户端都需要,不然会报:

    Fatal error: Uncaught SoapFault exception: [Client] Function (”test”) is not a valid method for this service in ……\clien.php:5 Stack trace:
    #0 [internal function]: SoapClient->__call(’test’, Array)
    #1 D:\xampp\htdocs\clien.php(5): SoapClient->test()
    #2 {main}

    关闭方法:
    ini_set("soap.wsdl_cache_enabled", "0");

    可以通过类似 $client->__getFunctions() 等方法查看 Soap 的一些信息。


    2、如果调试时报告不识别xml错误,请确保代码里没有空格等无关信息,比如 Utf-8 编码文件的 BOM 头。

    作者:张庆(网眼) 西安 PHP 教育培训中心 2010-7-11
    来自“网眼视界”:http://blog.why100000.com
    作者微博:http://t.qq.com/zhangking
    “十万个为什么”电脑学习网:http://www.why100000.com

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-354474-1-1.html 上篇帖子: JSON jquery 与php 入门 下篇帖子: PHP框架中的SPB分离
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表