php开发webservice服务端接口(wsdl)
php开发webservice服务端接口(wsdl)
首先要创建两个文件Test.php Test.wsdl
Test.php文件代码是处理具体业务的接口代码
Test.php代码:
1 <?php 2 ini_set("soap.wsdl_cache_enabled", "0"); 3 class Test 4 { 5 6 function getData($Content)//接口参数,可有可无,看具体需求 7 { 8 $res="<xml><status>1</status> 9 <msg>操作成功</msg></xml>"; 10 return $res; 11 } 12 function addData($Content)//接口参数,可有可无,看具体需求 13 { 14 $res="<xml><status>1</status> 15 <msg>操作成功</msg></xml>"; 16 return $res; 17 18 } 19 } 20 $service = new SoapServer('http://127.0.0.1/webservice/Test.wsdl', array('soap_version' => SOAP_1_2)); 21 $service->setClass("Test"); //! 注册Service类的所有方法 22 $service->handle(); //! 处理请求*/ 23 24 ?>
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>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端