Home >Backend Development >PHP Tutorial >Use Zend_Rest_Server for Web Service architecture

Use Zend_Rest_Server for Web Service architecture

WBOY
WBOYOriginal
2016-07-25 09:11:061102browse
  1. $base_url = 'http://bbs.it-home.org/';

  2. $client = new Zend_Rest_Client($base_url);
  3. // Set global variables

  4. $headers = array();

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

  6. // Remote call, call sayHello interface, pass two string parameters

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

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

  9. echo "
    ";</li>
    <li> print_r(json_decode($response, 1));</li>
    <li> echo "
    ";
  10. } else {
  11. echo $response;
  12. }
Copy code

Server side:

  1. // Capture request header information

  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. / /Interface processing class: (Lijiabeibei_Blog)

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

  10. function __construct() {

  11. }

  12. / **

  13. * Test interface
  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. }

Copy code

If you want to set up multiple service classes, you can do this : $server = new Ylili_Rest_Server(); $server->setClass('Lijiabeibei_Blog'); $server->setClass('Lijiabeibei_Shop); Note that the method naming of multiple server classes should not conflict.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn