CodeIgniter Forums
Associative array as request-data in XML-RPC request - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Associative array as request-data in XML-RPC request (/thread-9050.html)



Associative array as request-data in XML-RPC request - El Forum - 06-10-2008

[eluser]hlz[/eluser]
It has been adressed before but i can't find a solid answer or a good solution. I want to post code below to a rpc-server:
Code:
$request = array(
    
    array(
                
        'userid'    => array('99','string'),
        'url'       => array('http://yoursite.com','string'),
        'email'     => array('[email protected]','string'),
        'lastname'  => array('Smith','string'),
        'firstname' => array('John','string')
    ),
    
    'struct'
);

This doesn't work as a request. It does however work when the same array is set as a server-response. Could anyone tell me why a struct or array type cannot be used as request data?

Edit: my question actually is: how does one pass key-value pairs to a xml-rpc-server if code above cannot be used?


Associative array as request-data in XML-RPC request - El Forum - 06-11-2008

[eluser]hlz[/eluser]
Anyone? Maybe the developer (Paul Burdick?) of this set of classes could shine a light on this?


Associative array as request-data in XML-RPC request - El Forum - 06-11-2008

[eluser]gtech[/eluser]
I have to say this was a complete head bender to get working, but after reading the code I got the following to work

Client:
Code:
<?php

class Xmlrpc_client extends Controller {
    
    function index()
    {    
        $this->load->helper('url');
        $server_url = site_url('xmlrpc_server');
    
        $this->load->library('xmlrpc');

        
        $this->xmlrpc->server($server_url, 80);
        $this->xmlrpc->method('Greetings');

        $this->xmlrpc->request(array(
                                    array(
                                          // Param 0
                                          array('test'=>'testdata'),
                                          'struct'
                                    ),
                                    array(
                                          // Param 1
                                          array('var1'=>'testdata2',
                                                'var2'=>'testdata3'),
                                          'struct'
                                    )
                               ),
                               'struct');        

        if ( ! $this->xmlrpc->send_request())
        {
            echo $this->xmlrpc->display_error();
        }
        else
        {
            echo '<pre>';
            print_r($this->xmlrpc->display_response());
            echo '</pre>';
        }
    }
}
?&gt;

Server:
Code:
&lt;?php

class Xmlrpc_server extends Controller {

  function index()
  {
    $this->load->library('xmlrpc');
    $this->load->library('xmlrpcs');
        
    $config['functions']['Greetings'] = array('function' => 'Xmlrpc_server._process');
        
    $this->xmlrpcs->initialize($config);
    $this->xmlrpcs->serve();
  }
    
    
  function _process($request)
  {
    $parameters = $request->output_parameters();
    $response = array(
                     array(
                          'you_said'  => $parameters['0']['test'],
                          'you_said2'  => $parameters['1']['var1'],
                          'you_said3'  => $parameters['1']['var2'],
                          'i_respond' => 'Not bad at all.'
                     ),
                'struct');
    return $this->xmlrpc->send_response($response);
  }
}
?&gt;

enjoy Smile


Associative array as request-data in XML-RPC request - El Forum - 06-11-2008

[eluser]gtech[/eluser]
just edited above to add more test data


Associative array as request-data in XML-RPC request - El Forum - 06-11-2008

[eluser]hlz[/eluser]
That works indeed, thanks for your thoughts and work. But, there is always a but, wouldn't it be nice to have the parameters in the server like this:
Code:
'you_said1' => $parameters['test'],
'you_said2' => $parameters['var1'],
'you_said3' => $parameters['var2'],

That is what i was actually looking for. With the numeric indices it's difficult to do isset($parameters['test'])'s. Of course one could first rearrange the parameters-array and filter out the numeric indices, but that's not the point here Tongue


Associative array as request-data in XML-RPC request - El Forum - 06-11-2008

[eluser]gtech[/eluser]
the problem is I don't think you can do what you are suggesting, in your case you just send the associative array as paramater 0

Code:
....
    $this->xmlrpc->request(array(
                                array(
                                      array(
                                            'userid'    => '99',
                                            'url'       => 'http://yoursite.com',
                                            'email'     => '[email protected]',
                                            'lastname'  => 'Smith',
                                            'firstname' => 'John'
                                      ),
                                      'struct'
                                )
                           ),
                           'struct');
...

              $response = array(
                     array(
                          'you_said2'  => $parameters['0']['userid'],
                          'you_said3'  => $parameters['0']['url'],
                          'i_respond' => 'Not bad at all.'
                     ),
                'struct');

the reason why is xmlrpc will send data like:
Code:
&lt;?xml version="1.0"?&gt;
<methodCall>
   <methodName>examples.getStateName</methodName>
   <params>
      <param>
         <value><i4>41</i4></value>
      </param>
      <param>
         <value><string>Another Val</string></value>
      </param>
   </params>
</methodCall>

The parameters are sent through the param tags, you don't give them a name so it will always be param0 param1 param2 etc.

say if you wanted to pass an array to a normal php function you would pass it through a paramter index, the principle is the same (thats my understanding anyway).


Associative array as request-data in XML-RPC request - El Forum - 06-11-2008

[eluser]hlz[/eluser]
I had already set your conclusion as my preliminary conclusion. I guess i have to live with it then. Thanks a lot for your help.


Associative array as request-data in XML-RPC request - El Forum - 06-11-2008

[eluser]gtech[/eluser]
your welcome, I have learned a lot as well.


Associative array as request-data in XML-RPC request - El Forum - 06-11-2008

[eluser]Jacob156[/eluser]
Thank you for this code. I have been trying to find this out for the last two weeks.


Associative array as request-data in XML-RPC request - El Forum - 06-11-2008

[eluser]gtech[/eluser]
maybe a request to enhance the XMLRPC documentation is needed.

[url="http://ellislab.com/forums/viewthread/82212/"]http://ellislab.com/forums/viewthread/82212/[/url]