Ajax| Client
This article gives the Ajax client implementation of the PHPRPC 2.1 protocol . The client is used in much the same way as version 2.0. The difference is that if the remote function you want to invoke is a reference parameter pass, you need to set the function's Ref property to True before calling it. This property indicates whether the reference parameter is passed. This property defaults to False. It is recommended that you set the Onready event in the client object.
The following is the implementation code for the client:
Download:Phprpc_ajax_client.js
- /* Phprpc_ajax_client.js-ajax PHPRPC Client
- *
- * Copyright Ma Bingyao <andot@ujn.edu.cn>
- * version:2.1
- * LASTMODIFIED:2006-03-12
- * This library are free. You can redistribute it and/or modify it.
- * http://www.coolcode.cn/?p=146
- */
- /*
- * Interfaces:
- * Phprpc_client.create (' RPC ');
- * Rpc.use_service (' http://domain.com/phprpc/server.php ');
- * Rpc.method_callback = function (result, args, output) {
- * If (result instanceof Phprpc_error) {
- * Alert (RESULT.ERRSTR);
- * }
- * Else {
- * Alert (result); Or do no other things.
- * }
- * }
- * ....
- * IF (Rpc.ready) Rpc.method ();
- */
- function Phprpc_error (errno, errstr) {
- This.errno = errno;
- This.errstr = Errstr;
- }
- function Phprpc_client () {
- this.__php = new Php_serializer ();
- This.__url = ';
- This.__encrypt = false;
- This.encrypt = 0;
- This.async = true;
- This.ready = false;
- This.args = null;
- This.output = "";
- This.use_service = function (URL, encrypt) {
- if (typeof (encrypt) = = "undefined") {
- encrypt = This.__encrypt;
- }
- if (typeof (this.__name) = = "undefined") {
- return false;
- }
- This.__url = URL;
- var xmlhttp = this.__create_xmlhttp ();
- var __rpc = this;
- if (encrypt = = True) {
- Xmlhttp.onreadystatechange = function () {
- if (xmlhttp.readystate = = 4) {
- if (Xmlhttp.responsetext) {
- eval (xmlhttp.responsetext);
- if (typeof (phprpc_encrypt) = = "undefined") {
- __rpc.__encrypt = false;
- __rpc.use_service (__rpc.__url);
- }
- else {
- __rpc.__encrypt = __rpc.__php.unserialize (Phprpc_encrypt);
- __rpc.__encrypt[' P '] = dec2num (__rpc.__encrypt[' P '));
- __rpc.__encrypt[' g '] = Dec2num (__rpc.__encrypt[' G '));
- __rpc.__encrypt[' y '] = dec2num (__rpc.__encrypt[' y '));
- __rpc.__encrypt[' x '] = rand (127, 1);
- var key = Pow_mod (__rpc.__encrypt[' y '),
- __rpc.__encrypt[' x '],
- __rpc.__encrypt[' P ']);
- Key = Num2str (key);
- for (var i = 0; i < 16-key.length; i++) {
- Key = ' + ' + key;
- }
- __rpc.__encrypt[' k '] = key;
- var encrypt = Pow_mod (__rpc.__encrypt[' G '),
- __rpc.__encrypt[' x '],
- __rpc.__encrypt[' P ']);
- __rpc.use_service (__rpc.__url, Num2dec (encrypt). Replace (/\+/g, '%2b '));
- }
- }
- Delete (XMLHTTP);
- }
- }
- }
- else {
- Xmlhttp.onreadystatechange = function () {
- if (xmlhttp.readystate = = 4) {
- if (Xmlhttp.responsetext) {
- eval (xmlhttp.responsetext);
- var functions = __rpc.__php.unserialize (phprpc_functions);
- var func;
- for (var i = 0; i < functions.length; i++) {
- Func = __rpc.__name + "." + functions[i] + "= function () {\ r \ n";
- Func + = "This.__call (' + functions[i] +" ', This.__args_to_array (arguments)); \ r \ n ";
- Func + = "}\r\n";
- Func + = __rpc.__name + "." + Functions[i] + ". ref = false;\r\n";
- Eval (func);
- }
- __rpc.ready = true;
- if (typeof (__rpc.onready) = = "function") {
- __rpc.onready ();
- }
- }
- Delete (XMLHTTP);
- }
- }
- }
- Xmlhttp.open ("Get", This.__url + '? phprpc_encrypt= ' + Encrypt + ' &phprpc_encode=false ', true);
- Xmlhttp.send (NULL);
- };
- This.__call = function (func, args) {
- var __args = this.__php.serialize (args);
- if ((This.__encrypt!== false) && (This.encrypt > 0)) {
- __args = Xxtea_encrypt (__args, this.__encrypt[' K '));
- }
- __args = Base64Encode (__args);
- var request = ' phprpc_func= ' + func
- + ' &phprpc_args= ' + __args
- + ' &phprpc_encode=false '
- + ' &phprpc_encrypt= ' + this.encrypt;
- var ref = eval (This.__name + "." + func + ". Ref");
- if (!ref) {
- Request + = ' &phprpc_ref=false ';
- }
- var xmlhttp = this.__create_xmlhttp ();
- var session = {' args ': args, ' ref ': REF, ' Encrypt ': This.encrypt};
- if (This.async) {
- var __rpc = this;
- Xmlhttp.onreadystatechange = function () {
- if (xmlhttp.readystate = = 4) {
- if (Xmlhttp.responsetext) {
- __rpc.__get_result (XMLHTTP, session);
- if (typeof (__rpc.__name +. + func + "_callback")) = = "function") {
- Eval (__rpc.__name + "." + func + "_callback (Phprpc_result, Phprpc_args, phprpc_output);");
- }
- }
- Delete (XMLHTTP);
- }
- }
- }
- Xmlhttp.open ("Post", This.__url, This.async);
- Xmlhttp.setrequestheader (' Content-type ', ' application/x-www-form-urlencoded; Charset=utf-8 ');
- Xmlhttp.send (Request.replace (/\+/g, '%2b '));
- if (!this.async) {
- if (Xmlhttp.responsetext) {
- This.__get_result (XMLHTTP, session);
- This.output = Phprpc_output;
- This.args = Phprpc_args;
- return phprpc_result;
- }
- else {
- Return to New Phprpc_error (1, "No data received from server");
- }
- Delete (XMLHTTP);
- }
- };
- This.__get_result = function (XMLHTTP, session) {
- eval (xmlhttp.responsetext);
- if (Phprpc_errno = = 0) {
- if ((This.__encrypt!== false) && (Session.encrypt > 0)) {
- if (Session.encrypt > 1) {
- Phprpc_result = Xxtea_decrypt (Phprpc_result, this.__encrypt[' K '));
- }
- if (session.ref) {
- Phprpc_args = Xxtea_decrypt (Phprpc_args, this.__encrypt[' K '));
- }
- }
- Phprpc_result = This.__php.unserialize (Phprpc_result);
- if (session.ref) {
- Phprpc_args = This.__php.unserialize (Phprpc_args);
- }
- else {
- Phprpc_args = This.args;
- }
- }
- else {
- Phprpc_result = new Phprpc_error (Phprpc_errno, PHPRPC_ERRSTR);
- Phprpc_args = null;
- }
- }
- The function __create_xmlhttp modified from
- Http://webfx.eae.net/dhtml/xmlextras/xmlextras.html and
- http://www.ugia.cn/?p=85
- This.__create_xmlhttp = function () {
- if (window. XMLHttpRequest) {
- var objxmlhttp = new XMLHttpRequest ();
- Some older versions of Moz did not support the ReadyState property
- and the Onreadystate event so we patch it!
- if (objxmlhttp.readystate = = null) {
- objxmlhttp.readystate = 0;
- Objxmlhttp.addeventlistener (
- "Load",
- function () {
- Objxmlhttp.readystate = 4;
- if (typeof (objxmlhttp.onreadystatechange) = = "function") {
- Objxmlhttp.onreadystatechange ();
- }
- },
- False
- );
- }
- return objxmlhttp;
- }
- else {
- var MSXML = [' MSXML2. xmlhttp.5.0 ', ' MSXML2. xmlhttp.4.0 ', ' MSXML2. xmlhttp.3.0 ', ' MSXML2. XMLHTTP ', ' microsoft.xmlhttp '];
- for (var n = 0; n < msxml.length n + +) {
- try {
- return objxmlhttp = new ActiveXObject (Msxml[n]);
- }
- catch (e) {}
- }
- throw new Error ("Your browser does not support XMLHTTP");
- }
- };
- This.__args_to_array = function (args) {
- Argarray = [];
- for (i = 0; i < args.length; i++) {
- Argarray[i] = args[i];
- }
- return argarray;
- }
- }
- Phprpc_client.create = function (name, encrypt) {
- Eval (name + ' = new phprpc_client (); ');
- Eval (name + '. __name = ' + name + '; ');
- if (encrypt) {
- Encrypt = true;
- Eval (name + '. __encrypt = ' + encrypt + '; ');
- }
- }
Author Andot
<