【问题标题】:Zend_Rest_Server + Zend_Rest_Client, variable not readZend_Rest_Server + Zend_Rest_Client,变量未读取
【发布时间】:2009-04-20 09:35:06
【问题描述】:

我想要做的是将 $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 是不会被读取的。

【问题讨论】:

  • 我想你错过了一些代码。 getByShortname 的 id 来自哪里?不需要是 $shortname 吗?
  • 我已将 getByEmployee($id) 编辑为 getByEmployeeByShortname($shortname)... 其中 $shortname 在函数中传递

标签: zend-framework zend-rest


【解决方案1】:

来源:http://framework.zend.com/manual/en/zend.rest.client.html 第 44.2.3 节。请求参数

$client = new Zend_Rest_Client('http://example.org/rest');

$client->arg('value1');
$client->arg2('value2');
$client->get();

// or

$client->arg('value1')->arg2('value2')->get();

上面例子中的两个方法,都会得到如下的get args:

?method=arg&arg1=value1&arg=value1&arg2=value2

你会注意到第一次调用

$client->arg('value1');

两者都有

method=arg&arg1=value1 and arg=value1;

这样 Zend_Rest_Server 可以正确理解请求,而不需要预先存在的服务知识。

因此:

$rest = new Zend_Rest_Client('http://example.org/api/emp');
$rest->getById(); //this was not here before. This made it work!
$rest->id('1124921');
$rest->apikey('1234');
$result = $rest->get();
if($result->status()=='success') {
    echo $result->emp_id() .' '. $result->emp_shortname().' '. $result->status();
} else {
    echo $result->response().' '.$result->status();
}

会工作的!

【讨论】:

    猜你喜欢
    • 2012-05-21
    • 2013-03-07
    • 2019-04-24
    • 1970-01-01
    • 2019-11-30
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    • 2018-08-05
    相关资源
    最近更新 更多