CodeIgniter Forums
XML-RPC from Non-Codeigniter source - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: XML-RPC from Non-Codeigniter source (/showthread.php?tid=8377)



XML-RPC from Non-Codeigniter source - El Forum - 05-15-2008

[eluser]hotmeteor[/eluser]
This may be a very basic practice but I am having a ton of trouble getting XML-RPC to work.

Basically, I have a form on one site that I want to send data and process on another site, then return information. I'm assuming I can do this.

Can anyone give me a basic example of this (I am using the XMLRPC class, not the native-PHP class). The biggest issue I'm having is not with returning data but with using the data that is sent from the client, ie. how to read the request and pull out data.

Anyone? Help? Please?!?


XML-RPC from Non-Codeigniter source - El Forum - 05-15-2008

[eluser]gtech[/eluser]
are you needing to convert post data to xmlrpc? your requirements are a bit vague.. maybe you could post a cut down version of your client and server, and then someone might be able to help you.


XML-RPC from Non-Codeigniter source - El Forum - 05-15-2008

[eluser]hotmeteor[/eluser]
Yep, it is post data but it's already been validated and cleaned up so I have put it into a new array. I am able to send the client data (my array) to my server (CI app) but at that point I can't figure out how to pull the variables out. I'm using php_xmlrpc_encode on the client end as well, so maybe there's a problem there?

Also, I can send server data back to the client, but it's just the sample data from the User Guide.

Hopefully that's a bit more info, but if sample code is necessary I might be able to post some in the morning.


XML-RPC from Non-Codeigniter source - El Forum - 05-16-2008

[eluser]gtech[/eluser]
its difficult for anyone to see where the problem is without code, but what I would recommend is use something like [url="http://www.wireshark.org/"]wireshark[/url] to see whats being sent to your server.. and then it may help you to be able to decipher how to pull out the XML.

its free Smile


XML-RPC from Non-Codeigniter source - El Forum - 05-16-2008

[eluser]hotmeteor[/eluser]
Ok, here is my code.

Client
Code:
include('xmlrpc.inc');
                    include('xmlrpcs.inc');                

                    // recursive struct
                    $req = php_xmlrpc_encode( array('tom') );
                                    
                    $c = new xmlrpc_client('/4cm/index.php/forms', 'localhost', 80);    // Set up client
                    $msg = new xmlrpcmsg('Add_contact', $req);
                    $r = $c->send($msg);                    

                    echo '<pre>';
                    print_r( $r );
                    echo '</pre>';

Server
Code:
&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Forms extends Public_Controller {

    function Forms() {
        parent::Public_Controller();
    }
    
    /*
    --------------------------------------------------------------
    Serve XML-RPC
    --------------------------------------------------------------
    */
    
    function index() {
        $this->load->library('xmlrpc');
        $this->load->library('xmlrpcs');
        
        $config['functions']['Add_contact'] = array('function' => 'Forms.add_contact');
        
        $this->xmlrpcs->initialize($config);
        $this->xmlrpcs->serve();
    }

    /*
    --------------------------------------------------------------
    Add Contact (XML-RPC)
    --------------------------------------------------------------
    */
    
    function add_contact($request) {        
        
        $pars = $request->output_parameters();
        $response = array(
            array(
                  'pars'      => array('ya', 'string'),
                  'nickname'  => array('Smitty','string'),
                  'userid'    => array('99','string'),
                  'url'       => array('http://yoursite.com','string'),
                  'email'     => array('[email protected]','string'),
                  'lastname'  => array('Smith','string'),
                  'firstname' => array('John','string')
                  ),
           'struct');
                        
        return $this->xmlrpc->send_response($response);
    }

}

?&gt;

I want to retrieve the variables from the $req array on my server. How do I do that?


XML-RPC from Non-Codeigniter source - El Forum - 05-16-2008

[eluser]gtech[/eluser]
Hi i gave your code a go, and found a solution.. you were very close.

here is what I did, note the php_xmlrpc_encode was the problem as you thought.. the parameters I passed also gets passed in as an array, so I simply return $pars[0].

hope this helps.

Code:
&lt;?php
    include('lib/xmlrpc.inc');
    include('lib/xmlrpcs.inc');
    $c = new xmlrpc_client('/CI1.6.1/index.php/cif_s', 'localhost', 80); // Set up client

    // I managed to use this way of passing the params.
    $msg = new xmlrpcmsg('Add_contact', array(new xmlrpcval('param1','string'),new xmlrpcval(2,'int')));

    $r = $c->send($msg);
    echo '<pre>';
    print_r( $r );
    echo '</pre>';
?&gt;
Code:
&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Cif_s extends Controller {

    function Cif_s() {
        parent::Controller();
    }
    /*--------------------------------*/    
    function index() {
        $this->load->library('xmlrpc');
        $this->load->library('xmlrpcs');
        $config['functions']['Add_contact'] = array('function' => 'Cif_s.add_contact');
        $this->xmlrpcs->initialize($config);
        $this->xmlrpcs->serve();
    }
    /*--------------------------------*/
    function add_contact($request) {        
        $pars = $request->output_parameters();
        // I return $pars[0] I could also return $pars[1]
        $response = array(
                        array('pars' => array($pars[0], 'string')
                    ),'struct');                    
        return $this->xmlrpc->send_response($response);
    }
}
?&gt;


RESPONSE:
Code:
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
<methodResponse>
<params>
<param>
<value>
<struct>
<member>
<name>pars</name>
<value>
<string>param1</string>
.....
//woo hoo it worked



XML-RPC from Non-Codeigniter source - El Forum - 09-29-2008

[eluser]kyko[/eluser]
Using wireshark, how did you get the actual xml being sent as request and response? i see the array i passed but not in xml.