在PHP中利用wsdl創(chuàng)建標(biāo)準(zhǔn)webservice的實(shí)現(xiàn)代碼
更新時(shí)間:2011年12月07日 23:17:26 作者:
網(wǎng)上有現(xiàn)成的nusoap,我沒(méi)使用,如果使用了,我可能就不知道PHP是怎么創(chuàng)建webservice的了
1、創(chuàng)建wsdl
說(shuō)明:
A、非標(biāo)準(zhǔn)的webservice,可能只能PHP才能訪(fǎng)問(wèn)
B、標(biāo)準(zhǔn)的webservice,就必須要使用wsdl(webservice description language,就是用XML語(yǔ)法標(biāo)準(zhǔn)來(lái)描述你的服務(wù)內(nèi)容,我是這么理解的)
在這里我只介紹標(biāo)準(zhǔn)的webservice。
那么如何創(chuàng)建wsdl呢?對(duì)于PHP來(lái)說(shuō)這確實(shí)是件很不容易的事情,有人說(shuō)用zend studio創(chuàng)建很方便,這是一種方法。但對(duì)于那些不喜歡用zend studio的人來(lái)說(shuō),會(huì)覺(jué)得創(chuàng)建一個(gè)webservice還要安裝zend studio,太強(qiáng)人所難了,我就是,嘿嘿。
在這里我介紹一個(gè)簡(jiǎn)單的方法,到網(wǎng)上下載SoapDiscovery.class.php類(lèi),里面有個(gè)公用方法:getWSDL,這個(gè)方法末尾是用的return,那么,你修改一下這個(gè)方法,我是這么做的:
//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的類(lèi)有了,SoapDiscovery.class.php★。
我只要再準(zhǔn)備一個(gè)提供服務(wù)的類(lèi)或者函數(shù)就可以創(chuàng)建wsdl了
比如我有個(gè)類(lèi):person,文件名為:person.class.php★,里面有兩個(gè)方法,一個(gè)是say,一個(gè)是run。很簡(jiǎn)單。
<?php
class person
{
public function say()
{
return("i'm speaking.");
}
public function run()
{
return("i'm running,don't disturb me please.");
}
}
?>
到這里有兩個(gè)類(lèi)了:SoapDiscovery.class.php和person.class.php。
開(kāi)始正式生成wsdl:
創(chuàng)建文件server.php。將以下內(nèi)容拷貝進(jìn)去,運(yùn)行即可生成一個(gè)person.wsdl文件
<?php
include("person.class.php");
include("SoapDiscovery.class.php");
$disco = new SoapDiscovery('person','Person');//第一個(gè)參數(shù)是類(lèi)名(生成的wsdl文件就是以它來(lái)命名的),即person類(lèi),第二個(gè)參數(shù)是服務(wù)的名字(這個(gè)可以隨便寫(xiě))。
$disco->getWSDL();
?>
2、創(chuàng)建webservice服務(wù)端程序
將server.php文件的內(nèi)容清空,復(fù)制以下代碼進(jìn)去:
<?php
include("person.class.php");
$objSoapServer = new SoapServer("person.wsdl");//person.wsdl是剛創(chuàng)建的wsdl文件
//$objSoapServer = new SoapServer("server.php?wsdl");//這樣也行
$objSoapServer->setClass("person");//注冊(cè)person類(lèi)的所有方法
$objSoapServer->handle();//處理請(qǐng)求
?>
3、創(chuàng)建webservice客戶(hù)端程序,測(cè)試webservice是否有效,文件名是:client.php
將以下內(nèi)容拷貝進(jìn)去
<?php
$client = new SoapClient("person.wsdl");
//$client = new SoapClient("server.php?wsdl");//這樣也行
echo($client->say());
echo "<br />";
echo($client->run());
echo "<br />";
?>
OK,結(jié)束。很簡(jiǎn)單吧?
.NET如果要使用的話(huà),你只要提供一個(gè)url給他就行了。
獲得url的方法:你可以先到person.wsdl文件里面查找<soap:address location="http://xxxxxxxxxxxxxxxxxxxx/server.php" />,這里的url(具體url是根據(jù)你的目錄確定的)就是你要提供給.NET開(kāi)發(fā)人員使用的。不過(guò)別高興太早,后面要加:“?wsdl”,http://xxxxxxxxxxxxxxxxxxxx/server.php?wsdl這樣才是對(duì)的,不信你可以將url拷貝到瀏覽器的地址欄里看下就知道了。
.NET開(kāi)發(fā)人員獲得你給他的url之后,就可以在自己的項(xiàng)目里面添加一個(gè)服務(wù)引用或者web引用了,然后就可以根據(jù)提示完成相關(guān)操作,對(duì)于使用.NET的開(kāi)發(fā)人員來(lái)說(shuō)很簡(jiǎn)單的。
在這里我只介紹標(biāo)準(zhǔn)的webservice
一、 創(chuàng)建WSDL
1。網(wǎng)上下載SoapDiscovery.class.php類(lèi)
2。修改SoapDiscovery.class.php的公共方法getWsdl(),讓其自動(dòng)生成wsdl文件(注意存放路徑),這里只是創(chuàng)建一個(gè)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。提供服務(wù)的類(lèi)或者函數(shù)
//比如我有個(gè)類(lèi):person,文件名為:person.class.php★,里面有兩個(gè)方法,一個(gè)是say,一個(gè)是run。很簡(jiǎn)單。
<?php
class person
{
public function say()
{
return("i'm speaking.");
}
public function run()
{
return("i'm running,don't disturb me please.");
}
}
?>
4。開(kāi)始正式生成wsdl:
創(chuàng)建文件server.php。將以下內(nèi)容拷貝進(jìn)去,運(yùn)行即可生成一個(gè)person.wsdl文件
<?php
include("person.class.php");
include("SoapDiscovery.class.php");
//第一個(gè)參數(shù)是類(lèi)名(生成的wsdl文件就是以它來(lái)命名的),即person類(lèi),第二個(gè)參數(shù)是服務(wù)的名字(這個(gè)可以隨便寫(xiě))。
$disco = new SoapDiscovery('person','Person');
$disco->getWSDL();
?>
5。創(chuàng)建webservice服務(wù)端程序
將server.php文件的內(nèi)容清空,復(fù)制以下代碼進(jìn)去:
<?php
include("person.class.php");
$objSoapServer = new SoapServer("person.wsdl");//person.wsdl是剛創(chuàng)建的wsdl文件
//$objSoapServer = new SoapServer("server.php?wsdl");//這樣也行
$objSoapServer->setClass("person");//注冊(cè)person類(lèi)的所有方法
$objSoapServer->handle();//處理請(qǐng)求
?>
6。創(chuàng)建webservice客戶(hù)端程序,測(cè)試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如果要使用的話(huà),你只要提供一個(gè)url給他就行了。
獲得url的方法:你可以先到person.wsdl文件里面查找<soap:address location="http://xxxxxxxxxxxxxxxxxxxx/server.php" />,這里的url(具體url是根據(jù)你的目錄確定的)就是你要提供給.NET開(kāi)發(fā)人員使用的。不過(guò)別高興太早,后面要加:“?wsdl”,http://xxxxxxxxxxxxxxxxxxxx/server.php?wsdl這樣才是對(duì)的,不信你可以將url拷貝到瀏覽器的地址欄里看下就知道了。
.NET開(kāi)發(fā)人員獲得你給他的url之后,就可以在自己的項(xiàng)目里面添加一個(gè)服務(wù)引用或者web引用了,然后就可以根據(jù)提示完成相關(guān)操作,對(duì)于使用.NET的開(kāi)發(fā)人員來(lái)說(shuō)很簡(jiǎn)單的。
(1)創(chuàng)建一網(wǎng)站,創(chuàng)建一個(gè)web引用,輸入url
(2)實(shí)力調(diào)用
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
sdaf.Solsoft_HelloWorld ddd = new sdaf.Solsoft_HelloWorld();
Label1.Text = ddd.say();
}
}
測(cè)試代碼http://xiazai.jb51.net/201112/yuanma/CreateSoap.rar
說(shuō)明:
A、非標(biāo)準(zhǔn)的webservice,可能只能PHP才能訪(fǎng)問(wèn)
B、標(biāo)準(zhǔn)的webservice,就必須要使用wsdl(webservice description language,就是用XML語(yǔ)法標(biāo)準(zhǔn)來(lái)描述你的服務(wù)內(nèi)容,我是這么理解的)
在這里我只介紹標(biāo)準(zhǔn)的webservice。
那么如何創(chuàng)建wsdl呢?對(duì)于PHP來(lái)說(shuō)這確實(shí)是件很不容易的事情,有人說(shuō)用zend studio創(chuàng)建很方便,這是一種方法。但對(duì)于那些不喜歡用zend studio的人來(lái)說(shuō),會(huì)覺(jué)得創(chuàng)建一個(gè)webservice還要安裝zend studio,太強(qiáng)人所難了,我就是,嘿嘿。
在這里我介紹一個(gè)簡(jiǎn)單的方法,到網(wǎng)上下載SoapDiscovery.class.php類(lèi),里面有個(gè)公用方法:getWSDL,這個(gè)方法末尾是用的return,那么,你修改一下這個(gè)方法,我是這么做的:
//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的類(lèi)有了,SoapDiscovery.class.php★。
我只要再準(zhǔn)備一個(gè)提供服務(wù)的類(lèi)或者函數(shù)就可以創(chuàng)建wsdl了
比如我有個(gè)類(lèi):person,文件名為:person.class.php★,里面有兩個(gè)方法,一個(gè)是say,一個(gè)是run。很簡(jiǎn)單。
復(fù)制代碼 代碼如下:
<?php
class person
{
public function say()
{
return("i'm speaking.");
}
public function run()
{
return("i'm running,don't disturb me please.");
}
}
?>
到這里有兩個(gè)類(lèi)了:SoapDiscovery.class.php和person.class.php。
開(kāi)始正式生成wsdl:
創(chuàng)建文件server.php。將以下內(nèi)容拷貝進(jìn)去,運(yùn)行即可生成一個(gè)person.wsdl文件
復(fù)制代碼 代碼如下:
<?php
include("person.class.php");
include("SoapDiscovery.class.php");
$disco = new SoapDiscovery('person','Person');//第一個(gè)參數(shù)是類(lèi)名(生成的wsdl文件就是以它來(lái)命名的),即person類(lèi),第二個(gè)參數(shù)是服務(wù)的名字(這個(gè)可以隨便寫(xiě))。
$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");//注冊(cè)person類(lèi)的所有方法
$objSoapServer->handle();//處理請(qǐng)求
?>
3、創(chuàng)建webservice客戶(hù)端程序,測(cè)試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é)束。很簡(jiǎn)單吧?
.NET如果要使用的話(huà),你只要提供一個(gè)url給他就行了。
獲得url的方法:你可以先到person.wsdl文件里面查找<soap:address location="http://xxxxxxxxxxxxxxxxxxxx/server.php" />,這里的url(具體url是根據(jù)你的目錄確定的)就是你要提供給.NET開(kāi)發(fā)人員使用的。不過(guò)別高興太早,后面要加:“?wsdl”,http://xxxxxxxxxxxxxxxxxxxx/server.php?wsdl這樣才是對(duì)的,不信你可以將url拷貝到瀏覽器的地址欄里看下就知道了。
.NET開(kāi)發(fā)人員獲得你給他的url之后,就可以在自己的項(xiàng)目里面添加一個(gè)服務(wù)引用或者web引用了,然后就可以根據(jù)提示完成相關(guān)操作,對(duì)于使用.NET的開(kāi)發(fā)人員來(lái)說(shuō)很簡(jiǎn)單的。
在這里我只介紹標(biāo)準(zhǔn)的webservice
一、 創(chuàng)建WSDL
1。網(wǎng)上下載SoapDiscovery.class.php類(lèi)
2。修改SoapDiscovery.class.php的公共方法getWsdl(),讓其自動(dòng)生成wsdl文件(注意存放路徑),這里只是創(chuàng)建一個(gè)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ù)的類(lèi)或者函數(shù)
復(fù)制代碼 代碼如下:
//比如我有個(gè)類(lèi):person,文件名為:person.class.php★,里面有兩個(gè)方法,一個(gè)是say,一個(gè)是run。很簡(jiǎn)單。
<?php
class person
{
public function say()
{
return("i'm speaking.");
}
public function run()
{
return("i'm running,don't disturb me please.");
}
}
?>
4。開(kāi)始正式生成wsdl:
創(chuàng)建文件server.php。將以下內(nèi)容拷貝進(jìn)去,運(yùn)行即可生成一個(gè)person.wsdl文件
復(fù)制代碼 代碼如下:
<?php
include("person.class.php");
include("SoapDiscovery.class.php");
//第一個(gè)參數(shù)是類(lèi)名(生成的wsdl文件就是以它來(lái)命名的),即person類(lèi),第二個(gè)參數(shù)是服務(wù)的名字(這個(gè)可以隨便寫(xiě))。
$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");//注冊(cè)person類(lèi)的所有方法
$objSoapServer->handle();//處理請(qǐng)求
?>
6。創(chuàng)建webservice客戶(hù)端程序,測(cè)試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如果要使用的話(huà),你只要提供一個(gè)url給他就行了。
獲得url的方法:你可以先到person.wsdl文件里面查找<soap:address location="http://xxxxxxxxxxxxxxxxxxxx/server.php" />,這里的url(具體url是根據(jù)你的目錄確定的)就是你要提供給.NET開(kāi)發(fā)人員使用的。不過(guò)別高興太早,后面要加:“?wsdl”,http://xxxxxxxxxxxxxxxxxxxx/server.php?wsdl這樣才是對(duì)的,不信你可以將url拷貝到瀏覽器的地址欄里看下就知道了。
.NET開(kāi)發(fā)人員獲得你給他的url之后,就可以在自己的項(xiàng)目里面添加一個(gè)服務(wù)引用或者web引用了,然后就可以根據(jù)提示完成相關(guān)操作,對(duì)于使用.NET的開(kāi)發(fā)人員來(lái)說(shuō)很簡(jiǎn)單的。
(1)創(chuàng)建一網(wǎng)站,創(chuàng)建一個(gè)web引用,輸入url
(2)實(shí)力調(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();
}
}
測(cè)試代碼http://xiazai.jb51.net/201112/yuanma/CreateSoap.rar
相關(guān)文章
單臺(tái)服務(wù)器的PHP進(jìn)程之間實(shí)現(xiàn)共享內(nèi)存的方法
這篇文章主要介紹了單臺(tái)服務(wù)器的PHP進(jìn)程之間實(shí)現(xiàn)共享內(nèi)存的方法,需要的朋友可以參考下2014-06-06
php 5.6版本中編寫(xiě)一個(gè)PHP擴(kuò)展的簡(jiǎn)單示例
這篇文章主要介紹了php 5.6版本中編寫(xiě)一個(gè)PHP擴(kuò)展的簡(jiǎn)單示例,本文給出擴(kuò)展實(shí)現(xiàn)代碼、編譯方法、配置方法和使用例子等內(nèi)容,需要的朋友可以參考下2015-01-01
PHP函數(shù)microtime()用法與說(shuō)明
這篇文章主要介紹了PHP函數(shù)microtime()用法與說(shuō)明,有需要的朋友可以參考一下2013-12-12
php實(shí)現(xiàn)將base64格式圖片保存在指定目錄的方法
這篇文章主要介紹了php實(shí)現(xiàn)將base64格式圖片保存在指定目錄的方法,涉及php針對(duì)圖片文件的傳輸、判定及轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2016-10-10

