This article mainly introduces a simple json rpc framework example implemented in php. This article gives the RPC server and client Code and application examples, friends in need can refer to it
json rpc is a remote calling service with json as the message format. It is a set of specifications and a series of implementations that allow programs running in different operating systems and different environments to implement Internet-based process calls. This remote procedure call can use http as the transmission protocol or other transmission protocols. The transmitted content is the json message body.
Below we code a set of RPC framework based on PHP. This framework includes RPC server and application client;
(1) PHP server RPCserver jsonRPCServer.php
The code is as follows:
(2) Rpc client, jsonRPCClient.php
Copy the code The code is as follows:
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 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;
}
// 拼装成一个request请求
$request = array( 'method' => $method, 'params' => $params,'id' => $currentId);
$request = json_encode($request);
$this->debug && $this->debug.='***** Request *****'."n".$request."n".'***** End Of request *****'."nn";
$opts = array ('http' => array (
'method' => 'POST',
'header' => 'Content-type: application/json',
'content' => $request
));
// 关键几部
$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);
}
// 输出调试信息
if ($this->debug) {
echo nl2br(($this->debug));
}
// 检验response信息
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;
}
}
}
?>
(三) 应用实例
(1)服务端 server.php
代码如下:
代码如下:
(2)测试类文件,member.php
代码如下:
(3)客户端 client.php
代码如下:
$url = 'http://localhost/rpc/server.php';
$myExample = new jsonRPCClient($url);
// 客户端调用
try {
$name = $myExample->getName();
echo $name ;
} catch (Exception $e) {
echo nl2br($e->getMessage()).'
'."n";
}