Welcome Guest, Not a member yet? Register   Sign In
Easyxmlrpc library
#1

[eluser]Unknown[/eluser]
Hey guys, I'm short on time, but I wanted to share this little library I made to help us use Code Igniter's XML-RPC library. This is under the MIT license (http://www.opensource.org/licenses/mit-license.php), but if someone wanted to add this to the official XML-RPC library in CI, they're more than welcome to do so without the MIT/copyright notices under the license CI uses.

For us, this just took things one step further and made it really easy to return nested arrays we had via XML-RPC calls for our site http://bug.gd. In default CI this is a bit more annoying than it should be since XML-RPC response arrays need to be of a special form.

This effectively lets you take an Active Record's query result_array() and return it via XML-RPC without manual modification. Here is some example code we had:

Code:
// done earlier:       $this->load->library('xmlrpc');
// done earlier:       $this->load->library('easyxmlrpc');
// done earlier:       $this->load->model('User_model');    
    function getUserID($request) {
        $parameters = $request->output_parameters();
        $info = $this->User_model->getuserid($parameters[1]); // returns a query result
        if($info) {
            $response = $this->easyxmlrpc->convert_response($info->result_array());
            return $this->xmlrpc->send_response($response);
        }
        else {
            return $this->xmlrpc->send_error_message('123', 'Requested data not available');            
        }        
    }

We have this file inside system\application\libraries\Easyxmlrpc.php:

Code:
<?php
class Easyxmlrpc {

    // hacky function to detect if it's a sequential array or an associative array
    // array_is_associative()
    function _array_is_associative ($array)
    {
        if ( is_array($array) && ! empty($array) )
        {
            for ( $iterator = count($array) - 1; $iterator; $iterator-- )
            {
                if ( ! array_key_exists($iterator, $array) ) { return true; }
            }
            return ! array_key_exists(0, $array);
        }
        return false;
    }
    // This function is used inside getXMLRPCdata()
    // arrays for XML-RPC convert normal data to strings, but need
    //  array structure.
    function _getArrayValue($data) {
        if (is_array($data)) {
            return $this->convert_response($data);
        }
        else {
            return $data;
        }
    }
    // This function takes a value, array, or nested array and reconstructs it into
    //   an array structure that CI's XML-RPC libraries like.
    // Sequentially indexed arrays are returned as lists unless $onlystruct is provided and
    //   then they'll be constructed as structs with the key as a string (e.g. '1' => 'myvalue')
    function convert_response($data, $onlystruct=FALSE) {
        if (is_int($data)) {
            return array($data, 'int');
        }
        if (is_string($data)) {
            return array($data, 'string');
        }
        if (is_array($data)) {
            $outputform = 'struct';
            $temporary = array();
            $assoc =  $this->_array_is_associative($data);
            foreach($data as $key => $value) {
                if (!$assoc AND !$onlystruct) {
                    $outputform = 'array';
                    $temporary[] = $this->_getArrayValue($value);
                }
                else {
                    $outputform = 'struct';                    
                    $temporary[$key] = $this->convert_response($value);
                }
            }
            return (array($temporary, $outputform));
        }
        // TODO: Support more types maybe?
        //* boolean
        //* double
        //* dateTime.iso8601
        //* base64
        return array("_".strval($data), 'string');
    }
}
?>

Hope this helps someone other than just us,
-Matthew
Co-founder, http://bug.gd




Theme © iAndrew 2016 - Forum software by © MyBB