一个.NET调用PHP Web Service的典型例子


  本文标签:.NET调用PHP Web Service

  最近一个项目由"WinForm直接访问DB2"移植到"WinForm通过PHP Web Service来访问DB2”  。

  这个命题的难点不是访问DB2,而是.NET调用PHP Web Service  。对于我这个长期作.NET,之前一直以为只有通过.NET调用PHP Web Service……的人来说,真是有点强“聪”所难了  。

  但是问题还是要解决的,期限就摆在眼前呢  。经过一番调查,终于有了眉目,现在分享给大家  。

  首先要说明的,PHP服务器需要至少需要两个文件——一个WSDL文件和一个PHP文件  。WSDL文件是一种机读的XML文件,用于描述WebService提供的服务和调用方法(对于.NET则可以自动生成调用代码,十分好用),php文件就是真正实现的WEB服务了  。

  1)PHP服务器端代码

  1-1)TestWebService.php代码

  1. TestWebService.php  
  2. class TestWebService  
  3. {  
  4.     public function HelloWorld()  
  5.     {  
  6.         return array("HelloWorldResult"=>"Hello");  
  7.     }  
  8.  
  9.     public function GetArray($args)  
  10.         {  
  11.           /*  
  12.            注意,Web Service的方法在声明时至多一个参数,  
  13.             可是在调用该方法时就必须传value1,value2两个参数  。  
  14.             (这一点十分令人费解,我的理解是,在调用该方法时,系统把所有参数都放到一个对象里传过来的)  
  15.           */ 
  16.  
  17.         $value1 = $args->value1;    
  18.         $value2 = $args->value2;//这两句是获取真正的参数  
  19.    
  20.         $arry = array($value1,$value2);  
  21.  
  22.         //返回值也很特别,不是直接返回$arry,而是把它放到一个对象里再返回  。  
  23.         return array("GetArrayResult"=>$arry);  
  24.     }  
  25. }  
  26. //创建WebSevice实例  
  27. $server = new SoapServer("TestWebService.wsdl");  
  28. //指定类名  
  29. $server->setClass("TestWebService");  
  30. $server->handle();  
  31. ?> 

  1-2)TestWebService.wsdl代码

  1. TestWebService.wsdl  
  2. xml version="1.0" encoding="utf-8"?> 
  3. <wsdl:definitions xmlns: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/"> 
  4.   <wsdl:types> 
  5.     <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/"> 
  6.       <s:element name="HelloWorld"> 
  7.         <s:complexType /> 
  8.       s:element> 
  9.       <s:element name="HelloWorldResponse"> 
  10.         <s:complexType> 
  11.           <s:sequence> 
  12.             <s:element minOccurs="0" maxOccurs="1" name="HelloWorldResult" type="s:string" /> 
  13.           s:sequence> 
  14.         s:complexType> 
  15.       s:element> 
  16.       <s:element name="GetArray"> 
  17.         <s:complexType> 
  18.           <s:sequence> 
  19.             <s:element minOccurs="0" maxOccurs="1" name="value1" type="s:string" /> 
  20.             <s:element minOccurs="0" maxOccurs="1" name="value2" type="s:string" /> 
  21.           s:sequence> 
  22.         s:complexType> 
  23.       s:element> 
  24.       <s:element name="GetArrayResponse"> 
  25.         <s:complexType> 
  26.           <s:sequence> 
  27.             <s:element minOccurs="0" maxOccurs="1" name="GetArrayResult" type="tns:ArrayOfString" /> 
  28.           s:sequence> 
  29.         s:complexType> 
  30.       s:element> 
  31.       <s:complexType name="ArrayOfString"> 
  32.         <s:sequence> 
  33.           <s:element minOccurs="0" maxOccurs="unbounded" name="string" nillable="true" type="s:string" /> 
  34.         s:sequence> 
  35.       s:complexType> 
  36.     s:schema> 
  37.   wsdl:types> 
  38.   <wsdl:message name="HelloWorldSoapIn"> 
  39.     <wsdl:part name="parameters" element="tns:HelloWorld" /> 
  40.   wsdl:message> 
  41.   <wsdl:message name="HelloWorldSoapOut"> 
  42.     <wsdl:part name="parameters" element="tns:HelloWorldResponse" /> 
  43.   wsdl:message> 
  44.   <wsdl:message name="GetArraySoapIn"> 
  45.     <wsdl:part name="parameters" element="tns:GetArray" /> 
  46.   wsdl:message> 
  47.   <wsdl:message name="GetArraySoapOut"> 
  48.     <wsdl:part name="parameters" element="tns:GetArrayResponse" /> 
  49.   wsdl:message> 
  50.   <wsdl:portType name="TestWebServiceSoap"> 
  51.     <wsdl:operation name="HelloWorld"> 
  52.       <wsdl:input message="tns:HelloWorldSoapIn" /> 
  53.       <wsdl:output message="tns:HelloWorldSoapOut" /> 
  54.     wsdl:operation> 
  55.     <wsdl:operation name="GetArray"> 
  56.       <wsdl:input message="tns:GetArraySoapIn" /> 
  57.       <wsdl:output message="tns:GetArraySoapOut" /> 
  58.     wsdl:operation> 
  59.   wsdl:portType> 
  60.   <wsdl:binding name="TestWebServiceSoap" type="tns:TestWebServiceSoap"> 
  61.     <soap:binding transport="http://schemas.xmlsoap.org/soap/http" /> 
  62.     <wsdl:operation name="HelloWorld"> 
  63.       <soap:operation soapAction="http://tempuri.org/HelloWorld" style="document" /> 
  64.       <wsdl:input> 
  65.         <soap:body use="literal" /> 
  66.       wsdl:input> 
  67.       <wsdl:output> 
  68.         <soap:body use="literal" /> 
  69.       wsdl:output> 
  70.     wsdl:operation> 
  71.     <wsdl:operation name="GetArray"> 
  72.       <soap:operation soapAction="http://tempuri.org/GetArray" style="document" /> 
  73.       <wsdl:input> 
  74.         <soap:body use="literal" /> 
  75.       wsdl:input> 
  76.       <wsdl:output> 
  77.         <soap:body use="literal" /> 
  78.       wsdl:output> 
  79.     wsdl:operation> 
  80.   wsdl:binding> 
  81.   <wsdl:binding name="TestWebServiceSoap12" type="tns:TestWebServiceSoap"> 
  82.     <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" /> 
  83.     <wsdl:operation name="HelloWorld"> 
  84.       <soap12:operation soapAction="http://tempuri.org/HelloWorld" style="document" /> 
  85.       <wsdl:input> 
  86.         <soap12:body use="literal" /> 
  87.       wsdl:input> 
  88.       <wsdl:output> 
  89.         <soap12:body use="literal" /> 
  90.       wsdl:output> 
  91.     wsdl:operation> 
  92.     <wsdl:operation name="GetArray"> 
  93.       <soap12:operation soapAction="http://tempuri.org/GetArray" style="document" /> 
  94.       <wsdl:input> 
  95.         <soap12:body use="literal" /> 
  96.       wsdl:input> 
  97.       <wsdl:output> 
  98.         <soap12:body use="literal" /> 
  99.       wsdl:output> 
  100.     wsdl:operation> 
  101.   wsdl:binding> 
  102.   <wsdl:service name="TestWebService"> 
  103.     <wsdl:port name="TestWebServiceSoap" binding="tns:TestWebServiceSoap"> 
  104.       <soap:address location="http://localhost/phpmyadmin/ws/TestWebService.php" /> 
  105.     wsdl:port> 
  106.     <wsdl:port name="TestWebServiceSoap12" binding="tns:TestWebServiceSoap12"> 
  107.       <soap12:address location="http://localhost/phpmyadmin/ws/TestWebService.php" /> 
  108.     wsdl:port> 
  109.   wsdl:service> 
  110. wsdl:definitions> 

  WSDL的代码比较长,当方法很多时,手敲代码是不太可能的  。有一个巧的办法,就是也用.NET实现一个不含真正方法体的Web Serivce,然后通过http://***/TestWebService.asmx?wsdl的方法生成wsdl代码文件  。

  关于WSDL文件,我要说明特别说明两点:

  (1)soap:address结点是声明WebService的地址,在部署时要改成相应地址;

  (2)一维数组的声明类型为ArrayOfType,字符串数组为ArrayOfString  。如果Type不是简单类型,则Type需要另外声明  。

  2).NET客户端代码

  先要添加Web引用,地址为WSDL文件的Http地址  。

  .NET调用PHP Web Service调用代码(C#)

  1. //初始化WebService  
  2.         localhost.TestWebService srv = new localhost.TestWebService();  
  3.         //调第一个方法  
  4.          string str = srv.HelloWorld();  
  5.         //调第二个方法  
  6.          string[] arrysrv.GetArray("string1","string2"); 

  .NET调用PHP Web Service总结:

  (一)PHP是一种弱类型语言,检查错误比较困难  。array类型也与一般理解的数组不同,它也有类似Hashtable的用法  。

  (二)PHP Web Service方法的传入参数、返回值都至多有一个,因为真正调用时的参数和返回值,都是包装到一个对象中传送的  。

  (三)PHP Web Service也支持自定义类型和自定义类型数组等复杂类型,但不支持多组数组  。

  (四)若返回值需要是多张二维表时,我浅薄的以为,可以传化一组字符串数组传送,格式为

  [表1行数],[表1列数],[表1列名1],[表1列名2],……[表1列名N],[表1中按行列存放的值]

  [表2行数],[表2列数],[表2列名1],[表2列名2],……[表2列名N],[表2中按行列存放的值]……

  [表M行数],[表M列数],[表M列名1],[表M列名2],……[表M列名N],[表2中按行列存放的值]

  按顺序将上面[]中的内容串成字符串数组,效率还不错,我测试10000行240列的数据,我有现成编解代码,有兴趣的可以向我索取.