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

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

一個(gè).NET調(diào)用PHP Web Service的典型例子

文章來(lái)源:365jz.com     點(diǎn)擊數(shù):407    更新時(shí)間:2009-12-20 00:36   參與評(píng)論

最近一個(gè)項(xiàng)目由"WinForm直接訪問(wèn)DB2"移植到"WinForm通過(guò)PHP Web Service來(lái)訪問(wèn)DB2”。

這個(gè)命題的難點(diǎn)不是訪問(wèn)DB2,而是.NET調(diào)用PHP Web Service。對(duì)于我這個(gè)長(zhǎng)期作.NET,之前一直以為只有通過(guò).NET調(diào)用PHP Web Service……的人來(lái)說(shuō),真是有點(diǎn)強(qiáng)“聰”所難了。

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

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

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

1-1)TestWebService.php代碼

TestWebService.php  <?php  class TestWebService  {      public function HelloWorld()      {          return array("HelloWorldResult"=>"Hello");      }       public function GetArray($args)          {            /*             注意,Web Service的方法在聲明時(shí)至多一個(gè)參數(shù),              可是在調(diào)用該方法時(shí)就必須傳value1,value2兩個(gè)參數(shù)。              (這一點(diǎn)十分令人費(fèi)解,我的理解是,在調(diào)用該方法時(shí),系統(tǒng)把所有參數(shù)都放到一個(gè)對(duì)象里傳過(guò)來(lái)的)            */          $value1 = $args->value1;            $value2 = $args->value2;//這兩句是獲取真正的參數(shù)             $arry = array($value1,$value2);           //返回值也很特別,不是直接返回$arry,而是把它放到一個(gè)對(duì)象里再返回。          return array("GetArrayResult"=>$arry);      }  }  //創(chuàng)建WebSevice實(shí)例  $server = new SoapServer("TestWebService.wsdl");  //指定類名  $server->setClass("TestWebService");  $server->handle();  ?> 1-2)TestWebService.wsdl代碼

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的代碼比較長(zhǎng),當(dāng)方法很多時(shí),手敲代碼是不太可能的。有一個(gè)巧的辦法,就是也用.NET實(shí)現(xiàn)一個(gè)不含真正方法體的Web Serivce,然后通過(guò)http://***/TestWebService.asmx?wsdl的方法生成wsdl代碼文件。

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

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

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

2).NET客戶端代碼

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

.NET調(diào)用PHP Web Service調(diào)用代碼(C#)

//初始化WebService          localhost.TestWebService srv = new localhost.TestWebService();          //調(diào)第一個(gè)方法           string str = srv.HelloWorld();          //調(diào)第二個(gè)方法           string[] arry= srv.GetArray("string1","string2"); .NET調(diào)用PHP Web Service總結(jié):

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

(二)PHP Web Service方法的傳入?yún)?shù)、返回值都至多有一個(gè),因?yàn)檎嬲{(diào)用時(shí)的參數(shù)和返回值,都是包裝到一個(gè)對(duì)象中傳送的。

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

(四)若返回值需要是多張二維表時(shí),我淺薄的以為,可以傳化一組字符串?dāng)?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中按行列存放的值]

按順序?qū)⑸厦鎇]中的內(nèi)容串成字符串?dāng)?shù)組,效率還不錯(cuò),我測(cè)試10000行240列的數(shù)據(jù),我有現(xiàn)成編解代碼,有興趣的可以向我索取

本文作者:未知

如對(duì)本文有疑問(wèn),請(qǐng)?zhí)峤坏浇涣髡搲瑥V大熱心網(wǎng)友會(huì)為你解答??! 點(diǎn)擊進(jìn)入論壇

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

其它欄目

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

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

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

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

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