Part of the EllisLab Network
   
 
XML-RPC from Non-Codeigniter source
Posted: 15 May 2008 04:20 PM   [ Ignore ]  
Summer Student
Total Posts:  20
Joined  12-12-2007

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?!?

Profile
 
 
Posted: 15 May 2008 06:22 PM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  758
Joined  09-25-2007

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.

Profile
 
 
Posted: 15 May 2008 10:30 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  20
Joined  12-12-2007

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.

Profile
 
 
Posted: 16 May 2008 05:54 AM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  758
Joined  09-25-2007

its difficult for anyone to see where the problem is without code, but what I would recommend is use something like wireshark 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

Profile
 
 
Posted: 16 May 2008 09:10 AM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  20
Joined  12-12-2007

Ok, here is my code.

Client

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

<?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('jsmith@yoursite.com','string'),
                  
'lastname'  => array('Smith','string'),
                  
'firstname' => array('John','string')
                  ),
           
'struct');
                        
        return
$this->xmlrpc->send_response($response);
    
}

}

?>

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

Profile
 
 
Posted: 16 May 2008 11:28 AM   [ Ignore ]   [ # 5 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  758
Joined  09-25-2007

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.

<?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>';
?>


<?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);
    
}
}
?>


RESPONSE:

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

Profile
 
 
Posted: 29 September 2008 10:15 PM   [ Ignore ]   [ # 6 ]  
Summer Student
Avatar
Total Posts:  10
Joined  07-25-2007

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.

 Signature 

I love ExpressionEngine!

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 64454 Total Logged-in Users: 24
Total Topics: 80961 Total Anonymous Users: 0
Total Replies: 435697 Total Guests: 192
Total Posts: 516658    
Members ( View Memberlist )