<?php

/**
* Simple JSON-RPC interface.
*/
namespace org; class JsonRpc
{
protected $host, $port, $version;
protected $id = 0; function __construct($host, $port, $version="2.0")
{
$this->host = $host;
$this->port = $port;
$this->version = $version;
} function request($method, $params=array())
{
$data = array();
$data['jsonrpc'] = $this->version;
$data['id'] = $this->id++;
$data['method'] = $method;
$data['params'] = $params; $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, json_encode($data)); $ret = curl_exec($ch); if($ret !== FALSE)
{
$formatted = $this->format_response($ret); if(isset($formatted->error))
{
//throw new RPCException($formatted->error->message, $formatted->error->code);
return false;
}
else
{
return $formatted;
}
}
else
{
throw new \think\Exception("Server did not respond");
}
} function format_response($response)
{
return @json_decode($response);
}
} // class RPCException extends Exception
// {
// public function __construct($message, $code = 0, Exception $previous = null)
// {
// parent::__construct($message, $code, $previous);
// } // public function __toString()
// {
// return __CLASS__ . ": ".(($this->code > 0)?"[{$this->code}]:":"")." {$this->message}\n";
// }
// }

jsonRPC的更多相关文章

  1. NodeJS POST Request Over JSON-RPC

    1.npm install art-template2.npm  install request3.在app.js中加入以下代码转html: var template = require('art-t ...

  2. 使用Jayrock开源组件开发基于JSON-RPC协议的接口

    最近接手一个以前的项目,无意间发现此项目开发接口的组件:Jayrock(接口组件估计用的少,用的最多的估计是这个Jayrock.json.dll,用于解析json) 以下是Jayrock的介绍官网: ...

  3. Lua JSONRPC学习笔记

    JSON RPC JSON RPC 为利用json数据格式来执行远程调用方式, 作用同xmlrpc,不过与xmlrpc相比, jsonrpc更加轻量,json更加节省数据量,更加可读性高. 官网网站: ...

  4. JSON-RPC轻量级远程调用协议介绍及使用

    这个项目能够帮助开发人员利用Java编程语言轻松实现JSON-RPC远程调用.jsonrpc4j使用Jackson类库实现Java对象与JSON对象之间的相互转换.jsonrpc4j包含一个JSON- ...

  5. PHPCURL直接访问JSONRPC服务

    <?php $ch = curl_init(); $url = 'http://localhost/jsonrpc?tm='.time().mt_rand (100,999); //参数是为了防 ...

  6. 利用QObject反射实现jsonrpc

    1.jsonrpc请求中的params数组生成签名 static QString signatureFromJsonArray(const QJsonArray &array) { QStri ...

  7. A simple json-rpc case for bitcoin blockchains

    #!/usr/bin/env python import json import jsonrpc import requests #url = "http://user:password@i ...

  8. 学习json-rpc

    最近做一个和SmartHome相关的项目,文档不全不说,连个像样的Demo都没,痛苦!!当然,这是题外话.今天来说说项目中主要用到的通讯协议:json-rpc,简单地说,它是以json格式进行的远程调 ...

  9. C语言JSON-RPC

         近期对json-rpc比較感兴趣,思想非常easy,并且看到了非常多不同语言的实现.在github上 hmngomes 的 json-rpc-c (实现的是server端,基于TCP流),短 ...

  10. Go学习笔记 - 使用jsonrpc进行远程访问

    Go学习笔记 - 使用jsonrpc进行远程访问 JSON-RPC JSON-RPC是一个轻量级的远程调用协议,简单易用. 请求数据体: { "method": "get ...

随机推荐

  1. UMP系统架构 LVS

  2. python pip安装扩展报错

    1.安装tldr报错 (1)报错详情: [root@linuxnode1 ~]# pip install tldrCollecting tldr Downloading https://files.p ...

  3. win10下aria2和BaiduExporter的配置和安装

    一.aria2的配置 下载 aria2下载地址: https://github.com/aria2/aria2/releases 链接:https://pan.baidu.com/s/1olJyZkX ...

  4. ros python

    https://www.ncnynl.com/archives/201611/1059.html python的节点需要对节点设置权限为可执行,在.py文件所在的路径执行如下命令 $ touch ta ...

  5. C#多线程实现方法——线程池(Thread Pool)

    ThreadPool使用 同步机制   ThreadPool使用 需要定义waitcallback委托形式如 public delegate void WaitCallback(object stat ...

  6. js 获取字符串中某字符第二次出现的下标

    var res = "a-b-c-d";var index = find(res,'-',1); //字符串res中第二个‘-’的下标 var ress = res.substri ...

  7. mysql80版本—yum安装—图文全过程

    这是官网的Quick Giude:https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/ 以下为自己安装的步骤: 第一步:下载.rpm安装包 ...

  8. Lint found fatal errors while assembling a release target问题的解决方案

    此问题发生在编译为 release 版本时,出现错误提示如下: Lint found fatal errors while assembling a release target. To procee ...

  9. visio去除直线交叉处的歪曲

    1 问题描述 Visio画图时,两根直线交叉时,总是默认会出现一个跨线的标志,如下图所示: 2 解决办法 在2007前的版本,可以通过以下方式解决: 选中线条,然后菜单的格式->行为->连 ...

  10. vue页面刷新数据丢失问题

    参考: https://blog.csdn.net/aliven1/article/details/80743470          https://blog.csdn.net/liang37712 ...