|
說明:
A、非標(biāo)準(zhǔn)的webservice,可能只能php才能訪問
B、標(biāo)準(zhǔn)的webservice,就必須要使用wsdl(webservice description language,就是用XML語法標(biāo)準(zhǔn)來描述你的服務(wù)內(nèi)容,我是這么理解的)
在這里我只介紹標(biāo)準(zhǔn)的webservice。
那么如何創(chuàng)建wsdl呢?對于php來說這確實是件很不容易的事情,有人說用zend studio創(chuàng)建很方便,這是一種方法。但對于那些不喜歡用zend studio的人來說,會覺得創(chuàng)建一個webservice還要安裝zend studio,太強(qiáng)人所難了,我就是,嘿嘿。
在這里我介紹一個簡單的方法,到網(wǎng)上下載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>'));
現(xiàn)在生成wsdl的類有了,SoapDiscovery.class.php★。
我只要再準(zhǔn)備一個提供服務(wù)的類或者函數(shù)就可以創(chuàng)建wsdl了
比如我有個類:person,文件名為:person.class.php★,里面有兩個方法,一個是say,一個是run。很簡單。
復(fù)制代碼 代碼如下:
<?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:
創(chuàng)建文件server.php。將以下內(nèi)容拷貝進(jìn)去,運行即可生成一個person.wsdl文件
復(fù)制代碼 代碼如下:
<?php
include("person.class.php");
include("SoapDiscovery.class.php");
$disco = new SoapDiscovery('person','Person');//第一個參數(shù)是類名(生成的wsdl文件就是以它來命名的),即person類,第二個參數(shù)是服務(wù)的名字(這個可以隨便寫)。
$disco->getWSDL();
?>
2、創(chuàng)建webservice服務(wù)端程序
將server.php文件的內(nèi)容清空,復(fù)制以下代碼進(jìn)去:
復(fù)制代碼 代碼如下:
<?php
include("person.class.php");
$objSoapServer = new SoapServer("person.wsdl");//person.wsdl是剛創(chuàng)建的wsdl文件
//$objSoapServer = new SoapServer("server.php?wsdl");//這樣也行
$objSoapServer->setClass("person");//注冊person類的所有方法
$objSoapServer->handle();//處理請求
?>
3、創(chuàng)建webservice客戶端程序,測試webservice是否有效,文件名是:client.php
將以下內(nèi)容拷貝進(jìn)去
復(fù)制代碼 代碼如下:
<?php
$client = new SoapClient("person.wsdl");
//$client = new SoapClient("server.php?wsdl");//這樣也行
echo($client->say());
echo "<br />";
echo($client->run());
echo "<br />";
?>
OK,結(jié)束。很簡單吧?
.NET如果要使用的話,你只要提供一個url給他就行了。
獲得url的方法:你可以先到person.wsdl文件里面查找<soap:address location="http://xxxxxxxxxxxxxxxxxxxx/server.php" />,這里的url(具體url是根據(jù)你的目錄確定的)就是你要提供給.NET開發(fā)人員使用的。不過別高興太早,后面要加:“?wsdl”,http://xxxxxxxxxxxxxxxxxxxx/server.php?wsdl這樣才是對的,不信你可以將url拷貝到瀏覽器的地址欄里看下就知道了。
.NET開發(fā)人員獲得你給他的url之后,就可以在自己的項目里面添加一個服務(wù)引用或者web引用了,然后就可以根據(jù)提示完成相關(guān)操作,對于使用.NET的開發(fā)人員來說很簡單的。
在這里我只介紹標(biāo)準(zhǔn)的webservice
一、 創(chuàng)建WSDL
1。網(wǎng)上下載SoapDiscovery.class.php類
2。修改SoapDiscovery.class.php的公共方法getWsdl(),讓其自動生成wsdl文件(注意存放路徑),這里只是創(chuàng)建一個wsdl模型
復(fù)制代碼 代碼如下:
//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。提供服務(wù)的類或者函數(shù)
復(fù)制代碼 代碼如下:
//比如我有個類: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:
創(chuàng)建文件server.php。將以下內(nèi)容拷貝進(jìn)去,運行即可生成一個person.wsdl文件
復(fù)制代碼 代碼如下:
<?php
include("person.class.php");
include("SoapDiscovery.class.php");
//第一個參數(shù)是類名(生成的wsdl文件就是以它來命名的),即person類,第二個參數(shù)是服務(wù)的名字(這個可以隨便寫)。
$disco = new SoapDiscovery('person','Person');
$disco->getWSDL();
?>
5。創(chuàng)建webservice服務(wù)端程序
將server.php文件的內(nèi)容清空,復(fù)制以下代碼進(jìn)去:
復(fù)制代碼 代碼如下:
<?php
include("person.class.php");
$objSoapServer = new SoapServer("person.wsdl");//person.wsdl是剛創(chuàng)建的wsdl文件
//$objSoapServer = new SoapServer("server.php?wsdl");//這樣也行
$objSoapServer->setClass("person");//注冊person類的所有方法
$objSoapServer->handle();//處理請求
?>
6。創(chuàng)建webservice客戶端程序,測試webservice是否有效,文件名是:client.php
復(fù)制代碼 代碼如下:
<?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是根據(jù)你的目錄確定的)就是你要提供給.NET開發(fā)人員使用的。不過別高興太早,后面要加:“?wsdl”,http://xxxxxxxxxxxxxxxxxxxx/server.php?wsdl這樣才是對的,不信你可以將url拷貝到瀏覽器的地址欄里看下就知道了。
.NET開發(fā)人員獲得你給他的url之后,就可以在自己的項目里面添加一個服務(wù)引用或者web引用了,然后就可以根據(jù)提示完成相關(guān)操作,對于使用.NET的開發(fā)人員來說很簡單的。
(1)創(chuàng)建一網(wǎng)站,創(chuàng)建一個web引用,輸入url
(2)實力調(diào)用
復(fù)制代碼 代碼如下:
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
php技術(shù):在PHP中利用wsdl創(chuàng)建標(biāo)準(zhǔn)webservice的實現(xiàn)代碼,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。