Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-20-2007, 01:08 AM
Junior Member
 
Join Date: Mar 2007
Posts: 2
Default Some Zend_Rest_Server issues I'm having

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-21-2007, 12:49 AM
SpotSec's Avatar
Senior Member
 
Join Date: Feb 2007
Location: United States
Posts: 121
Default

Quote:
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'.
Are you trying to return an array? Or are you trying to parse the result?


Quote:
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.
If I remember correctly (dont quote me, its been a while) optional parameters are not supported by rest, soap, xmlrpc etc...

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-21-2007, 05:46 AM
Junior Member
 
Join Date: Mar 2007
Posts: 2
Default

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:
function getTracks()  {
    return array(
'Track1''Track2''Track3');

The XML would return something like this obviously:

Code:
<key_0>Track1</key_0>
<key_1>Track2</key_1>
<key_2>Track3</key_2>
I've played around with it a few different ways and haven't found a solution other than just using simpleXML.


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)
could work as:

Code:
http://api.domain.com/v1/catalog/getTracks?genreId=1&promo=true
or

Code:
http://api.domain.com/v1/catalog/getTracks?genreId=1
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-21-2007, 11:05 PM
SpotSec's Avatar
Senior Member
 
Join Date: Feb 2007
Location: United States
Posts: 121
Default

To get the array format you wanted I am not exactly sure, but it should like
PHP Code:
return array('Track' => array('track1'
                                           
'track2'
                                           
'track3')); 

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:
if (count($calling_args) < count($func_args)) {
    throw new 
Zend_Rest_Server_Exception('Invalid Method Call to ' $this->_method '.   Requires ' count($func_args) . ', ' count($calling_args) . ' given.'400);

__________________
Zym Framework - A Zend Framework extension library w/ demo app

SpotSec Blog:
http://spotsec.com/blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-30-2007, 06:17 AM
Junior Member
 
Join Date: Mar 2007
Posts: 1
Default Posible solution to pass arrays with Zend_Rest

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).
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 05-02-2007, 12:48 AM
SpotSec's Avatar
Senior Member
 
Join Date: Feb 2007
Location: United States
Posts: 121
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 08-30-2008, 06:50 PM
Junior Member
 
Join Date: Aug 2008
Posts: 1
Default You can override the _structValue method in your own class

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);
        }
    }	
}
Then, in order to use your class, you would do something like this:

$server = new JoshuaBeall_Rest_Server();
$server->setClass('JoshuaBeall_WebApi_MainApi');
$server->handle();

Hope that helps!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 09:28 PM.