背景:在最近的开发中,为了解决公司内部系统与外部系统的对接,开始接触到了webservice接口,外部公司提供接口供咱们调用,已达到数据同步的目的,所以有必要普及一下web service的知识了!php
什么是web service:web service是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,可以使用开放的XML(标准通用标记御园下的一个子集)标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式的互操做的应用程序。web
webservice三要素:SOAP、WSDL(WebServicesDescriptionLanguage)、UDDI(UniversalDescriptionDiscovery andIntegration)之一, soap用来描述传递信息的格式, WSDL 用来描述如何访问具体的接口, uddi用来管理,分发,查询webService 。具体实现能够搜索 Web Services简单实例 ; SOAP 能够和现存的许多因特网协议和格式结合使用,包括超文本传输协议(HTTP),简单邮件传输协议(SMTP),多用途网际邮件扩充协议(MIME)。它还支持从消息系统到远程过程调用(RPC)等大量的应用程序。SOAP使用基于XML的数据结构和超文本传输协议(HTTP)的组合定义了一个标准的方法来使用Internet上各类不一样操做环境中的分布式对象。编程
什么是SOAP:SOAP 是基于 XML 的简易协议,可以使应用程序在 HTTP 之上进行信息交换。浏览器
HTTP与SOAP:网络
什么是WSDL:WSDL(网络服务描述语言,Web Services Description Language)是一门基于 XML 的语言,用于描述 Web Services 以及如何对它们进行访问。数据结构
什么是UDDI:UDDI 是一种目录服务,企业可使用它对 Web services 进行注册和搜索。tcp
UDDI基于什么:分布式
PHP建立webservice:工具
前提:环境要确保PHP支持SOAP;测试
建立一个.wsdl文件(方式:一、使用zend studio工具直接生成;二、使用SoapDiscovery.class.php自动生成wsdl文件)
<?php /** * Created by PhpStorm. * User: 黎志明 * Date: 2018/6/19 * Time: 11:40 */ class Person { public function say() { return "我在说话。"; } public function run() { return "我在跑步"; } }
<?php /** * Created by PhpStorm. * User: 黎志明 * Date: 2018/6/19 * Time: 11:41 */ include("Person.class.php"); include("SoapDiscovery.class.php"); //第一个参数是类名(生成的wsdl文件就是以它来命名的),即person类,第二个参数是服务的名字(这个能够随便写)。 $disco = new SoapDiscovery('Person', 'Person'); $disco->getWSDL();
<?xml version="1.0" ?> <definitions name="Person" targetNamespace="urn:Person" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="urn:Person" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns="http://schemas.xmlsoap.org/wsdl/"> <types xmlns="http://schemas.xmlsoap.org/wsdl/"/> <portType name="PersonPort"> <operation name="say"> <input message="tns:sayRequest"/> <output message="tns:sayResponse"/> </operation> <operation name="run"> <input message="tns:runRequest"/> <output message="tns:runResponse"/> </operation> </portType> <binding name="PersonBinding" type="tns:PersonPort"> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="say"> <soap:operation soapAction="urn:Person#Person#say"/> <input> <soap:body use="encoded" namespace="urn:Person" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </input> <output> <soap:body use="encoded" namespace="urn:Person" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </output> </operation> <operation name="run"> <soap:operation soapAction="urn:Person#Person#run"/> <input> <soap:body use="encoded" namespace="urn:Person" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </input> <output> <soap:body use="encoded" namespace="urn:Person" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </output> </operation> </binding> <service name="Person"> <documentation/> <port name="PersonPort" binding="tns:PersonBinding"> <soap:address location="http://localhost:80/wsdl/Service.php"/> </port> </service> <message name="sayRequest"> </message> <message name="sayResponse"> <part name="say" type="xsd:string"/> </message> <message name="runRequest"> </message> <message name="runResponse"> <part name="run" type="xsd:string"/> </message> </definitions>
<?php /** * Created by PhpStorm. * User: 黎志明 * Date: 2018/6/19 * Time: 11:41 */ include("Person.class.php"); $objSoapServer = new SoapServer("Person.wsdl");//person.wsdl是刚建立的wsdl文件 //$objSoapServer = new SoapServer("server.php?wsdl");//这样也行 $objSoapServer->setClass("Person");//注册person类的全部方法 $objSoapServer->handle();//处理请求
<?php /** * Created by PhpStorm. * User: 黎志明 * Date: 2018/6/19 * Time: 11:59 */ $client = new SoapClient("Person.wsdl"); //$client = new SoapClient("server.php?wsdl");//这样也行 echo $client->say(); echo "<br />"; echo $client->run(); echo "<br />";
小结:.NET若是要使用的话,只要提供一个url给他就好了;得到url的方法:你能够先到Person.wsdl文件里面查找<soap:address location="http://localhost:80/wsdl/Service.php" />,这里的url(具体url是根据你的目录肯定的)就是你要提供给.NET开发人员使用的;不过别高兴太早,后面要加:“?wsdl”,http://localhost:80/wsdl/Service.php?wsdl这样才是对的,不信你能够将url拷贝到浏览器的地址栏里看下就知道了,.NET开发人员得到你给他的url以后,就能够在本身的项目里面添加一个服务引用或者web引用了,而后就能够根据提示完成相关操做,对于使用.NET的开发人员来讲很简单的。