$base_url = 'http://bbs.it-home.org/'; - $client = new Zend_Rest_Client($base_url);
- // 设置全局变量
$headers = array(); $client->headers($headers); // 远程调用, 调用sayHello接口,传递两个字符串参数 - $response = $client->sayHello('Hello', 'world!')->get();
if ($this->_request->getParam('debug')) { - echo "
"; - print_r(json_decode($response, 1));
- echo "";
- } else {
- echo $response;
- }
-
服务器端:
// 捕获请求头信息 - $headers = $this->_request->getParam('headers');
- Zend_Registry::set('headers', $headers);
-
- $server = new Zend_Rest_Server();
- $server->setClass('Lijiabeibei_Blog');
- $server->handle();
//接口处理类:(Lijiabeibei_Blog) - class Lijiabeibei_Blog {
- protected $retval = array();
function __construct() { -
- }
/** - * 测试接口
- * @param string $a
- * @param string $b
- */
- function sayHello($a, $b) {
- if (!$this->retval['success']) {
- $this->retval['debug']= print_r(
- Zend_Registry::get('headers'), 1
- );
- return json_encode($this->retval);
- }
- $this->retval['debug'] = $a .' '.$b;
return json_encode($this->retval); - }
- }
如果要设置多个服务类,可以这样:
$server = new Ylili_Rest_Server();
$server->setClass('Lijiabeibei_Blog');
$server->setClass('Lijiabeibei_Shop);
注意,多个服务器类的方法命名不要冲突。 |