• WebService-php- 1(16)


    最近看了挺多关于php中webservice的资料,感谢燕十八的分享,帮助了我构建服务端的过程。将学习笔记记录如下,其中包含燕十八的笔记。

    WebService

    1 快速了解WebService

    通俗的说:按一定的XML格式,调用远程服务器的方法,且服务器按一定的格式返回XML内容.
    "一定的格式"----SOAP(Simple Object Access Protocol )简单对象访问协议是在分散或分布式的环境中交换信息的简单的协议,是一个基于XML的协
    议.
    远程服务器 ---- 一般通过HTTP协议来传递消息
    总结: WebServie == HTTP协议 + Soap格式的XML

    例1:soap请求

      POST /WebServices/MobileCodeWS.asmx HTTP/1.1
      Host: webservice.webxml.com.cn
      Content-Type: text/xml; charset=utf-8
      Content-Length: 354
      SOAPAction: "http://WebXml.com.cn/getMobileCodeInfo"
      <?xml version="1.0" encoding="utf-8"?>
      <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"                  xmlns:soap="http://WebXml.com.cn/getMobileCodeInfo >

    2 PHP客户端请求WebService

      修改PHP.ini
      extension=php_soap.dll 前的";"去掉.
      并重启apache
      PHP SoapClient类可以用来请求WebService

    $soap = new soapClient('http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL');
    print_r($soap->getMobileCodeInfo( array('mobileCode'=>'13**********') ) );
    Array
    (
    [0] => getMobileCodeInfoResponse getMobileCodeInfo(getMobileCodeInfo $parameters)
    [1] => getDatabaseInfoResponse getDatabaseInfo(getDatabaseInfo $parameters)
    )
    Array
    (
    [0] => struct getMobileCodeInfo {
    string mobileCode;
    string userID;
    }
    [1] => struct getMobileCodeInfoResponse {
    string getMobileCodeInfoResult;
    }
    [2] => struct getDatabaseInfo {
    }
    [3] => struct getDatabaseInfoResponse {
    ArrayOfString getDatabaseInfoResult;
    }
    [4] => struct ArrayOfString {

    string string;
    }

    // 调用方法
    print_r($soap->getMobileCodeInfo( array('mobileCode'=>'13426060134') ) );

    返回结果

    stdClass Object ( [getMobileCodeInfoResult] => 13*********:北京 北京 北京移动动感地带卡 )

    3 搭建WebService服务器

    wsdl是什么?
    wsdl是WebService的规格说明书.

    <?xml version ='1.0' encoding ='UTF-8' ?>
    <definitions name='自定义名称[可选]'
    targetNamespace='命名空间[一般为URL]'
    xmlns:tns='命名空间[值同targetNamespace]'
    xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
    xmlns:xsd='http://www.w3.org/2001/XMLSchema'
    xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'
    xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
    xmlns='http://schemas.xmlsoap.org/wsdl/'>
    <!--<types> 元素定义 web service 使用的数据类型,WSDL 使用 XML Schema 语法来定义数据类型,也可以自定义Schema不包含的类型-->
    <types>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    targetNamespace="[值同上tns]">
    </xsd:schema>
    </types>
    <!--
    <message> 元素可定义每个消息的部件,以及相关联的数据类型.
    -->
    <message name='操作名Request'>
    <part name="term" type="xsd:string"/>
    </message>
    <message name='操作名Response'>
    <part name="value" type="xsd:string"/>
    </message>
    <!--
    <portType> 元素是最重要的 WSDL 元素.它可描述一个 web service、可被执行的操作,以及相关的消息.
    它告诉你去哪个WebService的连接点,扮演了一个控制者.
    -->
    <portType name='操作列表名'>
    <operation name='操作名'>
    <input message='tns:操作名Request'/>
    <output message='tns:操作名Response'/>
    </operation>
    </portType>
    <!--<binding> 元素为每个端口定义消息格式和协议细节-->
    <binding name='WS下的频道名称' type='tns:频道下的操作列表'>
    <!--style:属性可取值 "rpc""document",ransport:属性定义了要使用的 SOAP 协议.在这个例子中我们使用 HTTP-->
    <soap:binding style='rpc'
    transport='http://schemas.xmlsoap.org/soap/http'/>
    <!--operation 元素定义了每个端口提供的操作符,对于每个操作,相应的 SOAP 行为都需要被定义-->
    <operation name='test'>
    <soap:operation soapAction='http://www.cwtservice.cn/newOperation/'/>
    <input>
    <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
    encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
    </input>
    <output>
    <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
    encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
    </output>
    </operation>
    </binding>
    <!--<service>包含一个或者多个port元素,每个port元素表示一个不同的Web服务-->
    <service name='WebService名称[如weatherWS,shopWS]'>
    <port name='WS下的频道名称[如cartSoap,购物车服务]' binding='tns:[频道名,同左]'>
    <soap:address location='http://[webservice地址]'/>
    </port>
    </service>
    </definitions>
  • 相关阅读:
    Beats数据采集---PacketbeatFilebeatTopbeatWinlogBeat使用指南
    《书读完了》—— 随笔
    《历史的天空》—— 读后总结
    Hadoop HDFS 用户指南
    单节点部署Hadoop教程
    [收藏]IntelliJ Idea快捷键
    《鬼谷子的局5》—— 读后总结
    Logstash为什么那么慢?—— json序列化
    《一线架构师实践指南》—— 读后总结
    Oracle Redo 以及 Archived日志简述
  • 原文地址:https://www.cnblogs.com/zhenghongxin/p/4468234.html
Copyright © 2020-2023  润新知