php soap執行個體講解

來源:互聯網
上載者:User

標籤:des   style   blog   http   java   color   

php soap執行個體講解 

一,什麼是soap,什麼是wsdl,為什麼要用他們

SOAP是基於XML和HTTP通訊協定,xml各種平台,各種語言都支援的一個種語言。http呢它得到了所有的網際網路瀏覽器及伺服器的支援。

WSDL 指網路服務描述語言 (Web Services Description Language),是一種使用 XML 編寫的文檔。這種文檔可描述某個 Web service。它可規定服務的位置,以及此服務提供的操作。

我是做php的,你是java的,他是做.net,如果我們三個之間要進行通訊,要進行資料交換,怎麼辦呢?我們需要一個能和我們都能通訊的工具。soap,wsdl被創造出來,使得運行在不同的作業系統並使用不同的技術和程式設計語言的應用程式可以互相進行通訊。

二,執行個體

如果php要使用soap的話,通常做法是,添加了一下php的soap模組,在php.ini裡面加上soap.so,下面介紹一個不要添加soap.so檔案,也可以實現soap的方法

nusoap是php寫的一個功能檔案,包涵進來就可以用了,網上很多自己去搜一下吧。

1,不使用wsdl

a,服務端helloworld2.php

    <?php      //包函nusoap.php      require_once(‘./lib/nusoap.php‘);            //建立服務端      $server = new soap_server;            //定義用戶端調用方法      $server->register(‘hello‘);            //調用方法以及參數      function hello($name) {       return ‘Hello, ‘ . $name;      }            $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ‘‘;      $server->service($HTTP_RAW_POST_DATA);      ?>  

b,用戶端hello.php

    <?php      //包函nusoap.php      require_once(‘./lib/nusoap.php‘);      //建立一個soap用戶端,調用服務端提供的wsdl      //$client = new soapclient(‘http://localhost/test/hellowsdl2.php?wsdl‘, true);      $client = new soapclient(‘http://localhost/test/helloworld2.php‘);      //查看一下是不是報錯      $err = $client->getError();      if ($err) {       //顯示錯誤       echo ‘<h2>Constructor error</h2><pre>‘ . $err . ‘</pre>‘;      }            //調用服務端的方法      $result = $client->call(‘hello‘, array(‘person‘ => "this is a test"));            echo ‘<h2>Result</h2><pre>‘;      print_r($result);      echo ‘</pre>‘;      ?>  

2,使用wsld

a,伺服器端

    <?php      //包函nusoap.php      require_once(‘./lib/nusoap.php‘);      //建立一個soap服務      $server = new soap_server();      //初始化支援wsdl      $server->configureWSDL(‘hellowsdl2‘, ‘urn:hellowsdl2‘);      //定義資料結構來接收資料      $server->wsdl->addComplexType(       ‘Person‘,       ‘complexType‘,       ‘struct‘,       ‘all‘,       ‘‘,       array(       ‘firstname‘ => array(‘name‘ => ‘firstname‘, ‘type‘ => ‘xsd:string‘),//後面的type定義資料的類型,這個是string       ‘age‘ => array(‘name‘ => ‘age‘, ‘type‘ => ‘xsd:int‘),//後面的type定義資料的類型,這個是int       ‘gender‘ => array(‘name‘ => ‘gender‘, ‘type‘ => ‘xsd:string‘)//後面的type定義資料的類型,這個是string       )      );      $server->wsdl->addComplexType(       ‘SweepstakesGreeting‘,       ‘complexType‘,       ‘struct‘,       ‘all‘,       ‘‘,       array(       ‘greeting‘ => array(‘name‘ => ‘greeting‘, ‘type‘ => ‘xsd:string‘),       ‘winner‘ => array(‘name‘ => ‘winner‘, ‘type‘ => ‘xsd:string‘)       )      );      //伺服器定義的soap調用方法      $server->register(‘hello‘,                    // 方法名字hello,方法就在下面       array(‘person‘ => ‘tns:Person‘),          // 用戶端傳來的變數       array(‘return‘ => ‘tns:SweepstakesGreeting‘),    //返回參數       ‘urn:hellowsdl2‘,                         // soap名       ‘urn:hellowsdl2#hello‘,                   // soap的方法名       ‘rpc‘,                                    // 使用的方式       ‘encoded‘,                                // 編碼       ‘test‘                                    // 存檔      );      //定義上面註冊過的函數hello      function hello($person) {       $greeting = ‘Hello, ‘ . $person[‘firstname‘] .       ‘. It is nice to meet a ‘ . $person[‘age‘] .       ‘ year old ‘ . $person[‘gender‘] . ‘.‘;             $winner =  ‘Scott‘;      //要返回的資料       return array(       ‘greeting‘ => $greeting,       ‘winner‘ => $winner       );      }      // 請求時(試圖)調用服務      $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ‘‘;      $server->service($HTTP_RAW_POST_DATA);      ?>  

b,用戶端

<?php  //包函nusoap.php  require_once(‘./lib/nusoap.php‘);  //建立一個soap用戶端,調用服務端提供的wsdl  //$client = new soapclient(‘http://localhost/test/hellowsdl2.php?wsdl‘, true);  $client = new soapclient(‘http://localhost/test/helloworld2.php‘);  //查看一下是不是報錯  $err = $client->getError();  if ($err) {   //顯示錯誤   echo ‘<h2>Constructor error</h2><pre>‘ . $err . ‘</pre>‘;  }  //要向服務端要傳的參數  $person = array(‘firstname‘ => ‘Willi‘, ‘age‘ => 22, ‘gender‘ => ‘male‘);    //調用服務端的方法  $result = $client->call(‘hello‘, array(‘person‘ => $person));  //錯誤審核  if ($client->fault) {   echo ‘<h2>Fault</h2><pre>‘;   print_r($result);   echo ‘</pre>‘;  } else {   $err = $client->getError();   if ($err) {   echo ‘<h2>Error</h2><pre>‘ . $err . ‘</pre>‘;   } else {   echo ‘<h2>Result</h2><pre>‘;   print_r($result);   echo ‘</pre>‘;   }  }  //顯示請求資訊  echo ‘<h2>Request</h2>‘;  echo ‘<pre>‘ . htmlspecialchars($client->request, ENT_QUOTES) . ‘</pre>‘;  //顯示返回資訊  echo ‘<h2>Response</h2>‘;  echo ‘<pre>‘ . htmlspecialchars($client->response, ENT_QUOTES) . ‘</pre>‘;  //顯示調試資訊  echo ‘<h2>Debug</h2>‘;  echo ‘<pre>‘ . htmlspecialchars($client->debug_str, ENT_QUOTES) . ‘</pre>‘;  ?> 

上面二個例子不管是用戶端,還是伺服器端,都是用php寫的,你可以試著用多種語言來寫,來測試一下。不管你是用php的模組,還是用nusoap,裡面具體方法就不在這多說了,手冊裡面都有。

聯繫我們

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