Welcome Guest, Not a member yet? Register   Sign In
how to change 'item' on Phil Sturgeon’s RESTful API
#1

[eluser]huuray[/eluser]

<xml>

<item>
<userid>1</userid>
<username>example</username>
<useremail>[email protected]</useremail>
</item>
&lt;/xml&gt;

hi all,im new to CI,just want to know how to change <item> to something else.maybe <user>
the array from database.

thanks
#2

[eluser]Stockhausen[/eluser]
Hi,

Near 408 on the REST_controller.php, add a param $rootxml and change inside the private function near 431

Code:
$key = "item";

by
Code:
$key = $rootxml;

Do the same action to

private function _format_rawxml

Code:
// Format XML for output
    private function _format_xml($data = array(), $structure = NULL, $basenode = 'xml', $rootxml = "item")
    {
        // turn off compatibility mode as simple xml throws a wobbly if you don't.
        if (ini_get('zend.ze1_compatibility_mode') == 1)
        {
            ini_set ('zend.ze1_compatibility_mode', 0);
        }

        if ($structure == NULL)
        {
            $structure = simplexml_load_string("&lt;?xml version='1.0' encoding='utf-8'?&gt;<$basenode />");
        }

        // loop through the data passed in.
        $data = $this->_force_loopable($data);
        foreach($data as $key => $value)
        {
            // no numeric keys in our xml please!
            if (is_numeric($key))
            {
                // make string key...
                //$key = "item_". (string) $key;
                $key = $rootxml;
            }

            // replace anything not alpha numeric
            $key = preg_replace('/[^_a-z]/i', '', $key);

            // if there is another array found recrusively call this function
            if (is_array($value) || is_object($value))
            {
                $node = $structure->addChild($key);
                // recrusive call.
                $this-> _format_xml($value, $node, $basenode);
            }
            else
            {
                // add single node.

                $UsedKeys[] = $key;

                $structure->addChild($key, $value);
            }

        }
        
        // pass back as string. or simple xml object if you want!
        return $structure->asXML();
    }
#3

[eluser]huuray[/eluser]
hi,Stockhausen,thx for reply.
its still the same.
#4

[eluser]Stockhausen[/eluser]
Exact do this way, maybe the only method

Near 87, we change the response method :
Code:
function response($data = array(), $http_code = 200, $xmlroot = 'item')
    {
           if(empty($data))
        {
            $this->output->set_status_header(404);
            return;
        }

        $this->output->set_status_header($http_code);
        
        // If the format method exists, call and return the output in that format
        if(method_exists($this, '_format_'.$this->_format))
        {
            // Set the correct format header
            $this->output->set_header('Content-type: '.$this->_supported_formats[$this->_format]);
        
            if($this->_format == 'xml')
            {
                $formatted_data = $this->{'_format_'.$this->_format}($data,null,$this->_format,$xmlroot);
            } else {
                $formatted_data = $this->{'_format_'.$this->_format}($data);
            }
                $this->output->set_output( $formatted_data );
        }
        
        // Format not supported, output directly
        else
        {
            $this->output->set_output( $data );
        }
    }


Near 408 on the REST_controller.php, add a param $rootxml and change inside the private function near 431

Code:
$key = "item";

by
Code:
$key = $rootxml;

Do the same action to

private function _format_rawxml

Code:
// Format XML for output
    private function _format_xml($data = array(), $structure = NULL, $basenode = 'xml', $rootxml = "item")
    {
        // turn off compatibility mode as simple xml throws a wobbly if you don't.
        if (ini_get('zend.ze1_compatibility_mode') == 1)
        {
            ini_set ('zend.ze1_compatibility_mode', 0);
        }

        if ($structure == NULL)
        {
            $structure = simplexml_load_string("&lt;?xml version='1.0' encoding='utf-8'?&gt;<$basenode />");
        }

        // loop through the data passed in.
        $data = $this->_force_loopable($data);
        foreach($data as $key => $value)
        {
            // no numeric keys in our xml please!
            if (is_numeric($key))
            {
                // make string key...
                //$key = "item_". (string) $key;
                $key = $rootxml;
            }

            // replace anything not alpha numeric
            $key = preg_replace('/[^_a-z]/i', '', $key);

            // if there is another array found recrusively call this function
            if (is_array($value) || is_object($value))
            {
                $node = $structure->addChild($key);
                // recrusive call.
                $this-> _format_xml($value, $node, $basenode, $xmlroot);
            }
            else
            {
                // add single node.

                $UsedKeys[] = $key;

                $structure->addChild($key, $value);
            }

        }
        
        // pass back as string. or simple xml object if you want!
        return $structure->asXML();
    }

and when you call your response in your controller do this syntax :
Code:
$this->response($user, 200,'user')

Probably not the best way. but it's work fine.
#5

[eluser]huuray[/eluser]
thanks man.its work.
#6

[eluser]Stockhausen[/eluser]
You're welcome so please pût thés topic with the résolve status.




Theme © iAndrew 2016 - Forum software by © MyBB