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:

<?php
error_reporting(E_ALL|E_STRICT);

$server_handle = xmlrpc_server_create();
xmlrpc_server_register_method(
    $server_handle
    ,'uname'
    ,'xmlrpc_uname');

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

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 😉 ):

<?php
error_reporting(E_ALL);

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")));
//$client->setDebug(1);
$response = $client->send($msg);
$result = $response->value()->structmem('info')->serialize();
var_dump($result);

?>

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

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

<h1> testing </h1>

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

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

</body>
</html>
?>

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