使用pear::soap建立web服務的步驟

WBOY
發布: 2016-07-25 09:11:06
原創
1124 人瀏覽過
複製代碼
  1. class FruitQuoteService
  2. {
  3. public $__dispatch_map = array();
  4. public $__type = array();
  5. public $__type defy(
  6. public function FruitQuoteService()
  7. {
  8. $this->__dispatch_map['getQuote'] = array(
  9. "in" => array("category" => "string") ,
  10. "out" => array("quote" => "int")
  11. );
  12. $this->__dispatch_map['getFruit'] = array(
  13. "in" => array(),
  14. "out" => array("fruitSummary" => "{urn:FruitQuoteService}fruitStruct")
  15. );
  16. $this->__typedef ['fruitStruct'] = array(
  17. $this->__typedef ['fruitStruct'] = array(
  18. '類別'=>'字串', '金額' => 'int'
  19. );
  20. }
  21. public function getQuote($category)
  22. {
  23. switch ($category)
  24. {
  25. case 'apple': 中斷;
  26. case '橘色':
  27. $quote = 12;
  28. 中斷;
  29. case '香蕉':
  30. $quote = 20;
  31. 中斷;
  32. 預設:
  33. $quote = 0;
  34. break;
  35. }
  36. return $quote;
  37. }//函數結束
  38. public function getFruit()
  39. {
  40. $list = array(
  41. array("蘋果", 100),
  42. array("柳橙", 500),
  43. array("香蕉", 260)
  44. );
  45. return $list;
  46. }//結束函數
  47. }//結束類別
  48. ?>
複製程式碼

第二步:建立伺服器.php 這個程式將會接收並處理客戶端的請求

  1. require_once("FruitQuoteService.php");
  2. require_once("SOAP/Server.php");
  3. $fruitQuote = new FruitQuoteService();
  4. $server = new Soap_Server();
  5. $server->addObjectMap($fruitQuote, "http://www.xxx.com");
  6. if (isset ($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD']=='POST')
  7. {
  8. $server->service($GLOBALS['HTTP_RAW_POST_DATA']);
  9. } else
  10. {
  11. require_once 'SOAP/Disco.php';
  12. $disco = new SOAP_DISCO_Server($server,'FruitQuoteService');
  13. header("內容類型:text/xml" );
  14. if (isset($_SERVER['QUERY_STRING']) && strcasecmp($_SERVER['QUERY_STRING'],'wsdl')==0) {
  15. echo $disco->getWSDL();
  16. } else {
  17. echo $disco->getDISCO();
  18. }
  19. }
  20. 退出;
  21. ?>
複製代碼

現在可以透過http://www.shangyong.com/ws/server.php?wsdl查看wsdl文件。

DISCO:一項微軟用於發布和發現 Web 服務的技術,定義了一個從給定的 url 獲取 Web 服務描述的簡單的 HTTP GET 機制

第三步:建立web服務客戶端程式碼

  1. require_once('SOAP/Client.php');
  2. //該名稱空間必須和server.php定義的一致
  3. $options = array('namespace' => 'http://www.xxx.com',
  4. 'trace' => 1); //為1可以表示透過__get_wire取得soap訊息,預設為0
  5. $client = new SOAP_client("http://www.shangyong.com/ws/server.php");
  6. $params = array();
  7. $response = $client->call("getFruit", $params, $options);
  8. //print_r($client->__get_wire()); // 輸出soap訊息
  9. if (PEAR::isError($response)) {
  10. echo 'Error: ' . $response->getMessage() 。 "
    n";
  11. } else {
  12. print_r($response) 。 "n";
  13. }
  14. $params = array("name" => "orange");
  15. $response = $client->call("getQuote", $params, $options) ;
  16. if (PEAR::isError($response)) {
  17. echo '錯誤:' 。 $response->getMessage() 。 。 "nn";
  18. }
  19. 退出;
  20. ?>
  21. 複製程式碼

客戶端程式碼2

  1. require_once('SOAP/Client.php');
  2. /**
  3. * 所有的服務內容,如:命名空間、UEL, 參數名稱等都可以從wsdl檔案取得
  4. */
  5. $ wsdl = new SOAP_WSDL("http://www.shangyong.com/ws/server.php?wsdl");
  6. /**
  7. * 從wsdl產生一個proxy對象,這個對象包含wsdl文件中定義的所有操作的方法。
  8. * 可以透過proxy物件直接呼叫函數
  9. * 優點:易於使用者使用
  10. */
  11. $client = $wsdl->getProxy( );
  12. $response = $client->getQuote("apple");
  13. if (PEAR::isError($response)) {
  14. echo '錯誤: ' . $response->getMessage() 。 "
    n";
  15. } else {
  16. echo $response . "nn";
  17. }
  18. $response = $client->getFruit();
  19. if (PEAR:: isError($response)) {
  20. echo '錯誤:' 。 $response->getMessage() 。 "
    n";
  21. } else {
  22. print_r($response) . "n";
  23. }
  24. 退出;
  25. ?>
複製;?>


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!