<?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的更多相关文章

随机推荐

  1. python字符串的索引切片和常用操作方法,for循环

    ---恢复内容开始--- 一.字符串的索引与切片 1.索引 s = 'ASDFGHJKL' 有序序列,索引--index:从0开始 s1 = s[0],取出单个元素:A: s1是个全新的字符串和原字符 ...

  2. MVC中DropDownListFor的使用注意事项

    1.在MVC的View页面中使用DropDownListFor时当DropDownListFor是列表是通过后台ViewBag传过来时,当ViewBag中的Key与DropDownListFor一致时 ...

  3. 连接RDS数据库

  4. 阿里云香港ECS搭建Shadowscoks

    注(转https://yijingping.github.io/2016/11/29/fanqiang.html) 1 为什么FQ 作为一个技术人员, 最常用的就是Google.StackOverfl ...

  5. Dash Speed【好题,分治,并查集按秩合并】

    Dash Speed Online Judge:NOIP2016十联测,Claris#2 T3 Label:好题,分治,并查集按秩合并,LCA 题目描述 比特山是比特镇的飙车圣地.在比特山上一共有 n ...

  6. android 使用现成做get请求

    //接受子线程发来的消息 Handler hanler = new Handler() { @Override public void handleMessage(Message msg) { // ...

  7. 线段树动态开点——cf1045G

    只计算半径小的能看到的半径大的,因为如果计算半径大的看到半径小的,虽然q在其范围内,但是小的不一定能看到大的 那么我们将机器人按照半径降序排序 遍历一次,去查询在[x-r,x+r]范围的,智商在[q- ...

  8. React中的表单应用

    React中的表单应用 用户在表单填入的内容,属于用户跟组件的互动,所以不能用this.props读取. var Input = React.createClass({ //初始化组件数据 getIn ...

  9. python 编程 一次错误记录 -1073740791

    原因是发生在我错把类当做实例化对象使用了

  10. some方法过滤

    // 已经存在该tab时跳过 this.tabs.some(item => item.title === option.title) || this.tabs.push(option)