json rpc 是一种以json为消息格式的远程调用服务,它是一套允许运行在不同操作系统、不同环境的程序实现基于Internet过程调用的规范和一系列的实现。这种远程过程调用可以使用http作为传输协议,也可以使用其它传输协议,传输的内容是json消息体。
下面我们code一套基于php的rpc框架,此框架中包含rpc的服务端server,和应用端client;
(一)PHP服务端RPCserver jsonRPCServer.php
复制代码 代码如下:
class jsonRPCServer {
/**
*处理一个request类,这个类中绑定了一些请求参数
* @param object $object
* @return boolean
*/
public static function handle($object) {
// 判断是否是一个rpc json请求
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);
// 执行请求类中的接口
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 格式输出
if (!empty($request['id'])) { // notifications don't want response
header('content-type: text/javascript');
echo json_encode($response);
}
return true;
}
}
(二)Rpc客户端,jsonRPCClient.php
复制代码 代码如下:
<"\n".$request."\n".'***** End Of request *****'."\n\n";
$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;
}
}
}
"codetitle">复制代码 代码如下:
<"codetitle">复制代码 代码如下:
// member 为测试类
require 'member.php';
// 服务端调用
$myExample = new member();
// 注入实例
jsonRPCServer::handle($myExample)
or print 'no request';
"codetitle">复制代码 代码如下:
class member{
public function getName(){
return 'hello word ' ; // 返回字符串
}
}
(3)客户端 client.php
复制代码 代码如下:
require_once 'jsonRPCClient.php';
$url = 'http://localhost/rpc/server.php';
$myExample = new jsonRPCClient($url);
// 客户端调用
try {
$name = $myExample->getName();
echo $name ;
} catch (Exception $e) {
echo nl2br($e->getMessage()).'<br />'."\n";
}
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]