一個php創建webservice,並通過c#調用的真實實例


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

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

本文目錄如下

一、php創建webservice

二、生成.wsdl文件

三、c#調用php的webservice

一、php創建webservice

1、php環境

我用的是windows的apache,php.ini文件中 extension=php_soap.dll 去掉注釋即可,前提是已經安裝了 php_soap.dll。

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

<?php  
class TestWebService  
{  
    public function HelloWorld()  
    {  
        return array("HelloWorldResult"=>"welcome to dongzi world");  
    }  
 
    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();  
?>
View Code

 

二、生成.wsdl文件

本來我想簡單一點,用SoapDiscovery.class.php來生成,可是總是出現各種奇葩問題;迫不得已安裝了Zend Studio12.5,可是還是出現另外的奇葩問題;最終我找到一個可行的方案,是用vs2010來生成,C#調用沒有問題。

1、打開vs2010,新建項目-ASP.NET空Web應用程序;

2、解決方案資源管理器-項目(右鍵)-添加-新建項-web服務,名稱修改為TestWebService.asmx,代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebApplication1
{
    /// <summary>
    /// TestWebService 的摘要說明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消對下行的注釋。
    // [System.Web.Script.Services.ScriptService]
    public class TestWebService : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "";
        }

        [WebMethod]
        public string[] GetArray(string a,string b)
        {
            return null;
        }
    }
}
View Code

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

3、F5運行,在彈出的IE瀏覽器中復制運行的url地址

http://localhost:63463/TestWebService.asmx

打開一個新的IE瀏覽器窗口,粘貼並修改為,然后回車,此時窗口會展示wsdl文件的xml格式內容:

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

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

代碼如下:

<?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="a" type="s:string" />
            <s:element minOccurs="0" maxOccurs="1" name="b" 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:63463/TestWebService.asmx" />
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>
View Code

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

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

 

三、c#調用php的webservice

1、新建網站-ASP.NET網站;

2、解決方案資源管理器-項目(右鍵)-添加Web引用,在URL中輸入:

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

會顯示出如下窗口:

點擊[添加引用]

3、修改Default.aspx.cs代碼為:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //初始化WebService  
        WebReference.TestWebService srv = new WebReference.TestWebService();
        //調第一個方法  
        string str = srv.HelloWorld();
        //調第二個方法  
        string[] arry = srv.GetArray("string1", "string2");
        Response.Write(str);
    }
}
View Code

4、CTRL+F5運行,看到最上面出現“welcome to dongzi world”,表示成功調用!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM