js does xmlrpc

Today I explored some new (to me) technologies to support a feature I was playing with. On a complex form representing a row being edited in a database, I had several 1-to-many relationships that I wanted to edit (i.e. make additions to) without submitting the main form. It is an intranet application, so I have the ability to rely on the fact that users will be using IE, and js will be enabled.

From the usual bag of tricks, my initial thought was you could have javascript submit a hidden form in a non-visible frame, and have the results come back with some onLoad() js that would update the main form (even did a little flow diagram, will attach at the end of the post). But I remembered Harry mentioning using js and php to communicate by serializing data in mutually compatible formats. This is the scriptserver project which he blogged about a while back. Harry also sent me a link to this js library to implement xmlrpc.

I have to admit that I have not played around with xmlrpc to date (at least in terms of actually doing anything with the technology, I have read enough to have the gist of xmlrpc and SOAP). This is mainly because I kept asking my Unix admin at work to compile php with the xmlrpc extension, and he always complained that he could not get the extension to compiled on HP-UX. I have finally started to push for some Linux web servers at work, so that will not persist as an issue in the future. Anyway, I decided to play around with this javascript xmlrpc library and see what I could come up with.

If you have stuck with me this long, then My first step was to try to get a php xmlrpc server up and running. With a fresh php 5.0.1 binary with xmlrpc installed, I hacked out this code for a test server:


$server_handle = xmlrpc_server_create();

header('Content-Type: text/xml');
echo xmlrpc_server_call_method($server_handle, $HTTP_RAW_POST_DATA, null);

function xmlrpc_uname($method, $parms) {
    return array(
        'info' => `uname -a`
        ,'input' => var_export(array($method,$parms),true));


and this for a client (in php first, have to stick with what you know 😉 ):


require_once 'XML/RPC.php';
$client = new XML_RPC_Client('/~sweatje/test_xmlrpc/', $_SERVER['HTTP_HOST'], 80);
$msg = new XML_RPC_Message('uname', array(new XML_RPC_Value(23, "int")));
$response = $client->send($msg);
$result = $response->value()->structmem('info')->serialize();


So here is what I ended up with for the javascript client:

<title>XMLRPC - JS to PHP test</title>
<script type="text/javascript" src="/js/jsolait/init.js"></script>
<script type="text/javascript">
var xmlrpc=null;
    var xmlrpc = importModule("xmlrpc");
    throw "importing of xmlrpc module failed.";
onSubmit = function(){
    var txRslt = document.getElementById("result");
    txRslt.innerHTML = "";
        var server = new xmlrpc.ServerProxy("http://intranet.server.com/~sweatje/test_xmlrpc/", ["uname"]);
        var rslt = server.uname();
        txRslt.innerHTML = rslt.info;
    return false;

<h1> testing </h1>

<h2> run </h2>
 <button type="button" onclick="onSubmit()">execute</button>

<h2> result</h2>
<div id="result">before test</div>


There, now I have everything for a complete proof of concept using a php xmlrpc server and javascript xmlrpc client. In addition, further tests reveal that the $_SESSION persists even with the js client! This is a bonus for me because this is an easy fix for my security concerns (my default security setup tucks user authentication info into the session).

And as promised, here is the initial stab at a frame/javascript base system flow diagram:
js frame flow diagram