<?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. mysqldump使用记录

    #导出单个数据库:结构 无数据 [root@localhost ~]#mysqldump -h127.0.0.1 -uroot -p --opt --no-data db_name >~/db_ ...

  2. (转)FastCgi与PHP-fpm之间是个什么样的关系

    首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. web server(比如说nginx)只是内容的分发者.比如,如果请求/index.h ...

  3. Python全栈开发:装饰器实例

    #!/usr/bin/env python # -*- coding;utf-8 -*- """ 1.将outer函数放入内存 2.遇见@ + 函数名,则将该函数转换为装 ...

  4. Axios的get和post请求写法

    执行get请求 // 为给定 ID 的 user 创建请求 axios.get('/user?ID=12345') .then(function (response) { console.log(re ...

  5. uoj349 即时战略

    题意:这是一道交互题.交互库中有一棵树.一开始只有1节点已知.需要在T次询问内使得n个节点都已知.一次询问explore(x,y),返回从x到y路径上第一个点,并将返回点标记为已知. 数据有区分. 标 ...

  6. 浏览器禁用Cookie后的Session处理

    1. 实现购物车, 可以基于Cookie, 也可以基于Session, 若服务器性能较差, 可以考虑基于Cookie实现购物车 2. 解决方案: URL重写 把用户可能点的每一个超链接后面,都跟上用户 ...

  7. 完全卸载之前8.0的Mysql,安装5.5mysql

    完全卸载: https://blog.csdn.net/sxingming/article/details/52601250 安装mysql5.5: https://blog.csdn.net/fly ...

  8. SPSS输出结果统计表与统计图的专业性编辑及三线表定制格式

    SPSS输出结果统计表与统计图的专业性编辑及三线表定制格式 世界前三统计软件,SPSS最容易学习,但SPSS默认输出的统计表与统计图美观度与专业度不够好,离发表水平尚有距离,本期咱们就谈谈SPSS图表 ...

  9. webservice技术--服务器端

    1.webservice实现单点登录具体逻辑为 ①软通web端作为客户端,请求wi社区后台,进行登录请求 ②wi社区后台验证t,核实无误后,走登录逻辑,直接进入欢迎页 ③如果有错误,封装错误xml,返 ...

  10. Axure中表格使用的技巧

    对于新手来说,用Axure做一个表格是一件麻烦的事情.本文教你如何快速学会Axure表格的基础应用. (1)Axure制作基本表格的使用 可以使用“线框图”中的“表格”控件来制作一些简单的表格,同时A ...