<?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. 如何给DropDownListFor设置默认值

    1.直接赋值: @Html.DropDownListFor(o => o.ParentId, ViewBag.root as IEnumerable<SelectListItem>, ...

  2. 【笔记篇】Ubuntu一日游

    今天做数据的时候在Windows下出问题了(好像是爆栈了QAQ) 于是乎就打开了自己的Ubuntu虚拟机… 然而沉迷Windows的我已经忘记自己对这台虚拟机做过什么(比如装残了一个ycm自己都不知道 ...

  3. C++ 系列:Boost Thread 编程指南

    转载自:http://www.cppblog.com/shaker/archive/2011/11/30/33583.html 作者: dozbC++ Boost Thread 编程指南0 前言1 创 ...

  4. Android开发 输入法调用学习

    方法一(如果输入法在窗口上已经显示,则隐藏,反之则显示) InputMethodManager imm = (InputMethodManager) getSystemService(Context. ...

  5. UVALive-3722 留个坑,为什么费马小定理求逆元不对??

    #include <iostream> #include <cstdlib> #include <queue> #include <algorithm> ...

  6. [JZOJ3167] 【GDOI2013模拟3】查税

    题目 描述 题目大意 维护一个有一次函数组成的序列 具体来说,对于位置xxx,现在的值为sx+zx∗(T−tx)s_x+z_x*(T-t_x)sx​+zx​∗(T−tx​) 有两个操作,修改某个位置上 ...

  7. 第九章 Odoo 12开发之外部 API - 集成第三方系统

    Odoo 服务器端带有外部 API,可供网页客户端和其它客户端应用使用.本文中我们将学习如何在我们的客户端程序中使用 Odoo 的外部 API.为避免引入大家所不熟悉的编程语言,此处我们将使用基于 P ...

  8. LUOGU P1337 [JSOI2004]平衡点 / 吊打XXX(模拟退火)

    传送门 解题思路 学习了一下玄学算法--模拟退火,首先要求平衡处,也就是求势能最小的地方,就是求这个点到所有点的距离*重量最小.剩下的几乎是模拟退火的板子了. #include<iostream ...

  9. MSI右键添加管理员运行.reg

    Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Msi.Package\shell\runas]@="右键以管理员运行MSI& ...

  10. HTTP各版本的区别

    什么是HTTP和HTTPS? HTTP是浏览器与服务器之间以明文的方式传送内容的一种互联网通信协议. HTTPS是在HTTP的基础上主要基于SPDF协议结合SSL/TLS加密协议,客户端依靠证书验证服 ...