Maison >développement back-end >tutoriel php >一个php创建webservice,并通过c#调用的真实实例
最近需要用php创建webservice供C#和JAVA来调用,通过3天的搜索和尝试,终于成功在C#下调用,JAVA的调用还没开始,为防止忘记,在这里记录下来全过程。
本文参考了许多文章,文中也采用了其中的示例。
本文目录如下
一、php创建webservice
二、生成.wsdl文件
三、c#调用php的webservice
一、php创建webservice
1、php环境
我用的是windows的apache,php.ini文件中 extension=php_soap.dll 去掉注释即可,前提是已经安装了 php_soap.dll。
环境测试,在cmd窗口中输入如下命令来检测soap是否正常
c:\>php --ini ;用来查看ini文件中的soap模块是否正常,它和phpinfo()看到的未必一致
c:\>php -i |findstr "soap" ;用来看soap模块是否正常
c:\>php -r "new SoapClient('http://localhost/wsdl/person.wsdl');" ;用来直接运行soap
2、php服务端文件(TestWebService.php)
<span>php </span><span>class</span><span> TestWebService { </span><span>public</span><span>function</span><span> HelloWorld() { </span><span>return</span><span>array</span>("HelloWorldResult"=>"welcome to dongzi world"<span>); } </span><span>public</span><span>function</span> GetArray(<span>$args</span><span>) { </span><span>/*</span><span> 注意,Web Service的方法在声明时至多一个参数, 可是在调用该方法时就必须传value1,value2两个参数。 (这一点十分令人费解,我的理解是,在调用该方法时,系统把所有参数都放到一个<strong>对象</strong>里传过来的) </span><span>*/</span><span>$value1</span> = <span>$args</span>-><span>value1; </span><span>$value2</span> = <span>$args</span>->value2;<span>//</span><span>这两句是获取真正的参数 </span><span>$arry</span> = <span>array</span>(<span>$value1</span>,<span>$value2</span><span>); </span><span>//</span><span>返回值也很特别,不是直接返回$arry,而是把它放到一个<strong>对象</strong>里再返回。 </span><span>return</span><span>array</span>("GetArrayResult"=><span>$arry</span><span>); } } </span><span>//</span><span>创建WebSevice实例 </span><span>$server</span> = <span>new</span> SoapServer("TestWebService.wsdl"<span>); </span><span>//</span><span>指定类名 </span><span>$server</span>->setClass("TestWebService"<span>); </span><span>$server</span>-><span>handle(); </span>?>View Code
二、生成.wsdl文件
本来我想简单一点,用SoapDiscovery.class.php来生成,可是总是出现各种奇葩问题;迫不得已安装了Zend Studio12.5,可是还是出现另外的奇葩问题;最终我找到一个可行的方案,是用vs2010来生成,C#调用没有问题。
1、打开vs2010,新建项目-ASP.NET空Web应用程序;
2、解决方案资源管理器-项目(右键)-添加-新建项-web服务,名称修改为TestWebService.asmx,代码如下:
<span>using</span><span> System; </span><span>using</span><span> System.Collections.Generic; </span><span>using</span><span> System.Linq; </span><span>using</span><span> System.Web; </span><span>using</span><span> System.Web.Services; </span><span>namespace</span><span> Web<strong>Application</strong>1 { </span><span>///</span><span><summary></summary></span><span>///</span><span> TestWebService 的摘要说明 </span><span>///</span><span></span> [WebService(Namespace = <span>"</span><span>http://tempuri.org/</span><span>"</span><span>)] [WebServiceBinding(ConformsTo </span>=<span> WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(</span><span>false</span><span>)] </span><span>//</span><span> 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 </span><span>//</span><span> [System.Web.Script.Services.ScriptService]</span><span>public</span><span>class</span><span> TestWebService : System.Web.Services.WebService { [WebMethod] </span><span>public</span><span>string</span><span> HelloWorld() { </span><span>return</span><span>""</span><span>; } [WebMethod] </span><span>public</span><span>string</span>[] GetArray(<span>string</span> a,<span>string</span><span> b) { </span><span>return</span><span>null</span><span>; } } }</span>View Code
HelloWorld()和GetArray()是我们希望暴露的方法,我写成空方法了。
3、F5运行,在弹出的IE浏览器中复制运行的url地址
http://localhost:63463/TestWebService.asmx
打开一个新的IE浏览器窗口,粘贴并修改为,然后回车,此时窗口会展示wsdl文件的xml格式内容:
http://localhost:63463/TestWebService.asmx?wsdl
4、点击IE浏览器的文件-另存为菜单,保存为TestWebService.wsdl文件,放到和TestWebService.php同目录下。
代码如下:
<span></span><span>xml version="1.0" encoding="utf-8"</span><span>?></span><span><span>wsdl:definitions </span><span>xmlns:soap</span><span>="http://schemas.xmlsoap.org/wsdl/soap/"</span><span> xmlns:tm</span><span>="http://microsoft.com/wsdl/mime/textMatching/"</span><span> xmlns:soapenc</span><span>="http://schemas.xmlsoap.org/soap/encoding/"</span><span> xmlns:mime</span><span>="http://schemas.xmlsoap.org/wsdl/mime/"</span><span> xmlns:tns</span><span>="http://tempuri.org/"</span><span> xmlns:s</span><span>="http://www.w3.org/2001/XMLSchema"</span><span> xmlns:soap12</span><span>="http://schemas.xmlsoap.org/wsdl/soap12/"</span><span> xmlns:http</span><span>="http://schemas.xmlsoap.org/wsdl/http/"</span><span> targetNamespace</span><span>="http://tempuri.org/"</span><span> xmlns:wsdl</span><span>="http://schemas.xmlsoap.org/wsdl/"</span><span>></span><span><span>wsdl:types</span><span>></span><span><span>s:schema </span><span>elementFormDefault</span><span>="qualified"</span><span> targetNamespace</span><span>="http://tempuri.org/"</span><span>></span><span><span>s:element </span><span>name</span><span>="HelloWorld"</span><span>></span><span><span>s:complexType </span><span>/></span><span></span><span>s:element</span><span>></span><span><span>s:element </span><span>name</span><span>="HelloWorldResponse"</span><span>></span><span><span>s:complexType</span><span>></span><span><span>s:sequence</span><span>></span><span><span>s:element </span><span>minOccurs</span><span>="0"</span><span> maxOccurs</span><span>="1"</span><span> name</span><span>="HelloWorldResult"</span><span> type</span><span>="s:string"</span><span>/></span><span></span><span>s:sequence</span><span>></span><span></span><span>s:complexType</span><span>></span><span></span><span>s:element</span><span>></span><span><span>s:element </span><span>name</span><span>="GetArray"</span><span>></span><span><span>s:complexType</span><span>></span><span><span>s:sequence</span><span>></span><span><span>s:element </span><span>minOccurs</span><span>="0"</span><span> maxOccurs</span><span>="1"</span><span> name</span><span>="a"</span><span> type</span><span>="s:string"</span><span>/></span><span><span>s:element </span><span>minOccurs</span><span>="0"</span><span> maxOccurs</span><span>="1"</span><span> name</span><span>="b"</span><span> type</span><span>="s:string"</span><span>/></span><span></span><span>s:sequence</span><span>></span><span></span><span>s:complexType</span><span>></span><span></span><span>s:element</span><span>></span><span><span>s:element </span><span>name</span><span>="GetArrayResponse"</span><span>></span><span><span>s:complexType</span><span>></span><span><span>s:sequence</span><span>></span><span><span>s:element </span><span>minOccurs</span><span>="0"</span><span> maxOccurs</span><span>="1"</span><span> name</span><span>="GetArrayResult"</span><span> type</span><span>="tns:ArrayOfString"</span><span>/></span><span></span><span>s:sequence</span><span>></span><span></span><span>s:complexType</span><span>></span><span></span><span>s:element</span><span>></span><span><span>s:complexType </span><span>name</span><span>="ArrayOfString"</span><span>></span><span><span>s:sequence</span><span>></span><span><span>s:element </span><span>minOccurs</span><span>="0"</span><span> maxOccurs</span><span>="unbounded"</span><span> name</span><span>="string"</span><span> nillable</span><span>="true"</span><span> type</span><span>="s:string"</span><span>/></span><span></span><span>s:sequence</span><span>></span><span></span><span>s:complexType</span><span>></span><span></span><span>s:schema</span><span>></span><span></span><span>wsdl:types</span><span>></span><span><span>wsdl:message </span><span>name</span><span>="HelloWorldSoapIn"</span><span>></span><span><span>wsdl:part </span><span>name</span><span>="parameters"</span><span> element</span><span>="tns:HelloWorld"</span><span>/></span><span></span><span>wsdl:message</span><span>></span><span><span>wsdl:message </span><span>name</span><span>="HelloWorldSoapOut"</span><span>></span><span><span>wsdl:part </span><span>name</span><span>="parameters"</span><span> element</span><span>="tns:HelloWorldResponse"</span><span>/></span><span></span><span>wsdl:message</span><span>></span><span><span>wsdl:message </span><span>name</span><span>="GetArraySoapIn"</span><span>></span><span><span>wsdl:part </span><span>name</span><span>="parameters"</span><span> element</span><span>="tns:GetArray"</span><span>/></span><span></span><span>wsdl:message</span><span>></span><span><span>wsdl:message </span><span>name</span><span>="GetArraySoapOut"</span><span>></span><span><span>wsdl:part </span><span>name</span><span>="parameters"</span><span> element</span><span>="tns:GetArrayResponse"</span><span>/></span><span></span><span>wsdl:message</span><span>></span><span><span>wsdl:portType </span><span>name</span><span>="TestWebServiceSoap"</span><span>></span><span><span>wsdl:operation </span><span>name</span><span>="HelloWorld"</span><span>></span><span><span>wsdl:input </span><span>message</span><span>="tns:HelloWorldSoapIn"</span><span>/></span><span><span>wsdl:output </span><span>message</span><span>="tns:HelloWorldSoapOut"</span><span>/></span><span></span><span>wsdl:operation</span><span>></span><span><span>wsdl:operation </span><span>name</span><span>="GetArray"</span><span>></span><span><span>wsdl:input </span><span>message</span><span>="tns:GetArraySoapIn"</span><span>/></span><span><span>wsdl:output </span><span>message</span><span>="tns:GetArraySoapOut"</span><span>/></span><span></span><span>wsdl:operation</span><span>></span><span></span><span>wsdl:portType</span><span>></span><span><span>wsdl:binding </span><span>name</span><span>="TestWebServiceSoap"</span><span> type</span><span>="tns:TestWebServiceSoap"</span><span>></span><span><span>soap:binding </span><span>transport</span><span>="http://schemas.xmlsoap.org/soap/http"</span><span>/></span><span><span>wsdl:operation </span><span>name</span><span>="HelloWorld"</span><span>></span><span><span>soap:operation </span><span>soapAction</span><span>="http://tempuri.org/HelloWorld"</span><span> style</span><span>="document"</span><span>/></span><span><span>wsdl:input</span><span>></span><span><span>soap:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:input</span><span>></span><span><span>wsdl:output</span><span>></span><span><span>soap:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:output</span><span>></span><span></span><span>wsdl:operation</span><span>></span><span><span>wsdl:operation </span><span>name</span><span>="GetArray"</span><span>></span><span><span>soap:operation </span><span>soapAction</span><span>="http://tempuri.org/GetArray"</span><span> style</span><span>="document"</span><span>/></span><span><span>wsdl:input</span><span>></span><span><span>soap:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:input</span><span>></span><span><span>wsdl:output</span><span>></span><span><span>soap:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:output</span><span>></span><span></span><span>wsdl:operation</span><span>></span><span></span><span>wsdl:binding</span><span>></span><span><span>wsdl:binding </span><span>name</span><span>="TestWebServiceSoap12"</span><span> type</span><span>="tns:TestWebServiceSoap"</span><span>></span><span><span>soap12:binding </span><span>transport</span><span>="http://schemas.xmlsoap.org/soap/http"</span><span>/></span><span><span>wsdl:operation </span><span>name</span><span>="HelloWorld"</span><span>></span><span><span>soap12:operation </span><span>soapAction</span><span>="http://tempuri.org/HelloWorld"</span><span> style</span><span>="document"</span><span>/></span><span><span>wsdl:input</span><span>></span><span><span>soap12:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:input</span><span>></span><span><span>wsdl:output</span><span>></span><span><span>soap12:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:output</span><span>></span><span></span><span>wsdl:operation</span><span>></span><span><span>wsdl:operation </span><span>name</span><span>="GetArray"</span><span>></span><span><span>soap12:operation </span><span>soapAction</span><span>="http://tempuri.org/GetArray"</span><span> style</span><span>="document"</span><span>/></span><span><span>wsdl:input</span><span>></span><span><span>soap12:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:input</span><span>></span><span><span>wsdl:output</span><span>></span><span><span>soap12:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:output</span><span>></span><span></span><span>wsdl:operation</span><span>></span><span></span><span>wsdl:binding</span><span>></span><span><span>wsdl:service </span><span>name</span><span>="TestWebService"</span><span>></span><span><span>wsdl:port </span><span>name</span><span>="TestWebServiceSoap"</span><span> binding</span><span>="tns:TestWebServiceSoap"</span><span>></span><span><span>soap:address </span><span>location</span><span>="http://localhost:63463/TestWebService.asmx"</span><span>/></span><span></span><span>wsdl:port</span><span>></span><span></span><span>wsdl:service</span><span>></span><span></span><span>wsdl:definitions</span><span>></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>View Code
把 http://localhost:63463/TestWebService.asmx 修改为你最终要访问的网址,我的是:
http://192.168.1.5/wsdl/006/TestWebService.php
三、c#调用php的webservice
1、新建网站-ASP.NET网站;
2、解决方案资源管理器-项目(右键)-添加Web引用,在URL中输入:
http://192.168.1.5/wsdl/006/TestWebService.php?wsdl
会显示出如下窗口:
点击[添加引用]
3、修改Default.aspx.cs代码为:
<span>using</span><span> System; </span><span>using</span><span> System.Collections.Generic; </span><span>using</span><span> System.Linq; </span><span>using</span><span> System.Web; </span><span>using</span><span> System.Web.UI; </span><span>using</span><span> System.Web.UI.WebControls; </span><span>public</span><span>partial</span><span>class</span><span> _Default : System.Web.UI.Page { </span><span>protected</span><span>void</span> Page_Load(<span>object</span><span> sender, EventArgs e) { </span><span>//</span><span>初始化WebService </span> WebReference.TestWebService srv = <span>new</span><span> WebReference.TestWebService(); </span><span>//</span><span>调第一个方法 </span><span>string</span> str =<span> srv.HelloWorld(); </span><span>//</span><span>调第二个方法 </span><span>string</span>[] arry = srv.GetArray(<span>"</span><span>string1</span><span>"</span>, <span>"</span><span>string2</span><span>"</span><span>); Response.Write(str); } }</span>View Code
4、CTRL+F5运行,看到最上面出现“welcome to dongzi world”,表示成功调用!
以上就介绍了一个php创建webservice,并通过c#调用的真实实例,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。