views:

30

answers:

3

I'm new to writing AJAX applications. I plan on using jQuery on the client side while PHP on the server side. I want to use something like XML-RPC to simplify my effort in calling server-side code. Ideally, I wouldn't care whether the transport layer uses XML or JSON or a format more optimized for the wire.

If I was writing a console app I'd use some tool to generate function stubs which I would then implement on the RPC server while the client would natively call into those stubs. This provides a clean separation. Is there something similar available in the AJAX world?

While on this topic, how would I proceed with session management? I would want it to be as transparent as possible. For example, if I try to hit an RPC end-point which needs a valid session, it should reject the request if the client doesn't pass a valid session cookie.

This would really ease my application development. I'd then have to simply handle the frontend using native JS functions. While on the backend, I can simply implement the RPC functions.

BTW I dont wish to use Google Web Toolkit. My app wont be extremely heavy on AJAX.

A: 

for example phpxmlrpc together with standard session stuff

Redlab
A: 

I wouldn't use XML-RPC. There's no need. Send either HTTP query parameters (GET or POST) to the server or possibly a JSON object and get JSON or HTML in response. PHP has methods for encoding and decoding JSON: json_encode() and json_decode().

cletus
+1  A: 

This is my typical solution, so YMMV.

For the PHP end, I break my various remote functions into individual files (e.g. 'add_user.php', 'login.php', 'do_something.php'), each of which sets response data into a pre-defined array named 'response', and include the files dynamically based on the action requested, such as:

switch ($_POST['action']) {
    case 'addUser':
        require 'add_user.php';
        break;
    case 'login':
        require 'login.php';
        break;
    // ... snip ...
    default:
        $response['result'] = 'badaction';
        break;
}

echo json_encode($response);

Each individual file is designed to parse an HTTP POST request, do something with it, and return a JSON response (this gives both sides a fairly simple time parsing results, since PHP will automatically prepare the POST values for you, and jQuery can automatically convert a JSON response into an object, though I recommend picking up the JSON2 library from json.org so you don't have to worry about eval() issues), and they look something like this:

<?php
if (basename(__FILE__) == basename($_SERVER['SCRIPT_FILENAME'])) {
    die; // prevent the file from being accessed directly
}
// the do {} while(0); block allows us to set the response and then short-circuit
// execution without using a big ugly nest of if/else statements or a function
do {
    if (!isset($_POST['something'],$_POST['parameter'],$_POST['whatever'])) {
        $response['result'] = 'badinfo';
        continue;
    }
    // ... snip ...
} while(0);

Given jQuery's fairly simple AJAX request methods, the JS shouldn't be too hard to figure out. One nice thing about this method is that, should you need the response to be in some other format (XML, URLEncoded, etc), it's a snap; personally, I add a 'format' parameter to the request and parse it like so:

// EXPLICIT FORMAT
$format = strtolower(isset($_REQUEST['format']) ? $_REQUEST['format'] : null);

// IMPLICIT FORMAT
if (!$format) {
    if (isset($_SERVER) && is_array($_SERVER) && array_key_exists('HTTP_ACCEPT',$_SERVER)) {
        $accept = $_SERVER['HTTP_ACCEPT'];
        if (stripos($accept, 'application/json') !== false  || stripos($accept, 'text/javascript') !== false) {
            $format = 'json';
        }
    }
    if (!$format) {
        $format = 'url';
    }
}
switch ($format) {
    case 'json':
        echo json_encode($response);
        break;
    case 'url':
    default:
        echo http_build_query($response);
        break;
}

Hope this helps, reply in comments with any questions and I will hopefully shed some more light on the situation.

Dereleased
cletus, Redflag: Thanks for your responses.Dereleased: Your explanation was pretty useful. Have you used GWT? Have a look at this: http://code.google.com/webtoolkit/doc/latest/DevGuideServerCommunication.html#DevGuidePlumbingDiagramIs there something similar that I can do using off the shelf libraries?