Welcome Guest, Not a member yet? Register   Sign In
XML-RPC - more than one record return help
#1

[eluser]PhxVyper[/eluser]
I have an xml-rpc server i'm creating and I need to return a response that contains more than one record.

Example, 2 records I want returned:

nickname = howdie ho!
userid = 99
url = http://yoursite.com
email = [email protected]
lastname = Smith
firstname = John

and

nickname = howdie ho 2!
userid = 101
url = http://dev.yoursite.com
email = [email protected]
lastname = Jones
firstname = James

I've tried a number of different methods of building the $response structure that gets passed into send_response(), but none of them work!

Any help would be appreciated.

Chad
#2

[eluser]PhxVyper[/eluser]
Wow! Is nobody else using the CodeIgniter XML-RPC server implementation?
#3

[eluser]gtech[/eluser]
As nobody else wanted to reply I had a go, I have never used it before but wrote a test app for you.

quote from the documentation:

"Responses, however, usually contain multiple pieces of information. In order to accomplish this we must put the response into its own array so that the primary array continues to contain a single piece of data"

here is and example that returns an array of arrays (or a struct of structs in XML speak) its copied from the documentation pages and mucked around with a little bit.

You can see res1 and res2 returned in the response (you obviously need to adapt that to your data).

you should be able to cut and copy the code and run the controller yourself by browsing to the index page.

test.php
Code:
class Test extends Controller {

    function index()
    {    
        $this->load->helper('url');
        $server_url = site_url('test/test2');
        $this->load->library('xmlrpc');
        
        $this->xmlrpc->server($server_url, 80);
        $this->xmlrpc->method('Greetings');
        
        $request = array('How is it going?');
        $this->xmlrpc->request($request);    
        
        if ( ! $this->xmlrpc->send_request())
        {
            echo $this->xmlrpc->display_error();
        }
        else
        {
            echo '<pre>';
            $res = $this->xmlrpc->display_response();
            print_r($res);
            echo '</pre>';
        }
    }
    function test2()
    {
        $this->load->library('xmlrpc');
        $this->load->library('xmlrpcs');


        
        $config['functions']['Greetings'] = array('function' => 'Test.process');
        
        $this->xmlrpcs->initialize($config);
        $this->xmlrpcs->serve();
    }
    
    
    function process($request)
    {
        $parameters = $request->output_parameters();

                $response = array (
                   array(
                         'res1' => array(array('Name'=>'a','Name2'=>'a'),'struct'),
                         'res2' => array(array('Name'=>'b','Name2'=>'b'),'struct'),
                        ),
                 'struct'
                 );

                        
        return $this->xmlrpc->send_response($response);
    }

}

The result:
Code:
Array
(
    [res1] => Array
        (
            [Name] => a
            [Name2] => a
        )

    [res2] => Array
        (
            [Name] => b
            [Name2] => b
        )

)
#4

[eluser]gtech[/eluser]
just edited above post to implement associative arrays
#5

[eluser]ejangi[/eluser]
I am working on an API at the moment and I want to be able to grab records from a model and throw the (easily) to the client.

I put together this method (for my controller), you can throw any value at it and it will convert it to the value the xmlrpc:Confusedend_response(); method expects. I haven't yet implemented every XML-RPC type, but it does the basic ones so far and I thought other people may find it helpful:

Code:
class Api_controller extends Controller {

    function Api_controller()
    {
        parent::Controller();
        $this->load->library('xmlrpc');
        $this->load->library('xmlrpcs');
    } // Api_controller()
    
    function index()
    {
        $config['functions'] = array(
            'list_articles' => array('function' => 'Api_controller.list_articles')
        );
        $this->xmlrpcs->initialize($config);
        $this->xmlrpcs->serve();
    } // index()

    function convert_to_xmlrpc_values($obj)
    {
        $return = $obj;
        $type = 'string';
        
        if (is_object($obj)) {
            $return = (array) $obj;
            $type = 'struct';
        } elseif (is_numeric($obj)) {
            $type = 'int';    
        } elseif (is_bool($obj)) {
            $type = 'bool';    
        } elseif (is_array($obj)) {
            $each = array();
            foreach ($obj as $key => $value) {
                if (is_object($value)) {
                    $each[$key] = $this->convert_to_xmlrpc_values($value);
                } else {
                    $each[$key] = $value;    
                }
            }
            $return = $each;
            $type = 'array';
        }
        
        return array($return, $type);
    } // convert_to_xmlrpc_values()

    function list_articles($request)
    {
        $this->load->model('articles');
        // Will be an array of objects:
        $articles = $this->articles->get_all();
        // This is where the magic happens:
        $response = $this->convert_to_xmlrpc_values($articles);
        return $this->xmlrpc->send_response($response);
    }
}

As you can see all the magic happens in convert_to_xmlrpc_values(). Hope this helps someone!
#6

[eluser]gtech[/eluser]
That looks like a neat little function, I will give it a go thanks.
#7

[eluser]omed habib[/eluser]
What if you wanted to SEND an associative array to the server?
#8

[eluser]gtech[/eluser]
[url="http://ellislab.com/forums/viewthread/81915/"]send associative array through XMLRPC example here[/url]




Theme © iAndrew 2016 - Forum software by © MyBB