php开发webservice服务端接口(wsdl)

时间:2024-03-02 21:46:51

php开发webservice服务端接口(wsdl)

 首先要创建两个文件Test.php  Test.wsdl

Test.php文件代码是处理具体业务的接口代码

Test.php代码:

<?php
ini_set("soap.wsdl_cache_enabled", "0");
class Test
{

function getData($Content)//接口参数,可有可无,看具体需求
{
    $res="<xml><status>1</status>
<msg>操作成功</msg></xml>";
return $res;
    }
    function addData($Content)//接口参数,可有可无,看具体需求
{
    $res="<xml><status>1</status>
<msg>操作成功</msg></xml>";
    return $res;

}
}
$service = new SoapServer(\'http://127.0.0.1/webservice/Test.wsdl\', array(\'soap_version\' => SOAP_1_2));
$service->setClass("Test"); //! 注册Service类的所有方法
$service->handle(); //! 处理请求*/

?>
Test.wsdl文件代码:

<?xml version =\'1.0\' encoding =\'UTF-8\' ?>
<definitions name=\'Test\' targetNamespace=\'http://127.0.0.1/webservice\'
xmlns:tns=\'http://127.0.0.1/webservice\' 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="http://127.0.0.1/Test">
</xsd:schema>
</types>

<!--
<message> 元素可定义每个消息的部件,以及相关联的数据类型。
请求-响应是最普通的操作类型,不过 WSDL 定义了四种类型:
One-way 此操作可接受消息,但不会返回响应。
Request-response 此操作可接受一个请求并会返回一个响应。(常用)
Solicit-response 此操作可发送一个请求,并会等待一个响应。
Notification 此操作可发送一条消息,但不会等待响应。
-->
<message name=\'getDate\'>
<part name="Content" type="xsd:string"/>
</message>
   <message name=\'addDate\'>
<part name="Content" type="xsd:string"/>
</message>
    <message name=\'getDataResponse\'>
<part name="getDataRespose" type="xsd:string"/>
</message>
 <message name=\'addDataResponse\'>
<part name="addDataRespose" type="xsd:string"/>
</message>
    <!--
<portType> 元素是最重要的 WSDL 元素。它可描述一个 web service、可被执行的操作,以及相关的消息。
它告诉你去哪个WebService的连接点,扮演了一个控制者。
-->
<portType name=\'Test\'>
<operation name=\'getData\'>
<input message=\'tns:getDataRequest\'/>
<output message=\'tns:getDataResponse\'/>
</operation>
 <operation name=\'addData\'>
<input message=\'tns:addDataRequest\'/>
<output message=\'tns:addDataResponse\'/>
</operation>

</portType>

<!--<binding> 元素为每个端口定义消息格式和协议细节-->
<binding name=\'Test\' type=\'Test\'>
<!--style:属性可取值 "rpc" 或 "document",ransport:属性定义了要使用的 SOAP 协议。在这个例子中我们使用 HTTP-->
<soap:binding style=\'rpc\' transport=\'http://schemas.xmlsoap.org/soap/http\'/>
<!--operation 元素定义了每个端口提供的操作符,对于每个操作,相应的 SOAP 行为都需要被定义-->
<operation name=\'payInfor\'>
<soap:operation soapAction=\'http://www.example.org/Test/getData\'/>
<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=\'Debit\'>
<port name=\'Debit\' binding=\'tns:Test\'>
<soap:address location=\'http://127.0.0.1/webservice/Test.php\'/>
</port>
</service>
</definitions>