最近需要用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)
<?phpclassTestWebService
{publicfunctionHelloWorld()
{returnarray("HelloWorldResult"=>"welcome to dongzi world");
}publicfunctionGetArray($args)
{/*注意,Web Service的方法在声明时至多一个参数,
可是在调用该方法时就必须传value1,value2两个参数。
(这一点十分令人费解,我的理解是,在调用该方法时,系统把所有参数都放到一个对象里传过来的)*/$value1=$args->value1;$value2=$args->value2;//这两句是获取真正的参数$arry=array($value1,$value2);//返回值也很特别,不是直接返回$arry,而是把它放到一个对象里再返回。returnarray("GetArrayResult"=>$arry);
}
}//创建WebSevice实例$server=newSoapServer("TestWebService.wsdl");//指定类名$server->setClass("TestWebService");$server->handle();?>
View Code
二、生成.wsdl文件
本来我想简单一点,用SoapDiscovery.class.php来生成,可是总是出现各种奇葩问题;迫不得已安装了Zend Studio12.5,可是还是出现另外的奇葩问题;最终我找到一个可行的方案,是用vs2010来生成,C#调用没有问题。
1、打开vs2010,新建项目-asp.net空Web应用程序;
2、解决方案资源管理器-项目(右键)-添加-新建项-web服务,名称修改为TestWebService.asmx,代码如下:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Services;namespaceWebapplication1
{///<summary>///TestWebService 的摘要说明///</summary>[WebService(Namespace ="http://tempuri.org/")]
[WebServiceBinding(ConformsTo=Wsiprofiles.BasicPRofile1_1)]
[System.ComponentModel.ToolboxItem(false)]//若要允许使用 ASP.NETAjax从脚本中调用此 Web 服务,请取消对下行的注释。//[System.Web.Script.Services.ScriptService]publicclassTestWebService : System.Web.Services.WebService
{
[WebMethod]publicstringHelloWorld()
{return"";
}
[WebMethod]publicstring[] GetArray(stringa,stringb)
{returnnull;
}
}
}
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同目录下。
代码如下:
<?xml version="1.0" encoding="utf-8"?><wsdl:definitionsxmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/"xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"xmlns:tns="http://tempuri.org/"xmlns:s="http://www.w3.org/2001/XMLSchema"xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"targetNamespace="http://tempuri.org/"xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><wsdl:types><s:schemaelementFormDefault="qualified"targetNamespace="http://tempuri.org/"><s:elementname="HelloWorld"><s:complexType/></s:element><s:elementname="HelloWorldResponse"><s:complexType><s:sequence><s:elementminOccurs="0"maxOccurs="1"name="HelloWorldResult"type="s:string"/></s:sequence></s:complexType></s:element><s:elementname="GetArray"><s:complexType><s:sequence><s:elementminOccurs="0"maxOccurs="1"name="a"type="s:string"/><s:elementminOccurs="0"maxOccurs="1"name="b"type="s:string"/></s:sequence></s:complexType></s:element><s:elementname="GetArrayResponse"><s:complexType><s:sequence><s:elementminOccurs="0"maxOccurs="1"name="GetArrayResult"type="tns:ArrayOfString"/></s:sequence></s:complexType></s:element><s:complexTypename="ArrayOfString"><s:sequence><s:elementminOccurs="0"maxOccurs="unbounded"name="string"nillable="true"type="s:string"/></s:sequence></s:complexType></s:schema></wsdl:types><wsdl:messagename="HelloWorldSoapIn"><wsdl:partname="parameters"element="tns:HelloWorld"/></wsdl:message><wsdl:messagename="HelloWorldSoapOut"><wsdl:partname="parameters"element="tns:HelloWorldResponse"/></wsdl:message><wsdl:messagename="GetArraySoapIn"><wsdl:partname="parameters"element="tns:GetArray"/></wsdl:message><wsdl:messagename="GetArraySoapOut"><wsdl:partname="parameters"element="tns:GetArrayResponse"/></wsdl:message><wsdl:portTypename="TestWebServiceSoap"><wsdl:Operationname="HelloWorld"><wsdl:inputmessage="tns:HelloWorldSoapIn"/><wsdl:outputmessage="tns:HelloWorldSoapOut"/></wsdl:operation><wsdl:operationname="GetArray"><wsdl:inputmessage="tns:GetArraySoapIn"/><wsdl:outputmessage="tns:GetArraySoapOut"/></wsdl:operation></wsdl:portType><wsdl:bindingname="TestWebServiceSoap"type="tns:TestWebServiceSoap"><soap:bindingtransport="http://schemas.xmlsoap.org/soap/http"/><wsdl:operationname="HelloWorld"><soap:operationsoapAction="http://tempuri.org/HelloWorld"style="document"/><wsdl:input><soap:bodyuse="literal"/></wsdl:input><wsdl:output><soap:bodyuse="literal"/></wsdl:output></wsdl:operation><wsdl:operationname="GetArray"><soap:operationsoapAction="http://tempuri.org/GetArray"style="document"/><wsdl:input><soap:bodyuse="literal"/></wsdl:input><wsdl:output><soap:bodyuse="literal"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:bindingname="TestWebServiceSoap12"type="tns:TestWebServiceSoap"><soap12:bindingtransport="http://schemas.xmlsoap.org/soap/http"/><wsdl:operationname="HelloWorld"><soap12:operationsoapAction="http://tempuri.org/HelloWorld"style="document"/><wsdl:input><soap12:bodyuse="literal"/></wsdl:input><wsdl:output><soap12:bodyuse="literal"/></wsdl:output></wsdl:operation><wsdl:operationname="GetArray"><soap12:operationsoapAction="http://tempuri.org/GetArray"style="document"/><wsdl:input><soap12:bodyuse="literal"/></wsdl:input><wsdl:output><soap12:bodyuse="literal"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:servicename="TestWebService"><wsdl:portname="TestWebServiceSoap"binding="tns:TestWebServiceSoap"><soap:addresslocation="http://localhost:63463/TestWebService.asmx"/></wsdl:port></wsdl:service></wsdl:definitions>
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代码为:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;publicpartialclass_Default : System.Web.UI.Page
{protectedvoidPage_Load(objectsender, EventArgs e)
{//初始化WebServiceWebReference.TestWebService srv =newWebReference.TestWebService();//调第一个方法stringstr =srv.HelloWorld();//调第二个方法string[] arry = srv.GetArray("string1","string2");
Response.Write(str);
}
}
View Code
4、CTRL+F5运行,看到最上面出现“welcome to dongzi world”,表示成功调用!