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的更多相关文章
随机推荐
- TortoiseGit可能遇到Permission denied (publickey).
1.检测是不是没设置公钥和私钥 2.公钥有没有添加到git账户里面去 3.检测如下图路径正确不正确
- 阿里云OSS简单上传本地文件
上传本地文件 # -*- coding: utf-8 -*- import oss2 # 阿里云主账号AccessKey拥有所有API的访问权限,风险很高.强烈建议您创建并使用RAM账号进行API访问 ...
- 廖雪峰Java11多线程编程-3高级concurrent包-8CompletableFuture
使用Future可以获得异步执行结果 Future<String> future = executor.submit(task); String result = future.get() ...
- 在Xsheel Linux上安装nodejs和npm
最近window系统转向linux系统开发,linux系统的确适合程序员的开发. 作为前端安装了nodejs和npm,遇到了一些坑,赶紧记录下来 第一种安装方法:安装nodejs : sudo a ...
- Mac 下搭建vue开发环境
tips:一定要有翻墙工具如lanter,另外要保证网速OK. 1. 首先需要安装homebrew liukingdeMBP:~ liuking$ /usr/bin/ruby -e "$(c ...
- 菜鸟nginx源码剖析数据结构篇(十) 自旋锁ngx_spinlock[转]
菜鸟nginx源码剖析数据结构篇(十) 自旋锁ngx_spinlock Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csd ...
- diskpart
比如格式化成fat32 format fs=fat32 quick 比鼠标方便 如何使用: 打开cmd输入diskpart进入命令 首先 list disk 然后 然后 clean 然后 create ...
- PKU--2184 Cow Exhibition (01背包)
题目http://poj.org/problem?id=2184 分析:给定N头牛,每头牛都有各自的Si和Fi 从这N头牛选出一定的数目,使得这些牛的 Si和Fi之和TS和TF都有TS>=0 F ...
- js (function(){}()),(function(){})(),$(function(){});之间的区别
参考:https://blog.csdn.net/stpice/article/details/80586444 (function(){}()), (function(){})() 均为立即执行函数 ...
- Tornado demo3 - tcpecho分析
在这个demo中,主要是使用了Tornado中异步的TCP client和server来实现一个简单的echo效果(即客户端发送的message会从server端返回到client).代码的githu ...