What is a good RPC model for building an AJAX web application using PHP and JS?

I am new to writing AJAX applications. I plan on using jQuery on the client side and PHP on the server side. I want to use something like XML-RPC to simplify my actions when invoking server-side code. Ideally, I don't care if the transport layer uses XML or JSON or a format more optimized for wires.

If I were writing a console application, I would use some kind of tool to create stubs for the functions that I would then implement on the RPC server, while the client would initially run these stubs. This provides a clean separation. Is there something similar in the AJAX world?

During this topic, how can I continue to manage the session? I would like it to be as transparent as possible. For example, if I try to hit an RPC endpoint that requires a valid session, it should reject the request if the client does not send a valid session cookie.

This would really facilitate the development of my application. Then I should just process the interface using JS native functions. Although on the backend, I can just implement the RPC functions.

BTW I do not want to use the Google Web Toolkit. My application will not be extremely heavy for AJAX.

+3
source share
3 answers

This is my typical solution, so YMMV.

PHP (, "add_user.php", "login.php", "do_something.php" ), "" , :

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

HTTP- POST, - JSON ( , PHP POST , jQuery JSON , JSON2 json.org, eval()), :

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

jQuery AJAX, JS . , , , (XML, URLEncoded ..), ; "" :

// 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;
}

, , , , , .

+1
0

XML-RPC. . HTTP- (GET POST) , , JSON JSON HTML. PHP JSON: json_encode() json_decode().

0

Source: https://habr.com/ru/post/1749898/


All Articles