一個php建立webservice,並透過c#呼叫的真實實例

WBOY
發布: 2016-07-29 09:15:11
原創
903 人瀏覽過

最近需要用php創建webservice供C#和JAVA來調用,透過3天的搜尋和嘗試,終於成功在C#下調用,JAVA的調用還沒開始,為防止忘記,在這裡記錄下來全過程。

本文參考了許多文章,文中也採用了其中的範例。

本文目錄如下

一、php建立webservice

二、產生.wsdl檔案

三、c#調用php的webservice

、創建環境我用的是windows的apache,php.ini檔案中extension=php_soap.dll 去掉註解即可,前提是已經安裝了php_soap.dll。 一個php建立webservice,並透過c#呼叫的真實實例

環境測試,在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)

<span>php  
</span><span>class</span><span> TestWebService  
{  
    </span><span>public</span><span>function</span><span> HelloWorld()  
    {  
        </span><span>return</span><span>array</span>("HelloWorldResult"=>"welcome to dongzi world"<span>);  
    }  
 
    </span><span>public</span><span>function</span> GetArray(<span>$args</span><span>)  
        {  
          </span><span>/*</span><span>
           注意,Web Service的方法在声明时至多一个参数,  
            可是在调用该方法时就必须传value1,value2两个参数。  
            (这一点十分令人费解,我的理解是,在调用该方法时,系统把所有参数都放到一个<strong>对象</strong>里传过来的)  
          </span><span>*/</span><span>$value1</span> = <span>$args</span>-><span>value1;    
        </span><span>$value2</span> = <span>$args</span>->value2;<span>//</span><span>这两句是获取真正的参数  </span><span>$arry</span> = <span>array</span>(<span>$value1</span>,<span>$value2</span><span>);  
 
        </span><span>//</span><span>返回值也很特别,不是直接返回$arry,而是把它放到一个<strong>对象</strong>里再返回。  </span><span>return</span><span>array</span>("GetArrayResult"=><span>$arry</span><span>);  
    }  
}  
</span><span>//</span><span>创建WebSevice实例  </span><span>$server</span> = <span>new</span> SoapServer("TestWebService.wsdl"<span>);  
</span><span>//</span><span>指定类名  </span><span>$server</span>->setClass("TestWebService"<span>);  
</span><span>$server</span>-><span>handle();  
</span>?>
登入後複製

View Code

一個php建立webservice,並透過c#呼叫的真實實例一個php建立webservice,並透過c#呼叫的真實實例二、生成想php來生成,可是總是出現各種奇葩問題;迫不得已安裝了Zend Studio12.5,可是還是出現另外的奇葩問題;最終我找到一個可行的方案,是用vs2010來生成,C#調用沒有問題。

1、開啟vs2010,新專案-ASP.NET空Web應用程式;2、解決方案資源管理器

-專案(右鍵)-新增-新建項目-web服務,名稱修改為TestWebService.asmx,程式碼如下:

<span>using</span><span> System;
</span><span>using</span><span> System.Collections.Generic;
</span><span>using</span><span> System.Linq;
</span><span>using</span><span> System.Web;
</span><span>using</span><span> System.Web.Services;

