Zend_Rest_Server + Zend_Rest_Client,变量未读取
我想做的是将 $rest->apikey 传递给我的 Zend_Rest_Server。 empAction 创建 Zend_Rest_Server 所需的数组。 但在 getByShortname($id,$apikey) 中,我无法读取 $apikey。 他们查询检查 API 密钥没有返回任何结果。
$rest = new Zend_Rest_Client('http://localhsot/api/emp');
$rest->method('getByShortname');
$rest->id('1124921');
$rest->apikey('1234');
$result = $rest->get();
var_dump($result); //should work
//---------------------------------------
//For Emp method--> api/emp
//---------------------------------------
//... rest of code ...
public function empAction()
{
require_once 'EmprestServer.php';
$params = $this->_getAllParams();
unset($params['controller']);
unset($params['action']);
unset($params['module']);
$param_keys = array_keys($params);
if($param_keys[0]=='id') {
$request = array('method' => 'getById');
} else if($param_keys[0]=='shortname') {
$request = array('method' => 'getByShortname');
}
foreach($param_keys AS $key) {
$request[$key] = $filter_params[$key]; //need to filter key
//need better checking
if(!$request[$key]) {
throw new Exception($request[$key].' contained invalid data.');
}
}
/*
I am able to generate this array using the code prior to this line...
$request = array();
$request['method'] = 'getByShortname';
$request['shortname'] = 'wdelrosa';
$request['apikey'] = '1234';
*/
$server = new Zend_Rest_Server();
$server->setClass('EmprestServer');
$server->handle($request);
}
//... rest of code ...
//---------------------------------------
//The Class
//---------------------------------------
class EmprestServer
{
public function getByShortname($shortname,$apikey)
{
$emp = new Employee();
$data = array();
/** PROBLEM **/
/** I can't access $apikey WHY? Any ideas? **/
if(!$this->checkKey($apikey)) {
throw new Exception('Key is invalid.');
}
if(!$data = $emp->getEmployeeByShortname($shortname)) throw new Exception('Employee ID not found.');
$data = $data->toArray();
return $data;
}
}
更新:这似乎有效。 我用这个得到了有效的 XML 输出
http://locahost/api/emp/shortname/wdelrosa/apikey/1234
但是如果我使用上面的 Zend_Rest_Client,则不会读取 apikey。
What i am trying to do is pass $rest->apikey to my Zend_Rest_Server.
The empAction creates the array needed by my Zend_Rest_Server. But in getByShortname($id,$apikey), I cannot read the $apikey. They query to check the API key does not return any results.
$rest = new Zend_Rest_Client('http://localhsot/api/emp');
$rest->method('getByShortname');
$rest->id('1124921');
$rest->apikey('1234');
$result = $rest->get();
var_dump($result); //should work
//---------------------------------------
//For Emp method--> api/emp
//---------------------------------------
//... rest of code ...
public function empAction()
{
require_once 'EmprestServer.php';
$params = $this->_getAllParams();
unset($params['controller']);
unset($params['action']);
unset($params['module']);
$param_keys = array_keys($params);
if($param_keys[0]=='id') {
$request = array('method' => 'getById');
} else if($param_keys[0]=='shortname') {
$request = array('method' => 'getByShortname');
}
foreach($param_keys AS $key) {
$request[$key] = $filter_params[$key]; //need to filter key
//need better checking
if(!$request[$key]) {
throw new Exception($request[$key].' contained invalid data.');
}
}
/*
I am able to generate this array using the code prior to this line...
$request = array();
$request['method'] = 'getByShortname';
$request['shortname'] = 'wdelrosa';
$request['apikey'] = '1234';
*/
$server = new Zend_Rest_Server();
$server->setClass('EmprestServer');
$server->handle($request);
}
//... rest of code ...
//---------------------------------------
//The Class
//---------------------------------------
class EmprestServer
{
public function getByShortname($shortname,$apikey)
{
$emp = new Employee();
$data = array();
/** PROBLEM **/
/** I can't access $apikey WHY? Any ideas? **/
if(!$this->checkKey($apikey)) {
throw new Exception('Key is invalid.');
}
if(!$data = $emp->getEmployeeByShortname($shortname)) throw new Exception('Employee ID not found.');
$data = $data->toArray();
return $data;
}
}
UPDATE: this seems to work. I get a valid XML output with this
http://locahost/api/emp/shortname/wdelrosa/apikey/1234
But if i use the Zend_Rest_Client above, the apikey is not read.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来源: http://framework.zend.com/manual/en/ zend.rest.client.html 第 44.2.3 节。 请求
参数 上例中的两个方法都会产生以下 get args:
您会注意到,第一次调用
会产生
这两个参数,这是为了让 Zend_Rest_Server 能够正确理解请求,而不是需要预先存在的知识服务。
因此:
会起作用!
Source: http://framework.zend.com/manual/en/zend.rest.client.html Section 44.2.3. Request Arguments
Both of the methods in the example above, will result in the following get args:
You will notice that the first call of
resulted in both
this is so that Zend_Rest_Server can understand the request properly, rather than requiring pre-existing knowledge of the service.
Therefore:
would work!