📜 ⬆️ ⬇️

.mrpc - simple client interaction with the server

For one of the following reports, implemented .mRPC - a simple mechanism for invoking client-side server methods.
Working example

I apologize in advance for the terrible markup on Habré, the correct article is in my personal blog ...
Yes, I will add - the arguments and return value of the server function are not limited by anything, except for the limitations of serialization as such. Do you want to return an array or hash of any nesting from the server - nobody restricts you;)

This is what the client looks like:
  1. <! - jQuery ->
  2. < script type = "text / javascript" src = "/ samples / mrpc / jquery.js" > </ script >
  3. <! - JSON-serialization support ->
  4. <script type = "text / javascript" src = "/samples/mrpc/jquery.json.js" > </ script>
  5. <! - .mrpc support ->
  6. <script type = "text / javascript" src = "/samples/mrpc/jquery.mrpc.js" > </ script>
  7. <! - description of supported server classes and methods ->
  8. <script type = "text / javascript" src = "/ samples / mrpc /" > </ script >
  9. <! - And here we call the Test class yo method in synchronous mode ->
  10. <a href = "javascript :;" onclick = "alert ($. mrpc.Test.yo ('foo'))"> foo </ a >


And this is how we describe the classes on the server (config.php, the Perl code will be later, but it's easy to write it yourself using CPAN Class :: Inspector):
')
 class Test
 {
	 public static function yo ($ s)
	 {
		 return 'ok, I got parameter "'. addslashes ($ s). '"';
	 }
 }

 MRpc :: registerRpcClass ('Test');


MRpc class source (MRpc.class.php):

 class MRpc
 {
	 private static $ classes = array ();

	 public static function registerRpcClass ($ className)
	 {
		 MRpc :: $ classes [] = $ className;
	 }

	 public static function getClassNames ()
	 {
		 return MRpc :: $ classes;
	 }

	 public static function getClassMethodNames ($ className)
	 {
		 if (! class_exists ($ className))
		 {
			 require_once ($ className. '.class.php');
		 }
		 return get_class_methods ($ className);
	 }

	 public static function performCall ($ className, $ methodName, $ arguments)
	 {
		 if (! in_array ($ className, MRpc :: $ classes))
		 {
			 header ('HTTP / 1.1 404 Not Found');
			 header ('Status: 404 Not Found');
			 print '{status: false, class: "Not registered"}';
			 exit;
		 }
		 $ methodNames = MRpc :: getClassMethodNames ($ className);
		 if (! in_array ($ methodName, $ methodNames))
		 {
			 header ('HTTP / 1.1 404 Not Found');
			 header ('Status: 404 Not Found');
			 print '{status: false, method: "Not registered"}';
			 exit;
		 }
		 return call_user_func_array (array ($ className, $ methodName), $ arguments);
	 }
 }


And finally, the code index.php (dispatcher):
 require_once (dirname (__ FILE__). '/MRpc.class.php');
 require_once (dirname (__ FILE__). '/config.php');

 header ('Content-type: text / plain');

 if (array_key_exists ('class', $ _REQUEST) && array_key_exists ('method', $ _REQUEST) && array_key_exists ('arguments', $ _REQUEST))
 {
	 header ("Cache-Control: no-cache, must-revalidate");  // HTTP / 1.1
	 header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");  // Date in the past
	 print json_encode (MRpc :: performCall ($ _ REQUEST ['class'], $ _REQUEST ['method'], json_decode ($ _ REQUEST ['arguments'])));
	 exit;
 }

 $ classes = array ();
 foreach (MRpc :: getClassNames () as $ className)
 {
	 $ def = array ('name' => $ className, 'url' => $ _SERVER ['PHP_SELF'], 'methods' => array ());

	
	 foreach (MRpc :: getClassMethodNames ($ className) as $ methodName)
	 {
		 $ def ['methods'] [] = $ methodName;
	 }

	 $ classes [] = $ def;
 }

 ?>
 // JavaScript code - MRPC wrapper

 (function ($)
 {

 $ .mrpc (? php print json_encode ($ classes);?);

 }) (jQuery);

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


All Articles