Hacker for Hire

PHP Alternative to DWR: Hacker For Hire’s Solution

Wyatt • • Rants and Technology

I have been scouring the Internet for two days for looking for a suitable [PHP][1] alternative to [DWR]2 and haven’t found a single one so I’ve hacked out my own.

I had been looking at [xajax][3] in the hopes that it would get me what I was looking for, but it didn’t. So the next one I found was [SAJAX][4]. This project appears to be less active than [xajax][5]; however, disappointment strikes again. [SAJAX][4] does not support JavaScript objects, only basic variables. While that’s all well and good, when I need to pass larger objects, that’s extremely fricken annoying!!!

But wait … a glimmer of hope on the horizon … [Sanjer][6] promises to combine [SAJAX][4] and [JSON][7]. Only one major problem, it’s not actively maintained and it’s based on the 0.10 version of [SAJAX][4] which had some serialization bugs.

So to end the frustration for me and anyone else on the Internet who would ever want to do something this stupid (it apparently has to be stupid since no one else is willing to pull this off), I present my solution to the problem … hacking [SAJAX][4]. Before I delve into what changed, I’m going to give you the files so you can just run with it in the event that you aren’t interested in that crap. [This zip includes everything you need to pull this off][8]: Crockford’s json.js from http://www.json.org/, hacked up version of Sajax.php from me, and a simple index.php file that gives a super simple example of how it works. Oh yeah, since this is all based on GPL/Open source code, my derivation falls under the same licensing and comes with absolutely no warranty. Now, on to the other stuff that most people don’t really care about.

Fortunately, the people who wrote the SAJAX framework did a really good job a keeping a very nice code separation. All that had to happen to tweak this was:

Now I know that I could put this encode and decode inside every [PHP][1] function; however, I’m lazy and I don’t want to do that, especially if it’s a framework and it should do it for me. Here’s a snip-it of changes I made to the file:

//parse out the args array
$newArgArray = array();
$arraySize = sizeof($args);
for($i = ; $i < $arraySize; $i++) {
        //decode the JSON object
        $tmpArg = json_decode($args[$i]);
        array_push($newArgArray, $tmpArg);

$result = call_user_func_array($func_name, $newArgArray);
//encode the result in to JSON
$retString = json_encode($result);
//echo "var res = " . trim(sajax_get_js_repr($result)) . "; res;";
echo "var res = " . $retString . "; res;"

for (i = ; i < args.length1; i++) {
        //uri += "&rsargs[]=" + escape(args[i]);
        uri += "&rsargs[]=" + escape(args[i].toJSONString());

for (i = ; i < args.length1; i++) {
        //post_data = post_data + "&rsargs[]=" + escape(args[i]);
        post_data = post_data + "&rsargs[]=" + escape(args[i].toJSONString());

Well, that’s it. Really fricken hard, eh? Yeah, didn’t think so. I’m sure that this version will work for most uses; however, if you do find a bug, feel free to leave a comment or send it to me at wyatt neal on my GMail account and I’ll do my best to help you out. Hopefully this will inspire someone else with more time to come up with a better PHP framework that is a lot closer to the coolness you get from DWR.

If you have one that does what DWR does, drop a comment and let me know. SAJAX, PAJAX, and XAJAX don’t count because I’ve looked at them and I know they don’t provide the same level of use. The only one that comes close is PAJAX but it fails because it does synchronous instead of asynchronous calls by default (what where you thinking people?!?).

[1]: http://www.php.net [2]: http://getahead.org/dwr/overview/dwr [3]: http://www.xajaxproject.org/ [4]: http://www.modernmethod.com/sajax/ [5]: http://www.xajaxproject.org [6]: http://sanjer.berlios.de/ [7]: http://www.json.org [8]: /wp-content/uploads/2007/03/hacked-sajax.zip "Sajax Hacked"

comments powered by Disqus