: This article mainly introduces the principles and applications of php-based jsonrpc. For more information about PHP tutorials, see.
Json rpc is a remote call service in json format, it is a set of Internet-based process calling specifications and a series of implementations that allow programs running in different operating systems and environments. For this remote procedure call, you can use http as the transmission protocol or other transmission protocols. the transmitted content is the json message body.
Below we code a php-based rpc framework, which contains the rpc server and application client;
(1) PHP server RPCserver jsonRPCServer. php
Class jsonRPCServer {/*** processes a request class, which is bound with some request parameters * @ param object $ object * @ return boolean */public static function handle ($ object) {// Determine whether the REQUEST is an rpc json REQUEST if ($ _ SERVER ['request _ method']! = 'Post' | empty ($ _ SERVER ['content _ type']) | $ _ SERVER ['content _ type']! = 'Application/json') {return false;} // reads the input data $ request = json_decode (file_get_contents ('php: // input'), true ); // execute the try {if ($ result = @ call_user_func_array (array ($ object, $ request ['method']), $ request ['params']) interface in the request class) {$ response = array ('id' => $ request ['id'], 'result' => $ result, 'error' => NULL );} else {$ response = array ('id' => $ request ['id'], 'result' => NULL, 'error' => 'un Known method or incorrect parameters ');} catch (Exception $ e) {$ response = array ('id' => $ request ['id'], 'result' => NULL, 'error' => $ e-> getMessage ();} // output if (! Empty ($ request ['id']) {// events don't want response header ('content-type: text/javascript '); echo json_encode ($ response);} return true ;}}
(2) Rpc client, jsonRPCClient. php
Url = $ url; // proxy empty ($ proxy )? $ This-> proxy = '': $ this-> proxy = $ proxy; // debug state empty ($ debug )? $ This-> debug = false: $ this-> debug = true; // message id $ this-> id = 1 ;} /***** @ param boolean $ notification */public function setRPCNotification ($ notification) {empty ($ notification )? $ This-> notification = false: $ this-> notification = true ;} /*** @ param $ method * @ param $ params * @ return bool * @ throws Exception */public function _ call ($ method, $ params) {// Check the request information if (! Is_scalar ($ method) {throw new Exception ('method name has no scalar value');} if (is_array ($ params )) {$ params = array_values ($ params);} else {throw new Exception ('params must be given as array');} if ($ this-> notification) {$ currentId = NULL;} else {$ currentId = $ this-> id;} // Construct a request $ request = array ('method' => $ method, 'params' => $ params, 'id' => $ currentId); $ request = json_enc Ode ($ request); $ this-> debug & $ this-> debug. = '****** Request *****'. "\ n ". $ request. "\ n ". * End Of request *****'. "\ n"; $ opts = array ('http '=> array ('method' => 'post', 'header' => 'Content-type: application/json', 'content' => $ request); // key parts $ context = stream_context_create ($ opts ); if ($ result = file_get_contents ($ this-> url, false, $ context) {$ response = json_decode ($ result, true );} Else {throw new Exception ('unable to connect '. $ this-> url);} // output debugging information if ($ this-> debug) {echo nl2br ($ this-> debug ));} // Check the response information if (! $ This-> notification) {// check if ($ response ['id']! = $ CurrentId) {throw new Exception ('incorrect response id (request id :'. $ currentId. ', response id :'. $ response ['id']. ');} if (! Is_null ($ response ['error']) {throw new Exception ('request error :'. $ response ['error']);} return $ response ['result'];} else {return true ;}}}?>
(3) application instances
(1) server. php
// Member is the testing class require 'member. php '; // server call $ myExample = new member (); // inject the instance jsonRPCServer: handle ($ myExample) or print 'No request';?>
(2) test files: member. phpClass member {public function getName () {return 'Hello word'; // return string }}
(3) client. phpRequire_once 'jsonpcclient. php '; $ url = 'http: // localhost/rpc/server. php '; $ myExample = new jsonRPCClient ($ url); // The client calls try {$ name = $ myExample-> getName (); echo $ name ;} catch (Exception $ e) {echo nl2br ($ e-> getMessage ()).'
'. "\ N ";}
The above describes the principles and applications of json rpc based on php, including some content. I hope to help some friends who are interested in PHP tutorials.