.NET调PHP Web Service的典型例子

最近一个项目由“WinForm直接访问DB2”移植到“WinForm通过PHP Web Service来访问DB2”。
(优点是php可以架在Linux上,而Linux是免费的)
这个命题的难点不是访问DB2,而是.NET调用PHP的Web Service。对于我这个长期作.NET,之前一直以为只有.NET才可以做Web Service……的人来说,真是有点强“聪”所难了。

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

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

1)PHP服务器端代码

1-1)TestWebService.php代码

以下为引用的内容:

<?php
class TestWebService
{
    public function HelloWorld()
    {
        return array("HelloWorldResult"=>"Hello");
    }

    public function GetArray($args)
        {
          /*
           注意,Web Service的方法在声明时至多一个参数,
            可是在调用该方法时就必须传value1,value2两个参数。
            (这一点十分令人费解,我的理解是,在调用该方法时,系统把所有参数都放到一个对象里传过来的)
          */

        $value1 = $args->value1; 
        $value2 = $args->value2;//这两句是获取真正的参数
 
        $arry = array($value1,$value2);

        //返回值也很特别,不是直接返回$arry,而是把它放到一个对象里再返回。
        return array("GetArrayResult"=>$arry);
    }
}

//创建WebSevice实例
$server = new SoapServer("TestWebService.wsdl");
//指定类名
$server->setClass("TestWebService");

$server->handle();

?>
 

1-2)TestWebService.wsdl代码

以下为引用的内容:

<?xml version="1.0" encoding="utf-8"?>
<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/">
  <wsdl:types>
    <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
      <s:element name="HelloWorld">
        <s:complexType />
      </s:element>
      <s:element name="HelloWorldResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="HelloWorldResult" type="s:string" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="GetArray">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="value1" type="s:string" />
            <s:element minOccurs="0" maxOccurs="1" name="value2" type="s:string" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="GetArrayResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="GetArrayResult" type="tns:ArrayOfString" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:complexType name="ArrayOfString">
        <s:sequence>
          <s:element minOccurs="0" maxOccurs="unbounded" name="string" nillable="true" type="s:string" />
        </s:sequence>
      </s:complexType>
    </s:schema>
  </wsdl:types>
  <wsdl:message name="HelloWorldSoapIn">
    <wsdl:part name="parameters" element="tns:HelloWorld" />
  </wsdl:message>
  <wsdl:message name="HelloWorldSoapOut">
    <wsdl:part name="parameters" element="tns:HelloWorldResponse" />
  </wsdl:message>
  <wsdl:message name="GetArraySoapIn">
    <wsdl:part name="parameters" element="tns:GetArray" />
  </wsdl:message>
  <wsdl:message name="GetArraySoapOut">
    <wsdl:part name="parameters" element="tns:GetArrayResponse" />
  </wsdl:message>
  <wsdl:portType name="TestWebServiceSoap">
    <wsdl:operation name="HelloWorld">
      <wsdl:input message="tns:HelloWorldSoapIn" />
      <wsdl:output message="tns:HelloWorldSoapOut" />
    </wsdl:operation>
    <wsdl:operation name="GetArray">
      <wsdl:input message="tns:GetArraySoapIn" />
      <wsdl:output message="tns:GetArraySoapOut" />
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="TestWebServiceSoap" type="tns:TestWebServiceSoap">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="HelloWorld">
      <soap:operation soapAction="http://tempuri.org/HelloWorld" style="document" />
      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="GetArray">
      <soap:operation soapAction="http://tempuri.org/GetArray" style="document" />
      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:binding name="TestWebServiceSoap12" type="tns:TestWebServiceSoap">
    <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="HelloWorld">
      <soap12:operation soapAction="http://tempuri.org/HelloWorld" style="document" />
      <wsdl:input>
        <soap12:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap12:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="GetArray">
      <soap12:operation soapAction="http://tempuri.org/GetArray" style="document" />
      <wsdl:input>
        <soap12:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap12:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="TestWebService">
    <wsdl:port name="TestWebServiceSoap" binding="tns:TestWebServiceSoap">
      <soap:address location="http://localhost/phpmyadmin/ws/TestWebService.php" />
    </wsdl:port>
    <wsdl:port name="TestWebServiceSoap12" binding="tns:TestWebServiceSoap12">
      <soap12:address location="http://localhost/phpmyadmin/ws/TestWebService.php" />
    </wsdl:port>
  </wsdl:service>
</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地址。

