五月综合缴情婷婷六月,色94色欧美sute亚洲线路二,日韩制服国产精品一区,色噜噜一区二区三区,香港三级午夜理伦三级三

您現(xiàn)在的位置: 365建站網(wǎng) > 365文章 > .NET調(diào)PHP Web Service的典型例子

.NET調(diào)PHP Web Service的典型例子

文章來源:365jz.com     點擊數(shù):408    更新時間:2009-12-20 00:36   參與評論

最近一個項目由“WinForm直接訪問DB2”移植到“WinForm通過PHP Web Service來訪問DB2”。

(優(yōu)點是php可以架在Linux上,而Linux是免費的)

這個命題的難點不是訪問DB2,而是.NET調(diào)用PHP的Web Service。對于我這個長期作.NET,之前一直以為只有.NET才可以做Web Service……的人來說,真是有點強“聰”所難了。

但是問題還是要解決的,期限就擺在眼前呢。經(jīng)過一番調(diào)查,終于有了眉目,現(xiàn)在分享給大家。

首先要說明的,PHP服務(wù)器需要至少需要兩個文件——一個WSDL文件和一個PHP文件。WSDL文件是一種機讀的XML文件,用于描述WebService提供的服務(wù)和調(diào)用方法(對于.NET則可以自動生成調(diào)用代碼,十分好用),php文件就是真正實現(xiàn)的WEB服務(wù)了。

1)PHP服務(wù)器端代碼

1-1)TestWebService.php代碼

以下為引用的內(nèi)容:

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

    public function GetArray($args)
        {
          /*
           注意,Web Service的方法在聲明時至多一個參數(shù),
            可是在調(diào)用該方法時就必須傳value1,value2兩個參數(shù)。
            (這一點十分令人費解,我的理解是,在調(diào)用該方法時,系統(tǒng)把所有參數(shù)都放到一個對象里傳過來的)
          */

        $value1 = $args->value1; 
        $value2 = $args->value2;//這兩句是獲取真正的參數(shù)
 
        $arry = array($value1,$value2);

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

//創(chuàng)建WebSevice實例
$server = new SoapServer("TestWebService.wsdl");
//指定類名
$server->setClass("TestWebService");

$server->handle();

?>
 


1-2)TestWebService.wsdl代碼

以下為引用的內(nèi)容:

<?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實現(xiàn)一個不含真正方法體的Web Serivce,然后通過http://***/TestWebService.asmx?wsdl的方法生成wsdl代碼文件。

關(guān)于WSDL文件,我要說明特別說明兩點:

(1)soap:address結(jié)點是聲明WebService的地址,在部署時要改成相應(yīng)地址;

(2)一維數(shù)組的聲明類型為ArrayOfType,字符串數(shù)組為ArrayOfString。如果Type不是簡單類型,則Type需要另外聲明。

2).NET客戶端代碼

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

調(diào)用代碼(C#)

以下為引用的內(nèi)容:

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


總結(jié): (一)PHP是一種弱類型語言,檢查錯誤比較困難。array類型也與一般理解的數(shù)組不同,它也有類似Hashtable的用法。

(二)PHP Web Service方法的傳入?yún)?shù)、返回值都至多有一個,因為真正調(diào)用時的參數(shù)和返回值,都是包裝到一個對象中傳送的。

(三)PHP Web Service也支持自定義類型和自定義類型數(shù)組等復(fù)雜類型,但不支持多組數(shù)組。

(四)若返回值需要是多張二維表時,我淺薄的以為,可以傳化一組字符串數(shù)組傳送,格式為

[表1行數(shù)],[表1列數(shù)],[表1列名1],[表1列名2],……[表1列名N],[表1中按行列存放的值]

[表2行數(shù)],[表2列數(shù)],[表2列名1],[表2列名2],……[表2列名N],[表2中按行列存放的值]

……

[表M行數(shù)],[表M列數(shù)],[表M列名1],[表M列名2],……[表M列名N],[表2中按行列存放的值]

本文作者:未知

如對本文有疑問,請?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會為你解答??! 點擊進入論壇

發(fā)表評論 (408人查看,0條評論)
請自覺遵守互聯(lián)網(wǎng)相關(guān)的政策法規(guī),嚴禁發(fā)布色情、暴力、反動的言論。
昵稱:
最新評論
------分隔線----------------------------

其它欄目

· 建站教程
· 365學(xué)習

業(yè)務(wù)咨詢

· 技術(shù)支持
· 服務(wù)時間:9:00-18:00
365建站網(wǎng)二維碼

Powered by 365建站網(wǎng) RSS地圖 HTML地圖

copyright © 2013-2024 版權(quán)所有 鄂ICP備17013400號