SOAP在PHP中的使用

来源:互联网 发布:怎么咨询网络服务商 编辑:程序博客网 时间:2024/05/22 14:55

SOAP(Simple Object Access Protocol )简单对象访问协议是在分散或分布式的环境中交换信息的简单的协议,是一个基于XML的协议,它包括四个部分:SOAP封装(envelop),封装定义了一个描述消息中的内容是什么,是谁发送的,谁应当接受并处理它以及如何处理它们的框架;SOAP编码规则(encoding rules),用于表示应用程序需要使用的数据类型的实例; SOAP RPC表示(RPC representation),表示远程过程调用和应答的协定;SOAP绑定(binding),使用底层协议交换信息。

WSDL(Web Service Description Language)就是描述XML Web服务的标准XML格式,WSDL由Ariba、Intel、IBM和微软等开发商提出。它用一种和具体语言无关的抽象方式定义了给定Web服务收发的有关操作和消息。就其定义来说,你还不能把WSDL当作一种对象接口定义语言,例如,CORBA或COM等应用程序体系结构就会用到对象接口定义语言。 WSDL保持协议中立,但它确实内建了绑定SOAP的支持,从而同SOAP建立了不可分割的联系。所以,当我在这篇文章中讨论WSDL的时候,我会假定你把SOAP作为了你的通讯协议。

SOAP和WSDL虽然是web service的两大标准,但是两者并没有必然的联系,都可以独立使用。它们之间的关系就类似HTTP和Html之间的关系。前者是一种协议,后者是对一个Web Server的描述。


一 客户端

通过调用SoaPClient对象可以获取到被调用的对象

header("content-type:text/html;charset=utf-8");try {    $client = new \SoapClient("http://***.******.com/services/AcceptedBusiness?wsdl");    print_r($client->__getFunctions());    print_r($client->__getTypes());  } catch (SOAPFault $e) {    print $e;}
$return = $client->insertProduct(...);

二 服务器端

服务器端实际上就是生成xml文件,其中有可调用的方法以及参数.

以Zend为例

$autodiscover = new \Zend\Soap\AutoDiscover();$soapClass = $this->getServiceLocator()->get($soapClassName);$autodiscover->setClass($soapClass)->setUri($url)->setComplexTypeStrategy(new OptionalArrayTypeSequence());header('Content-type: text/xml');echo $autodiscover->toXml();
通过此步生成xml文件

其中SoapClassName为一个类,定义了多个方法

class Product{    public function insertProduct($products)    {        ......    }}
如此客户端可以直接调用方法 insertProduct

生成的XML类似:

<?xml version="1.0" encoding="UTF-8"?><wsdl:definitions xmlns:typens="urn:test"             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/"             name="test"             targetNamespace="urn:test">    <wsdl:types>        <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:test">            <xsd:complexType name="productInfoEntity">                <xsd:sequence>                    <xsd:element name="sku" type="xsd:string" />                    <xsd:element name="title" type="xsd:string" />                    <xsd:element name="price" type="xsd:string" />                    <xsd:element name="size" type="xsd:string" />                    <xsd:element name="description" type="xsd:string" />                </xsd:sequence>            </xsd:complexType>            <xsd:complexType name="productsEntity">                <xsd:sequence>                    <xsd:element minOccurs="0" maxOccurs="unbounded" name="complexObjectArray" type="typens:productInfoEntity"/>                </xsd:sequence>            </xsd:complexType>            <xsd:complexType name="productExportEntity">                <xsd:sequence>                    <xsd:element name="result" type="xsd:string" />                </xsd:sequence>            </xsd:complexType>            <xsd:element name="productRequestParam">                <xsd:complexType>                    <xsd:sequence>                        <xsd:element minOccurs="1" maxOccurs="1" name="sessionId" type="xsd:string" />                        <xsd:element minOccurs="1" maxOccurs="1" name="content" type="typens:productsEntity" />                    </xsd:sequence>                </xsd:complexType>            </xsd:element>            <xsd:element name="productResponseParam">                <xsd:complexType>                    <xsd:sequence>                        <xsd:element minOccurs="1" maxOccurs="1" name="result" type="typens:productExportEntity" />                    </xsd:sequence>                </xsd:complexType>            </xsd:element>        </xsd:schema>    </wsdl:types>    <wsdl:message name="productRequest">        <wsdl:part name="parameters" element="typens:productRequestParam" />    </wsdl:message>    <wsdl:message name="productResponse">        <wsdl:part name="parameters" element="typens:productResponseParam" />    </wsdl:message>    <wsdl:portType name="productPortType">        <wsdl:operation name="product">            <wsdl:documentation>Update product</wsdl:documentation>            <wsdl:input message="typens:productRequest" />            <wsdl:output message="typens:productResponse" />        </wsdl:operation>    </wsdl:portType>    <wsdl:binding name="productBinding" type="typens:productPortType">        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />        <wsdl:operation name="product">            <soap:operation soapAction="" />            <wsdl:input>                <soap:body use="literal" />            </wsdl:input>            <wsdl:output>                <soap:body use="literal" />            </wsdl:output>        </wsdl:operation>    </wsdl:binding>    <wsdl:service name="testService">        <wsdl:port name="productPort" binding="typens:productBinding">            <soap:address location="http://...." />        </wsdl:port>    </wsdl:service></wsdl:definitions>