<?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. 自动生成DTO(Sugar框架)

    step1:启动api项目 step2:使用postman工具,填上接口地址http://localhost:7788/api/automapper/AutoMapperSuper step3:表格数 ...

  2. linux 2.6 内核的移植

    内核移植过程   下载 linux 内核 从 http://www.kernel.org/pub/linux/kernel/v2.6/linux­2.6.14.1.tar.bz2 下载 linux­2 ...

  3. 【学术篇】oj.jzxx.net2701 无根树

    这是一道来自OIerBBS的题目.. 原帖地址:http://www.oierbbs.com/forum.php?mod=viewthread&tid=512?fromuid=71 (似乎是个 ...

  4. 【珍惜时间】iReport

    项目很点意思,感觉很高超的样子 先放下项目的github地址:https://github.com/tctangyanan/iReport 感谢各位伟大的程序员无私的分享自己的技术 老规矩,我们会运行 ...

  5. var、fucntion关键字优先级问题

    情况1:使用var关键字定义的变量优先被声明 console.log(a);    // undefined var a = 20; /* var a =20; (1)声明变量a --  优先被执行, ...

  6. HTML和css简单日常总结

    今天主要学习了两个部分:第一部分html:在w3school学习了一些标签的使用和一些属性和方法,例如一些标签<a>,<h1><h6><from>< ...

  7. springboot与任务(邮件任务)

    邮件发送需要引入spring-boot-starter-mail Spring Boot 自动配置MailSenderAutoConfiguration 定义MailProperties内容,配置在a ...

  8. PDO连续query()失败问题

    设置了非缓冲查询(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY)以后,连续query会导致下一次结果为false $conn = "mysql:host=127.0. ...

  9. https,http和ssl的关系

    https,http和ssl这三者是什么关系呢? http是超文本传输协议,浏览器利用这一协议就可以访问各种网站了. ssl指Secure Sockets Layer 安全套接层,是一种保证网络数据传 ...

  10. Git命令汇总(转)

    转自:http://blog.csdn.net/esrichinacd/article/details/17645951 图片看不清请点击放大