SOAP擴充PHP輕鬆實現WebService

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   使用   

最近在一個PHP項目中對接外部介面涉及到WebService,搜尋引擎上相關文章不是很多,找到的大都是引用一個號稱很強大的開源軟體 NuSOAP(:http://sourceforge.net/projects/nusoap/),即一些類。文章寫描述的環境是PHP 4.3,現在都流行PHP 5.2或PHP 5.3了。先拿來試試,運行出錯,原來NuSOAP提供的soapclient類與PHP 5中新增了內建的SOAP擴充的SoapClient類衝突了。

雖然NuSOAP號稱可以用於所有的PHP 環境,不受伺服器安全設定的影響。但需要引用一大堆類檔案,還是覺得用PHP 5中新增了內建的SOAP擴充好一些,能實現實用就好。先瞭解一下SOAP:

一、SOAP和XML-PRC比較

在Web服務發展的初期,XML格式化訊息的第一個主要用途是,應用於XML-RPC協議,其中RPC代表遠端程序呼叫。在XML遠端程序呼叫(XML-RPC)中,用戶端發送一條特定訊息,該訊息中必須包括名稱、運行服務的程式以及輸入參數。

XML-RPC只能使用有限的資料類型種類和一些簡單的資料結構。人們認為這個協議還不夠強大,於是就出現了SOAP——其最初的定義是簡單對象訪問協 議。之後,大家逐漸意識到SOAP其實並不簡單,而且也不需要必須使用物件導向語言,所以,現在人們只是沿用SOAP這個名稱而已。

XML-RPC只有簡單的資料類型集,取而代之,SOAP是通過利用XML Schema的不斷髮展來定義資料類型的。同時,SOAP也能夠利用XML 命名空間,這是XML-RPC所不需要的。如此一來,SOAP訊息的開頭部分就可以是任何類型的XML命名空間聲明,其代價是在系統之間增加了更多的複雜 性和不相容性。

隨著電腦行業的覺醒,人們發現了基於XML的Web服務的商業潛力,於是,各家公司開始不斷地發掘想法、觀點、論據以及標準化嘗試。W3C曾經設法以 “Web服務活動”的名義來組織成果展,其中也包括實際做出SOAP的XML協議工作群組(XML Protocol Working Group)。與Web服務有關的標準化成果(從某種程度上說與SOAP相關或者依賴於SOAP)的數量已經倍增了到了令人驚訝的程度。

最初,SOAP是作為XML-RPC的擴充而發展起來的,它主要強調的是,通過從WSDL檔案中所獲得的方法和變數名來進行遠端程序呼叫。現在,通過不斷 進步,人們發現了更多的使用SOAP的方式,而不僅僅是採用“檔案”方式——基本上是使用一個SOAP信封來傳送XML格式檔案。無論如何,要掌握 SOAP,瞭解WSDL所扮演的角色是最根本的。

二、SOAP資料包結構解析

SOAP的訊息被稱為一個SOAP Envelope,包括SOAP Header和SOAP Body。其中,SOAP Header可以方便的插入各種其它訊息來擴充Web Service的功能,比如Security(採用認證訪問Web Service),SOAP Body則是具體的訊息本文,也就是Marshall後的資訊。 

SOAP調用的時候,也就是向一個URL(比如 http://api.google.com/search/beta2 )發送HTTP Post報文(根據SOAP規範,HTTP Get報文也可被支援),調用方法的名字在HTTP Request Header SOAP-Action中給出,接下來就是SOAP Envelope了。服務端接到請求,執行計算,將返回結果Marshall成XML,用HTTP返回給用戶端。 

三、SOAP簡單樣本

SOAP開發一般有三種方式選擇:

1)、PEAR內建的SOAP擴充;

2)、PHP內建的SOAP擴充;

3)、NuSOAP(純PHP) 。

PHP 5中新增了內建的SOAP擴充,作為PHP的一部分提供的,因此不需要下載、安裝和管理單獨的包。這是第一個用C而不是PHP為PHP編寫的SOAP實 現,因此作者聲稱它的速度要快得多。相關文檔包含在PHP手冊的Function Reference部分(php_soap.dll)。 

一個訪問.NET WEB服務的用戶端例子:

< ? php$objSoapClient = new SoapClient("http://www.webservicemart.com/uszip.asmx?WSDL");$param = array("ZipCode"=>‘12209‘); $out = $objSoapClient->ValidateZip($param);$data = $out->ValidateZipResult;echo $data;?>

四、執行個體

1)、用PHP建立SOAP服務

建立soap_server.php(虛擬路徑為:http://localhost/php/soap/soap_server.php)

< ? php/*** A simple math utility class*/class math{    /**    * Add two integers together    *    * @param integer $a The first integer of the addition    * @param integer $b The second integer of the addition    * @return integer The sum of the provided integers    */    public function add($a, $b){        return $a + $b;    }    /**    * Subtract two integers from each other    *    * @param integer $a The first integer of the subtraction    * @param integer $b The second integer of the subtraction    * @return integer The difference of the provided integers    */    public function sub($a, $b){        return $a - $b;    }    /**    * Div two integers from each other    *    * @param integer $a The first integer of the subtraction    * @param integer $b The second integer of the subtraction    * @return double The difference of the provided integers    */    public function div($a, $b){        if($b == 0){            throw new SoapFault(-1, "Cannot divide by zero!");        }        return $a / $b;    }}$server = new SoapServer(‘math.wsdl‘, array(‘soap_version‘=>SOAP_1_2));$server->setClass("math");$server->handle(); ?>

註:

a)、math類是即將公開的webservice;

b)、$server->setClass,不是$server->addClass。

2)、用PHP用戶端訪問剛建立SOAP服務

< ? php// $client = new SoapClient(‘http://localhost/php/soap/math.wsdl‘);$client = new SoapClient("http://localhost/php/soap/soap_server.php?WSDL");try{    $result = $client->div(8, 2); // will cause a Soap Fault if divide by zero    print "The answer is: $result";}catch(SoapFault $e){    print "Sorry an error was caught executing your request: {$e->getMessage()}";}?>

本質上,http://localhost/php/soap/soap_server.php?WSDL就是要訪問到注釋行所指的wsdl描述檔案,所 以這個WSDL檔案必須事先產生。而對於其他語言如Java則可以動態產生。對於PHP內建的SOAP擴充要求這個WSDL檔案必須事先產生好。

可以用ZendStudio產生靜態WSDL檔案,此時用到math類的phpdoc作為產生WSDL的中繼資料。用ZendStudio產生wsdl檔案時,必須正確說明Web服務目標地址,片斷如下:

...    <service name="mathService">        <port binding="typens:mathBinding" name="mathPort">            <soap:address location="http://localhost/php/soap/soap_server.php"></soap:address>        </port>    </service>...

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.