使用Zend_Rest_Server做Web Service架構

來源:互聯網
上載者:User
  1. $base_url = 'http://bbs.it-home.org/';

  2. $client = new Zend_Rest_Client($base_url);
  3. // 設定全域變數

  4. $headers = array();

  5. $client->headers($headers);

  6. // 遠程調用, 調用sayHello介面,傳遞兩個字串參數

  7. $response = $client->sayHello('Hello', 'world!')->get();

  8. if ($this->_request->getParam('debug')) {

  9. echo "
    ";
  10. print_r(json_decode($response, 1));
  11. echo "
  12. ";
  13. } else {
  14. echo $response;
  15. }
複製代碼

伺服器端:

  1. // 捕獲要求標頭資訊

  2. $headers = $this->_request->getParam('headers');
  3. Zend_Registry::set('headers', $headers);
  4. $server = new Zend_Rest_Server();
  5. $server->setClass('Lijiabeibei_Blog');
  6. $server->handle();

  7. //介面處理類:(Lijiabeibei_Blog)

  8. class Lijiabeibei_Blog {
  9. protected $retval = array();

  10. function __construct() {

  11. }

  12. /**

  13. * 測試介面
  14. * @param string $a
  15. * @param string $b
  16. */
  17. function sayHello($a, $b) {
  18. if (!$this->retval['success']) {
  19. $this->retval['debug']= print_r(
  20. Zend_Registry::get('headers'), 1
  21. );
  22. return json_encode($this->retval);
  23. }
  24. $this->retval['debug'] = $a .' '.$b;

  25. return json_encode($this->retval);

  26. }
  27. }

複製代碼

如果要設定多個服務類,可以這樣:$server = new Ylili_Rest_Server();$server->setClass('Lijiabeibei_Blog');$server->setClass('Lijiabeibei_Shop);注意,多個伺服器類的方法命名不要衝突。

  • 聯繫我們

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