Zend_Rest_Server + Zend_Rest_Client, переменная не читается - PullRequest
0 голосов
/ 20 апреля 2009

Я пытаюсь передать $ 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 не читается.

1 Ответ

0 голосов
/ 24 апреля 2009

Источник: 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:

?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();
}

будет работать!

...