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";
}
php,json,rpc框架
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
- 陈随意2008-爱的总铺师[美华][WAV+CUE]
- 裘德《一棵树所创造的》[320K/MP3][56.21MB]
- 裘德《一棵树所创造的》[24bit 48kHz][FLAC/分轨][355.1MB]
- 卓依娜姆《美人谋 网剧影视原声带》[320K/MP3][48.29MB]
- 张星特.2024-流绪呢喃【中视鸣达】【FLAC分轨】
- 邓紫棋.2010-MYSECRET【蜂鸟音乐】【WAV+CUE】
- 江惠仪.2011-感謝您【禾广娱乐】【FLAC分轨】
- 韩红2005-精选[首版][WAV+CUE]
- 张敬轩.2004-A.M/P.M【几何文化】【WAV+CUE】
- 范文芳1999-真心话电影原声带[HYPE][WAV+CUE]
- 袁培华.1990-我写不出一首情歌给我最爱的你【可登】【WAV+CUE】
- 群星.2007-歌林10年流行岁月12CD【歌林】【WAV+CUE】
- 软硬天师.2006-LONG.TIME.NO.SEE【金牌大风】【WAV+CUE】
- 卓依娜姆《美人谋 网剧影视原声带》[FLAC/分轨][162.27MB]
- 国风大师纯音系列《蒋倩 :古筝演奏家》1CD[MP3][160.7MB]