当前位置:Gxlcms > PHP教程 > 使用Zend_Rest_Server做WebService架构

使用Zend_Rest_Server做WebService架构

时间:2021-07-01 10:21:17 帮助过:2人阅读

  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); 注意,多个服务器类的方法命名不要冲突。

人气教程排行