應用NuSoap構建新型的基於PHP的Web服務

來源:互聯網
上載者:User

標籤:des   style   http   color   os   使用   io   strong   檔案   

一個例子便能說明一切,讓我們先看一個例子

為了說明如何應用nusoap和php來構建web services,我們將舉一個簡單的例子。這個例子應用程式由一個php web services的伺服器端和用戶端組成。他將實現兩個功能:顛倒一個字串字元的順序,求兩個數的和。

php soap伺服器
用php和nusoap來建立soap伺服器非常容易。基本上,你只要寫出你想要暴露給你的web services的函數,然後用nusoap去註冊它們就可以了。

ok,另外還需要兩步才能完成php soap伺服器的建立。首先你還要在你的php代碼中建立nusoap對象的一個執行個體,然後用http post方法將未經處理資料傳給nusoap進行處理。聽起來簡單吧。先看看清單1。

soapfunc.php

<?require_once(nusoap.php); function reverse($str){   $retval = "";   if(strlen($str) < 1) {    return new soap_fault(client,,invalid string);    }   for ($i = 1; $i <= strlen($str); $i++) {      $retval .= $str[(strlen($str) - $i)];    }  return $retval; }function add2numbers($num1, $num2) {   if (trim($num1) != intval($num1)) {      return new soap_fault(client, , the   first number is invalid);   }  if (trim($num2) != intval($num2)) {    return new soap_fault(client, , the second number is invalid);   }  return ($num1 + $num2); }?>

soapfunc.php的源檔案。這個檔案包含了我們想通過soap協議暴露給web services的兩個函數:reverse和add2numbers,它們是我們這個web services 應用的核心。函數reverse帶一個參數,顛倒字串中的字元的順序,然後返回。


soapserver.php

<?require_once(nusoap.php);include(soapfunc.php);$soap = new soap_server;$soap->register(reverse);$soap->register(add2numbers);$soap->service($http_raw_post_data);?>

nusoap註冊函數和調用soap處理常式的用法。你看到了,註冊你的兩個函數(在soapfunc.php中)、傳遞post資料給 soap_server對象,就跟建立一個新的soap_server對象執行個體一樣的簡單。soap_server對象將要檢查post資料並確定將調用 哪個函數,然後向這個php函數傳遞參數。從php函數返回的資料被重新封裝為soap響應並傳送給請求服務的soap用戶端。

php soap用戶端
現在我們已經利用nusoap和php建立了一個soap伺服器,我們需要對它進行測試。像我們建立soap伺服器程式一樣,我們可以在php中利用nusoap再建立一個soap的用戶端程式。清單3給出了php soap用戶端程式的來源程式。

現在我們已經利用nusoap和php建立了一個soap伺服器,我們需要對它進行測試。像我們建立soap伺服器程式一樣,我們可以在php中利用nusoap再建立一個soap的用戶端程式。清單3給出了php soap用戶端程式的來源程式。

soapclient.php

<?include(nusoap.php);$client = new soapclient(http://localhost/soapserver.php);$str = "this string will be reversed";$params1 = array(str=>$str);$reversed = $client->call(reverse,$params1);echo "if you reverse $str, you get $reversed<br>\n";$n1 = 5;$n2 = 14;$params2 = array(num1=>$n1, num2=>$n2);$added = $client->call(add2numbers, $params2);echo "if you add $n1 and $n2 you get $added<br>\n";?>

為了在一個soap伺服器上能使用php用戶端程式,你還得做三件事情。第一,你要建立一個soapclient對象。soapclient對象負責處理 參數的編組和soap協議。soapclient需要一個必須是url的參數。這個url可以指向一個實際soap伺服器的http終端或者一個wsdl 描述。在我們這個例子裡,它是指向我們的php soap伺服器的一個url。

當調用需要參數的函數時,你需要先建立一個參數數組,數組包括一組鍵-值(key-value)對。鍵(key)是參數的名字,值(value)是參數的值。

當需要調用函數時,你就用soapclient對象去調用,並且傳入兩個參數。第一個參數是你要調用的函數的名字,第二個參數是包含soap函數所帶參數的一個數組,主調函數將會返回你調用的soap函數的值。

要運行這個例子,只要簡單的在web瀏覽器中輸入soapclient.php的url。你將類似以下的輸出:

if you reverse this string will be reversed, you get desrever eb lliw gnirts siht if you add 5 and 14 you get 1Array 

應用NuSoap構建新型的基於PHP的Web服務

聯繫我們

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