|
|||
|
I'm finding Zend_Rest_Server a little limited. I'm having trouble doing the following things.
Anything beyond a basic return requires writing out all the XML. For instance I can't figure how to give my elements attributes, have multiple elements with the same name like 'book'. Most of the things I try return elements with the name 'key_0', 'key_1', 'key_2'. Also how can I make my functions have optional parameters. I register a class 'Tracks' for a playlist or tracks. For each method I want an optional parameter for each method. I set the parameter in the method to the default option, but i am still required to use them in the querystring or Zend_Rest_Client call. |
|
||||
|
Quote:
Quote:
If you haven't looked at the manual carefully, I suggest you do Zend Framework
__________________
Zym Framework - A Zend Framework extension library w/ demo app SpotSec Blog: http://spotsec.com/blog |
|
|||
|
Thanks for the reply!
I don't have the exact code in front of me because I'm no longer at work, but I'm pretty sure I can recall the scenario. My function was returning an array. What I wanted to was use each key in the array as a 'track' element in my XML. PHP Code:
Code:
<key_0>Track1</key_0> <key_1>Track2</key_1> <key_2>Track3</key_2> As for the optional parameters I got the idea from flickrs API. See this page: Flickr Services: Flickr API: flickr.activity.userComments or Flickr Services: Flickr API: flickr.interestingness.getList For instance we have some tracks that are considered promotional tracks. A user may want to reserve their search to just these promotional tracks, or all tracks. By default I'd like it to be all tracks so it'd be nice if when a parameter is optional in a function that it could also be optional in the URI parameters. Code:
public function getTracks($genreId, $promo=false) Code:
http://api.domain.com/v1/catalog/getTracks?genreId=1&promo=true Code:
http://api.domain.com/v1/catalog/getTracks?genreId=1 |
|
||||
|
To get the array format you wanted I am not exactly sure, but it should like
PHP Code:
Optional parameters are usually up to the server, generally most of the time I have not seen it implemented. Zend_Rest unfortunately does not support optional parameters, but you can easily change that by extending it. PHP Code:
__________________
Zym Framework - A Zend Framework extension library w/ demo app SpotSec Blog: http://spotsec.com/blog |
|
|||
|
One solution I have discovered about passing arrays with Zend_Rest is ENCODING de array before sending in the Zend_Rest_Server and DECODING it when receiving from the client. I am doing this with the Zend_Json_Encoder::encode and Zend_Json_Decoder::decode.
After doing this the client received the objects the same way the server sent it, but I encounter other problems. In my case I had problems with some spanish special characters (encoding), but I solved this by using recursive functions that encodes/decodes in utf8 every position of the array (see the php function array_walk). |
|
||||
|
o_0 that is all I have to say
__________________
Zym Framework - A Zend Framework extension library w/ demo app SpotSec Blog: http://spotsec.com/blog |
|
|||
|
Here's what I did to work around the issue with returning arrays. I put the lines I added/changed in red bold.
Code:
class JoshuaBeall_Rest_Server extends Zend_Rest_Server {
/**
* Recursively iterate through a struct
*
* Recursively iterates through an associative array or object's properties
* to build XML response.
*
* @param mixed $struct
* @param DOMDocument $dom
* @param DOMElement $parent
* @return void
*/
protected function _structValue($struct, DOMDocument $dom, DOMElement $parent)
{
$struct = (array) $struct;
foreach ($struct as $key => $value) {
if ($value === false) {
$value = 0;
} elseif ($value === true) {
$value = 1;
}
if (ctype_digit((string) $key) || !ctype_alnum($key)) {
$index = $key;
$key = 'data';
}
if (is_array($value) || is_object($value)) {
$element = $dom->createElement($key);
$this->_structValue($value, $dom, $element);
} else {
$element = $dom->createElement($key);
$element->appendChild($dom->createTextNode($value));
}
if(isset($index))
$element->setAttribute('index',$index);
$parent->appendChild($element);
}
}
}
$server = new JoshuaBeall_Rest_Server(); $server->setClass('JoshuaBeall_WebApi_MainApi'); $server->handle(); Hope that helps! |
![]() |
| Thread Tools | |
| Display Modes | |
|
|