A simple jsonrpc framework instance implemented by php. A simple jsonrpc framework instance implemented by php this article mainly introduces a simple jsonrpc framework instance implemented by php, this article provides the RPC server and client code and a simple json rpc framework instance implemented by the application instance php.
This article mainly introduces a simple json rpc framework instance implemented by php. This article provides the RPC server and client code and application instances. For more information, 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
The code is as follows:
Class jsonRPCServer {
/**
* Process 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 interface in the request class
Try {
If ($ result = @ call_user_func_array (array ($ object, $ request ['method']), $ request ['params']) {
$ Response = array ('id' => $ request ['id'], 'result' => $ result, 'error' => NULL );
} Else {
$ Response = array ('id' => $ request ['id'], 'result' => NULL,
'Error' => 'Unknown method or incorrect parameters ');}
} Catch (Exception $ e ){
$ Response = array ('id' => $ request ['id'], 'result' => NULL, 'error' => $ e-> getMessage ());
}
// Json output
If (! Empty ($ request ['id']) {// notifications don't want response
Header ('content-type: text/javascript ');
Echo json_encode ($ response );
}
Return true;
}
}
(2) Rpc client, jsonRPCClient. php
The code is as follows:
/*
*/
Class jsonRPCClient {
Private $ debug;
Private $ url;
// Request id
Private $ id;
Private $ notification = false;
/**
* @ Param $ url
* @ Param bool $ debug
*/
Public function _ construct ($ url, $ debug = false ){
// Server URL
$ This-> 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_encode ($ 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 components
$ 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 to '. $ 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 ('encrect 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
The code is as follows:
Require_once 'jsonserver. php ';
The code is as follows:
// Member is a test class
Require 'member. php ';
// Server call
$ MyExample = new member ();
// Inject instance
JsonRPCServer: handle ($ myExample)
Or print 'No request ';
?>
(2) test files: member. php
The code is as follows:
Class member {
Public function getName (){
Return 'Hello word'; // return a string
}
}
(3) client. php
The code is as follows:
Require_once 'jsonclient. php ';
$ Url = 'http: // localhost/rpc/server. php ';
$ MyExample = new jsonRPCClient ($ url );
// Client call
Try {
$ Name = $ myExample-> getName ();
Echo $ name;
} Catch (Exception $ e ){
Echo nl2br ($ e-> getMessage ()).'
'. "\ N ";
}
Rpc framework example this article mainly introduces a simple json rpc framework instance implemented by php. This article provides the RPC server and client code and application examples...