调用代码(C#)

以下为引用的内容:

        //初始化WebService
        localhost.TestWebService srv = new localhost.TestWebService();
        //调第一个方法
         string str = srv.HelloWorld();
        //调第二个方法
         string[] arry= srv.GetArray("string1","string2");
 

总结: (一)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列的数据,我有现成编解代码,有兴趣的可以向我索取.

时间: 2024-09-17 03:57:07

.NET调PHP Web Service的典型例子的相关文章

典型的Web Service结构

web 典型的Web Service结构(可乐 2001年11月01日 18:35) 典型的Web Service结构. 不管你的Web service是用什么工具,什么语言写出来的,只要你用SOAP协议通过HTTP来调用它,总体结构都应如下图所示.通常,你用你自己喜欢的语言(如VB 6或者VB.NET)来构建你的Web service,然后用SOAP Toolkit或者.NET的内建支持来把它暴露给Web客户.于是,任何语言,任何平台上的客户都可以阅读其WSDL文档,以调用这个Web serv

Axis2与Eclipse整合开发Web Service之一:简单的计算服务例子

系统功能: 开发一个计算器服务CalculateService,这个服务包含加(plus).减(minus).乘 (multiply) .除(divide)的操作. 开发前准备: 1.安装Eclipse-jee: 2.下载Axis2的最新版本Axis2 1.4.1 Release,网址 http://ws.apache.org/axis2/download/1_4_1/download.cgi ,选择Standard Binary Distribution 的.zip包即"axis2-1.4.1

Mule ESB 学习笔记(11)Web Service Proxy(这里是一个可以正常运行的例子)

一.WebSevice Proxy 简介      WebService Proxy 是ESB中最常见的使用场景之一,即通过 ESB 直接转发 WebService Client 的 SOAP 请求,并将 WebServcie Provider 的 SOAP 响应转发给 WebService Client ,此时的ESB就是一个WebService Proxy.   二.WebSevice Proxy 在 Mule 中的产生背景       Mule3.0 新增了一些比较有亮点的新特性 ,其中包

Java RESTful Web Service实战(第2版)

Java核心技术系列 Java RESTful Web Service实战 (第2版) 韩陆 著 图书在版编目(CIP)数据 Java RESTful Web Service实战 / 韩陆著. -2版. -北京:机械工业出版社,2016.7 (Java核心技术系列) ISBN 978-7-111-54213-1 Ⅰ. J-   Ⅱ. 韩-   Ⅲ. JAVA语言-程序设计   Ⅳ. TP312 中国版本图书馆CIP数据核字(2016)第156331号 Java RESTful Web Servi

在ASP.NET Atlas中调用Web Service—处理错误

asp.net|web|错误 在本系列的上一篇(在ASP.NET Atlas中调用Web Service--介绍及简单应用)中,我们熟悉了Atlas中调用Web Service的最基础方法,但是在实际开发中,仅仅发出请求并等待返回结果是不够的,我们大都需要考虑对错误超时等的处理,也要允许用户取消操作.幸运的是,Atlas对Web Service中的Web Method的封装也充分考虑到了这些需求.让我们举一个Web Method的例子来说明,例如,对于如下的Web Method: public

web service c# 互调 java (转)

一:简介 本文介绍了Java与.NET开发的Web Services相互调用的技术.本文包括两个部分,第一部分介绍了如何用.NET做客户端调用Java写的Web Services,第二部分介绍了如何用Java做客户端调用.NET开发的Web Services. 二:项目需要的工具 Windows2000 Server(IIS) Jbuilder9.0( 含有Tomcat , axis) JDK1.4 Java Web Services Develop VS.Net 2003 备注:如果没有JBu

在Web Service中实现Transaction

web 在Web Service中实现Transaction.Net Framework为类,WebForm和WebService提供了事务处理功能.在传统的windows应用程序中,要写一个有事务处理功能的组件不仅要写代码而且要在组件服务中创建一个事务包.这就意味着在任何一台要处理这个事务的机器上,你都不得不打开mmc在COM+应用程序节点下创建一个新包..NET Framework使得这一切变得很简单,事实上我们不用在组件服务中作任何事,这一切都是自动完成的.对WebService来说,.N

在Web Service中使用ASP.net状态保持(4)

asp.net|web 下一步,我创建了一个简单的WinForm应用程序,并且将上述的Web Service添加到Web引用中.下面就是调用我的Web Service的代码:' 这里并没有与Session打交道Private Sub Button1_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button1.ClickDim proxy As New localhost.Service1()

ASP.NET Web Service

asp.net|web     现在Internet正在不断地发展着,在互联网应用刚开始的时候,我们浏览的网页只是静态的,不可交互的.而现在随着技术的日益发展,将提供给网页浏览者一个可编程的Web 站点.这些站点将在组织.应用.服务.驱动上更加紧密的结合在一起,这些站点将通过一些应用软件直接连接到另一个Web 站点,这些可编程的Web 站点相比传统的web站点来说,将变得更加能重复使用,也更加智能化!       .net平台给我们提供了一种运行环境,即公用语言运行环境(CLR,Common L