JosnRpcClient
<?php /**
* Simple JSON-RPC interface.
*/
namespace org; class JosnRpcClient
{
protected $host;
protected $port;
protected $version;
protected $debug;
protected $id = 0; /**
* 初始化数据
* @param $host 主机IP
* @param $port 端口
* @param $debug debug模式(true or false)
* @param $version jsonrpc 版本号
* @param bool $debug
*/
public function __construct($host, $port, $debug = false, $version = "2.0")
{
$this->host = $host;
$this->port = $port;
$this->version = $version;
$this->debug = $debug;
} /**
* 请求核心方法
* @param $method 回调方法名
* @param $params 参数数组
* @return array 返回结果数组
*/
public function request($method, $params=array())
{
// 检验request信息
if (!is_scalar($method)) {
throw new \think\Exception('Method name has no scalar value');
}
if (is_array($params)) {
$params = array_values($params);
} else {
throw new \think\Exception('Params must be given as array');
} // 封装请求数据
$request = json_encode(array(
'jsonrpc' => $this->version,
'method' => $method,
'params' => $params,
'id' => $this->id++
)); // 是否是debug模式
$this->debug && $this->debug.='***** Request *****'."\n".$request."\n".'***** End Of request *****'."\n\n"; // curl请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->host);
curl_setopt($ch, CURLOPT_PORT, $this->port);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request); $ret = curl_exec($ch); // 输出调试信息
if ($this->debug) {
echo nl2br(($this->debug));
} if ($ret !== false) {
$response = json_decode($ret); if (isset($response->error)) {
//throw new RPCException($formatted->error->message, $formatted->error->code);
throw new \think\Exception('Request error: '.$response->error);
} else {
return $response;
}
} else {
throw new \think\Exception("Server did not respond: ".$this->host.':'.$this->port);
}
}
}
JosnRpcClient的更多相关文章
随机推荐
- Input:type属性
1.button:定义可点击的按钮(通常与 JavaScript 一起使用来启动脚本). <input id="" type="button" name= ...
- C#真他妈神奇,一个函数都不用写就能实现一个简单的邮件发送工具
MailMessage EmaillMessage = new MailMessage( //创建一个对象 new MailAddress(loning.Te ...
- Java开发系列-注解
概述 在JDK5之后提供了一个新特性,和类.接口同级.定义时使用的关键字是@interface.注解主要有三个作用,分别是编译检查.替代配置文件.定义注解(元注解).分析代码(用到反射).注解的本质就 ...
- MYSQL随笔心得1
cmd链接数据库命令: 输入密码进入 显示全部的数据库: 退出服务器连接,还有/p quit 非关系型数据库:NOSQL,not only sql 不仅仅是SQL 代表:redis,mongodb
- 使用log4j实现日志API
添加SLF4J依赖,用于提供日志API, 使用log4j作为实现 1.pom.xml添加SLF4J依赖 <!-- SLF4J --> <dependency> <grou ...
- 2、java变量+零碎知识点
1>展示console:window--show view--console2>创建工程 右键--new---java project 文件夹 jre src 所有的java类都在src中 ...
- Ubuntu usb设备端口号绑定
1.将串口设备插入USB口,通过lsusb查看端口信息.例如: ID 1a86:7523 表示usb设备的ID(这个ID由芯片制造商设置,可以唯一表示该设备) 1a86 usb_device_desc ...
- 服务器迁移部署OmsWeb
绑定 基本设置 高级设置
- Hack Tools
Tools 2011-03-17 13:54:36| 分类: Security|举报|字号 订阅 Packet Shaper:Nemesis: a command line packet s ...
- leetcode 996. Number of Squareful Arrays
给定一个长度小于 12 的数组 要求排列方式的种数 使得相邻和为完全平方 不考虑数学结构 将问题转化为 一笔画问题 和为完全平方代表 之间存在通路 回溯法 N^N 记忆化搜索 NN 2^N 判断是否是 ...