</span><span>namespace</span><span> Web<strong>Application</strong>1
{
    </span><span>///</span><span><summary></summary></span><span>///</span><span> TestWebService 的摘要说明
    </span><span>///</span><span></span>    [WebService(Namespace = <span>"</span><span>http://tempuri.org/</span><span>"</span><span>)]
    [WebServiceBinding(ConformsTo </span>=<span> WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(</span><span>false</span><span>)]
    </span><span>//</span><span> 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    </span><span>//</span><span> [System.Web.Script.Services.ScriptService]</span><span>public</span><span>class</span><span> TestWebService : System.Web.Services.WebService
    {

        [WebMethod]
        </span><span>public</span><span>string</span><span> HelloWorld()
        {
            </span><span>return</span><span>""</span><span>;
        }

        [WebMethod]
        </span><span>public</span><span>string</span>[] GetArray(<span>string</span> a,<span>string</span><span> b)
        {
            </span><span>return</span><span>null</span><span>;
        }
    }
}</span>
登入後複製

View Code

HelloWorld()和GetArray()是我們希望暴露的方法,我寫成空方法了。

3、F5運行,在彈出的IE瀏覽器中複製運行的url位址一個php建立webservice,並透過c#呼叫的真實實例一個php建立webservice,並透過c#呼叫的真實實例http://localhost:63463/TestWebService.asmx

打開一個新的IE瀏覽器窗口,貼上並修改為,然後回車,此時視窗會展示wsdl檔案的xml格式內容:

http://localhost:63463/TestWebService.asmx?wsdl

4、點擊IE瀏覽器的檔案-另存為選單,儲存為TestWebService. wsdl文件,放到和TestWebService.php同目錄下。

代碼如下:

<span></span><span>xml version="1.0" encoding="utf-8"</span><span>?></span><span><span>wsdl:definitions </span><span>xmlns:soap</span><span>="http://schemas.xmlsoap.org/wsdl/soap/"</span><span> xmlns:tm</span><span>="http://microsoft.com/wsdl/mime/textMatching/"</span><span> xmlns:soapenc</span><span>="http://schemas.xmlsoap.org/soap/encoding/"</span><span> xmlns:mime</span><span>="http://schemas.xmlsoap.org/wsdl/mime/"</span><span> xmlns:tns</span><span>="http://tempuri.org/"</span><span> xmlns:s</span><span>="http://www.w3.org/2001/XMLSchema"</span><span> xmlns:soap12</span><span>="http://schemas.xmlsoap.org/wsdl/soap12/"</span><span> xmlns:http</span><span>="http://schemas.xmlsoap.org/wsdl/http/"</span><span> targetNamespace</span><span>="http://tempuri.org/"</span><span> xmlns:wsdl</span><span>="http://schemas.xmlsoap.org/wsdl/"</span><span>></span><span><span>wsdl:types</span><span>></span><span><span>s:schema </span><span>elementFormDefault</span><span>="qualified"</span><span> targetNamespace</span><span>="http://tempuri.org/"</span><span>></span><span><span>s:element </span><span>name</span><span>="HelloWorld"</span><span>></span><span><span>s:complexType </span><span>/></span><span></span><span>s:element</span><span>></span><span><span>s:element </span><span>name</span><span>="HelloWorldResponse"</span><span>></span><span><span>s:complexType</span><span>></span><span><span>s:sequence</span><span>></span><span><span>s:element </span><span>minOccurs</span><span>="0"</span><span> maxOccurs</span><span>="1"</span><span> name</span><span>="HelloWorldResult"</span><span> type</span><span>="s:string"</span><span>/></span><span></span><span>s:sequence</span><span>></span><span></span><span>s:complexType</span><span>></span><span></span><span>s:element</span><span>></span><span><span>s:element </span><span>name</span><span>="GetArray"</span><span>></span><span><span>s:complexType</span><span>></span><span><span>s:sequence</span><span>></span><span><span>s:element </span><span>minOccurs</span><span>="0"</span><span> maxOccurs</span><span>="1"</span><span> name</span><span>="a"</span><span> type</span><span>="s:string"</span><span>/></span><span><span>s:element </span><span>minOccurs</span><span>="0"</span><span> maxOccurs</span><span>="1"</span><span> name</span><span>="b"</span><span> type</span><span>="s:string"</span><span>/></span><span></span><span>s:sequence</span><span>></span><span></span><span>s:complexType</span><span>></span><span></span><span>s:element</span><span>></span><span><span>s:element </span><span>name</span><span>="GetArrayResponse"</span><span>></span><span><span>s:complexType</span><span>></span><span><span>s:sequence</span><span>></span><span><span>s:element </span><span>minOccurs</span><span>="0"</span><span> maxOccurs</span><span>="1"</span><span> name</span><span>="GetArrayResult"</span><span> type</span><span>="tns:ArrayOfString"</span><span>/></span><span></span><span>s:sequence</span><span>></span><span></span><span>s:complexType</span><span>></span><span></span><span>s:element</span><span>></span><span><span>s:complexType </span><span>name</span><span>="ArrayOfString"</span><span>></span><span><span>s:sequence</span><span>></span><span><span>s:element </span><span>minOccurs</span><span>="0"</span><span> maxOccurs</span><span>="unbounded"</span><span> name</span><span>="string"</span><span> nillable</span><span>="true"</span><span> type</span><span>="s:string"</span><span>/></span><span></span><span>s:sequence</span><span>></span><span></span><span>s:complexType</span><span>></span><span></span><span>s:schema</span><span>></span><span></span><span>wsdl:types</span><span>></span><span><span>wsdl:message </span><span>name</span><span>="HelloWorldSoapIn"</span><span>></span><span><span>wsdl:part </span><span>name</span><span>="parameters"</span><span> element</span><span>="tns:HelloWorld"</span><span>/></span><span></span><span>wsdl:message</span><span>></span><span><span>wsdl:message </span><span>name</span><span>="HelloWorldSoapOut"</span><span>></span><span><span>wsdl:part </span><span>name</span><span>="parameters"</span><span> element</span><span>="tns:HelloWorldResponse"</span><span>/></span><span></span><span>wsdl:message</span><span>></span><span><span>wsdl:message </span><span>name</span><span>="GetArraySoapIn"</span><span>></span><span><span>wsdl:part </span><span>name</span><span>="parameters"</span><span> element</span><span>="tns:GetArray"</span><span>/></span><span></span><span>wsdl:message</span><span>></span><span><span>wsdl:message </span><span>name</span><span>="GetArraySoapOut"</span><span>></span><span><span>wsdl:part </span><span>name</span><span>="parameters"</span><span> element</span><span>="tns:GetArrayResponse"</span><span>/></span><span></span><span>wsdl:message</span><span>></span><span><span>wsdl:portType </span><span>name</span><span>="TestWebServiceSoap"</span><span>></span><span><span>wsdl:operation </span><span>name</span><span>="HelloWorld"</span><span>></span><span><span>wsdl:input </span><span>message</span><span>="tns:HelloWorldSoapIn"</span><span>/></span><span><span>wsdl:output </span><span>message</span><span>="tns:HelloWorldSoapOut"</span><span>/></span><span></span><span>wsdl:operation</span><span>></span><span><span>wsdl:operation </span><span>name</span><span>="GetArray"</span><span>></span><span><span>wsdl:input </span><span>message</span><span>="tns:GetArraySoapIn"</span><span>/></span><span><span>wsdl:output </span><span>message</span><span>="tns:GetArraySoapOut"</span><span>/></span><span></span><span>wsdl:operation</span><span>></span><span></span><span>wsdl:portType</span><span>></span><span><span>wsdl:binding </span><span>name</span><span>="TestWebServiceSoap"</span><span> type</span><span>="tns:TestWebServiceSoap"</span><span>></span><span><span>soap:binding </span><span>transport</span><span>="http://schemas.xmlsoap.org/soap/http"</span><span>/></span><span><span>wsdl:operation </span><span>name</span><span>="HelloWorld"</span><span>></span><span><span>soap:operation </span><span>soapAction</span><span>="http://tempuri.org/HelloWorld"</span><span> style</span><span>="document"</span><span>/></span><span><span>wsdl:input</span><span>></span><span><span>soap:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:input</span><span>></span><span><span>wsdl:output</span><span>></span><span><span>soap:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:output</span><span>></span><span></span><span>wsdl:operation</span><span>></span><span><span>wsdl:operation </span><span>name</span><span>="GetArray"</span><span>></span><span><span>soap:operation </span><span>soapAction</span><span>="http://tempuri.org/GetArray"</span><span> style</span><span>="document"</span><span>/></span><span><span>wsdl:input</span><span>></span><span><span>soap:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:input</span><span>></span><span><span>wsdl:output</span><span>></span><span><span>soap:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:output</span><span>></span><span></span><span>wsdl:operation</span><span>></span><span></span><span>wsdl:binding</span><span>></span><span><span>wsdl:binding </span><span>name</span><span>="TestWebServiceSoap12"</span><span> type</span><span>="tns:TestWebServiceSoap"</span><span>></span><span><span>soap12:binding </span><span>transport</span><span>="http://schemas.xmlsoap.org/soap/http"</span><span>/></span><span><span>wsdl:operation </span><span>name</span><span>="HelloWorld"</span><span>></span><span><span>soap12:operation </span><span>soapAction</span><span>="http://tempuri.org/HelloWorld"</span><span> style</span><span>="document"</span><span>/></span><span><span>wsdl:input</span><span>></span><span><span>soap12:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:input</span><span>></span><span><span>wsdl:output</span><span>></span><span><span>soap12:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:output</span><span>></span><span></span><span>wsdl:operation</span><span>></span><span><span>wsdl:operation </span><span>name</span><span>="GetArray"</span><span>></span><span><span>soap12:operation </span><span>soapAction</span><span>="http://tempuri.org/GetArray"</span><span> style</span><span>="document"</span><span>/></span><span><span>wsdl:input</span><span>></span><span><span>soap12:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:input</span><span>></span><span><span>wsdl:output</span><span>></span><span><span>soap12:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:output</span><span>></span><span></span><span>wsdl:operation</span><span>></span><span></span><span>wsdl:binding</span><span>></span><span><span>wsdl:service </span><span>name</span><span>="TestWebService"</span><span>></span><span><span>wsdl:port </span><span>name</span><span>="TestWebServiceSoap"</span><span> binding</span><span>="tns:TestWebServiceSoap"</span><span>></span><span><span>soap:address </span><span>location</span><span>="http://localhost:63463/TestWebService.asmx"</span><span>/></span><span></span><span>wsdl:port</span><span>></span><span></span><span>wsdl:service</span><span>></span><span></span><span>wsdl:definitions</span><span>></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
登入後複製

View Code

把 http://localhost:63463/TestWebService.asmx 修改為你最終要訪問的網址,我的是1.5/wsdl/006/TestWebService.php

一個php建立webservice,並透過c#呼叫的真實實例三、c#呼叫php的webservice一個php建立webservice,並透過c#呼叫的真實實例

1、新網站-ASP.NET網站;2、解決方案資源管理器

-專案新增器

-專案(右鍵)- Web引用,在URL中輸入:

http://192.168.1.5/wsdl/006/TestWebService.php?wsdl

會顯示如下視窗:

點擊[新增引用] Default.aspx.cs程式碼為:

<span>using</span><span> System;
</span><span>using</span><span> System.Collections.Generic;
</span><span>using</span><span> System.Linq;
</span><span>using</span><span> System.Web;
</span><span>using</span><span> System.Web.UI;
</span><span>using</span><span> System.Web.UI.WebControls;

</span><span>public</span><span>partial</span><span>class</span><span> _Default : System.Web.UI.Page
{
    </span><span>protected</span><span>void</span> Page_Load(<span>object</span><span> sender, EventArgs e)
    {
        </span><span>//</span><span>初始化WebService  </span>        WebReference.TestWebService srv = <span>new</span><span> WebReference.TestWebService();
        </span><span>//</span><span>调第一个方法  </span><span>string</span> str =<span> srv.HelloWorld();
        </span><span>//</span><span>调第二个方法  </span><span>string</span>[] arry = srv.GetArray(<span>"</span><span>string1</span><span>"</span>, <span>"</span><span>string2</span><span>"</span><span>);
        Response.Write(str);
    }
}</span>
登入後複製

View Code

一個php建立webservice,並透過c#呼叫的真實實例4、CTRL+F5運行,看到最上面出現“welcome to dongzi world”,表示成功調用!

 

以上就介紹了一個php建立webservice,並透過c#呼叫的真實實例,包含了面向的內容,希望對PHP教學有興趣的朋友有幫助。 一個php建立webservice,並透過c#呼叫的真實實例

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板