在PHP中利用wsdl建立標準webservice的實現代碼

來源:互聯網
上載者:User

1、建立wsdl
說明:
A、非標準的webservice,可能只能PHP才能訪問
B、標準的webservice,就必須要使用wsdl(webservice description language,就是用XML文法標準來描述你的服務內容,我是這麼理解的)
在這裡我只介紹標準的webservice。
那麼如何建立wsdl呢?對於PHP來說這確實是件很不容易的事情,有人說用zend studio建立很方便,這是一種方法。但對於那些不喜歡用zend studio的人來說,會覺得建立一個webservice還要安裝zend studio,太強人所難了,我就是,嘿嘿。
在這裡我介紹一個簡單的方法,到網上下載SoapDiscovery.class.php類,裡面有個公用方法:getWSDL,這個方法末尾是用的return,那麼,你修改一下這個方法,我是這麼做的:
//return sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>');
//產生wsdl檔案,將上面的return注釋
$fso = fopen($this->class_name . ".wsdl" , "w");
fwrite($fso, sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>'));
現在產生wsdl的類有了,SoapDiscovery.class.php★。

我只要再準備一個提供服務的類或者函數就可以建立wsdl了
比如我有個類:person,檔案名稱為:person.class.php★,裡面有兩個方法,一個是say,一個是run。很簡單。 複製代碼 代碼如下:<?php
class person
{
public function say()
{
return("i'm speaking.");
}
public function run()
{
return("i'm running,don't disturb me please.");
}
}
?>

到這裡有兩個類了:SoapDiscovery.class.php和person.class.php。
開始正式產生wsdl:
建立檔案server.php。將以下內容拷貝進去,運行即可產生一個person.wsdl檔案 複製代碼 代碼如下:<?php
include("person.class.php");
include("SoapDiscovery.class.php");

$disco = new SoapDiscovery('person','Person');//第一個參數是類名(產生的wsdl檔案就是以它來命名的),即person類,第二個參數是服務的名字(這個可以隨便寫)。
$disco->getWSDL();
?>

2、建立webservice服務端程式
將server.php檔案的內容清空,複製以下代碼進去: 複製代碼 代碼如下:<?php
include("person.class.php");
$objSoapServer = new SoapServer("person.wsdl");//person.wsdl是剛建立的wsdl檔案
//$objSoapServer = new SoapServer("server.php?wsdl");//這樣也行
$objSoapServer->setClass("person");//註冊person類的所有方法
$objSoapServer->handle();//處理請求
?>

3、建立webservice用戶端程式,測試webservice是否有效,檔案名稱是:client.php
將以下內容拷貝進去 複製代碼 代碼如下:<?php
$client = new SoapClient("person.wsdl");
//$client = new SoapClient("server.php?wsdl");//這樣也行
echo($client->say());
echo "<br />";
echo($client->run());
echo "<br />";
?>

OK,結束。很簡單吧?
.NET如果要使用的話,你只要提供一個url給他就行了。
獲得url的方法:你可以先到person.wsdl檔案裡面尋找<soap:address location="http://xxxxxxxxxxxxxxxxxxxx/server.php" />,這裡的url(具體url是根據你的目錄確定的)就是你要提供給.NET開發人員使用的。不過別高興太早,後面要加:“?wsdl”,http://xxxxxxxxxxxxxxxxxxxx/server.php?wsdl這樣才是對的,不信你可以將url拷貝到瀏覽器的地址欄裡看下就知道了。
.NET開發人員獲得你給他的url之後,就可以在自己的項目裡面添加一個服務引用或者web引用了,然後就可以根據提示完成相關操作,對於使用.NET的開發人員來說很簡單的。

在這裡我只介紹標準的webservice
一、 建立WSDL
1。網上下載SoapDiscovery.class.php類
2。修改SoapDiscovery.class.php的公用方法getWsdl(),讓其自動產生wsdl檔案(注意存放路徑),這裡只是建立一個wsdl模型 複製代碼 代碼如下://return sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>');
//產生wsdl檔案,將上面的return注釋
$fso = fopen($this->class_name . ".wsdl" , "w");
fwrite($fso, sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>'));
exit;

3。提供服務的類或者函數 複製代碼 代碼如下://比如我有個類:person,檔案名稱為:person.class.php★,裡面有兩個方法,一個是say,一個是run。很簡單。
<?php
class person
{
public function say()
{
return("i'm speaking.");
}
public function run()
{
return("i'm running,don't disturb me please.");
}
}
?>

4。開始正式產生wsdl:
建立檔案server.php。將以下內容拷貝進去,運行即可產生一個person.wsdl檔案 複製代碼 代碼如下:<?php
include("person.class.php");
include("SoapDiscovery.class.php");
//第一個參數是類名(產生的wsdl檔案就是以它來命名的),即person類,第二個參數是服務的名字(這個可以隨便寫)。
$disco = new SoapDiscovery('person','Person');
$disco->getWSDL();
?>

5。建立webservice服務端程式
將server.php檔案的內容清空,複製以下代碼進去: 複製代碼 代碼如下:<?php
include("person.class.php");
$objSoapServer = new SoapServer("person.wsdl");//person.wsdl是剛建立的wsdl檔案
//$objSoapServer = new SoapServer("server.php?wsdl");//這樣也行
$objSoapServer->setClass("person");//註冊person類的所有方法
$objSoapServer->handle();//處理請求
?>

6。建立webservice用戶端程式,測試webservice是否有效,檔案名稱是:client.php 複製代碼 代碼如下:<?php
$client = new SoapClient("person.wsdl");
//$client = new SoapClient("server.php?wsdl");//這樣也行
echo($client->say());
echo "<br />";
echo($client->run());
echo "<br />";
?>

7。.NET如果要使用的話,你只要提供一個url給他就行了。
獲得url的方法:你可以先到person.wsdl檔案裡面尋找<soap:address location="http://xxxxxxxxxxxxxxxxxxxx/server.php" />,這裡的url(具體url是根據你的目錄確定的)就是你要提供給.NET開發人員使用的。不過別高興太早,後面要加:“?wsdl”,http://xxxxxxxxxxxxxxxxxxxx/server.php?wsdl這樣才是對的,不信你可以將url拷貝到瀏覽器的地址欄裡看下就知道了。
.NET開發人員獲得你給他的url之後,就可以在自己的項目裡面添加一個服務引用或者web引用了,然後就可以根據提示完成相關操作,對於使用.NET的開發人員來說很簡單的。

(1)建立一網站,建立一個web引用,輸入url

(2)實力調用 複製代碼 代碼如下:protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
sdaf.Solsoft_HelloWorld ddd = new sdaf.Solsoft_HelloWorld();
Label1.Text = ddd.say();
}
}

測試代碼http://xiazai.jb51.net/201112/yuanma/CreateSoap.rar

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.