WSDL
WSDL(網路服務描述語言,Web Services Description Language)是一門基於 XML 的語言,用於描述 Web Services 以及如何對它們進行訪問。這種文檔可描述某個 Web service。它可規定服務的位置,以及此服務提供的操作(或方法)。
一個 WSDL 文檔的主要結構是類似這樣的:
definition of types........ definition of a message.... definition of a port....... definition of a binding....
WSDL 文檔可包含其它的元素,比如 extension 元素,以及一個 service 元素,此元素可把若干個 web services 的定義組合在一個單一的 WSDL 文檔中。
PHP產生WSDL
類代碼(SoapDiscovery.class.php):
class_name = $class_name; $this->service_name = $service_name; } /** * SoapDiscovery::getWSDL() Returns the WSDL of a class if the class is instantiable. * * @return string **/ public function getWSDL() { if (empty($this->service_name)) { throw new Exception('No service name.'); } $headerWSDL = "n"; $headerWSDL.= "n"; $headerWSDL.= "n"; if (empty($this->class_name)) { throw new Exception('No class name.'); } $class = new ReflectionClass($this->class_name); if (!$class->isInstantiable()) { throw new Exception('Class is not instantiable.'); } $methods = $class->getMethods(); $portTypeWSDL = ''; $bindingWSDL = 'nn"; $serviceWSDL = 'nnservice_name.'Port" binding="tns:'.$this->service_name."Binding">nnn"; $messageWSDL = ''; foreach ($methods as $method) { if ($method->isPublic() && !$method->isConstructor()) { $portTypeWSDL.= 'n".'ngetName()."Response" />nn"; $bindingWSDL.= 'n".'nnnnnnn"; $messageWSDL.= 'n"; $parameters = $method->getParameters(); foreach ($parameters as $parameter) { $messageWSDL.= 'n"; } $messageWSDL.= "n"; $messageWSDL.= 'n"; $messageWSDL.= 'n"; $messageWSDL.= "n"; } } $portTypeWSDL.= "n"; $bindingWSDL.= "n"; return sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, ''); } /** * SoapDiscovery::getDiscovery() Returns discovery of WSDL. * * @return string **/ public function getDiscovery() { return "nnn"; }} ?>
使用方法(服務端server.php):
getWSDL(); file_put_contents(WSDL_URL,$str);} //SOAP開啟並接收Client傳入的參數響應 $server = new SoapServer(WSDL_URL);$server->setClass('HelloWorld');$server->handle(); //測試定義公開的類class HelloWorld { private $nombre = ''; public function __construct($name = 'World') {$this->name = $name;} public function greet($name = '') {$name = $name?$name:$this->name;return 'Hello '.$name.'.';} public function serverTimestamp() {return time();} }?>
用戶端client.php:
greet('ieliwb'); var_dump($result); echo "The answer isresult";}catch (SoapFault $f){ echo "Error Message: {$f->getMessage()}";}?>
建立 Webservice
1. 建立wsdl
- 非標準的webservice,可能只能PHP才能訪問
- 標準的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, '');//產生wsdl檔案,將上面的return注釋$fso = fopen($this->class_name . ".wsdl" , "w");fwrite($fso, sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, ''));
現在產生wsdl的類有了,SoapDiscovery.class.php。
我只要再準備一個提供服務的類或者函數就可以建立wsdl了。比如我有個類:person,檔案名稱為:person.class.php★,裡面有兩個方法,一個是say,一個是run。很簡單。
到這裡有兩個類了:SoapDiscovery.class.php和person.class.php。
開始正式產生wsdl:建立檔案server.php。將以下內容拷貝進去,運行即可產生一個person.wsdl檔案
getWSDL();?>
2. 建立webservice服務端程式
將server.php檔案的內容清空,複製以下代碼進去:
setClass("person");//註冊person類的所有方法 $objSoapServer->handle();//處理請求?>
3. 建立webservice用戶端程式,測試webservice是否有效,檔案名稱是:client.php
say()); echo "
"; echo($client->run()); echo "
";?>
OK,結束。.NET如果要使用的話,你只要提供一個url給他就行了。
獲得url的方法:你可以先到person.wsdl檔案裡面尋找,這裡的url(具體url是根據你的目錄確定的)就是你要提供給.NET開發人員使用的。不過別高興太早,後面要加:“?wsdl”,http://xxxxxxxxxxxxxxxxxxxx/server.php?wsdl這樣才是對的,不信你可以將url拷貝到瀏覽器的地址欄裡看下就知道了。
.NET開發人員獲得你給他的url之後,就可以在自己的項目裡面添加一個服務引用或者web引用了,然後就可以根據提示完成相關操作,對於使用.NET的開發人員來說很簡單的。
http://www.bkjia.com/PHPjc/752376.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/752376.htmlTechArticleWSDL WSDL(網路服務描述語言,Web Services Description Language)是一門基於 XML 的語言,用於描述 Web Services 以及如何對它們進行訪問。這種文檔可...