php curl jsonrpc,JsonRPC: Lightweight Json-RPC 2.0 client/server in PHP extension
JsonRPC 2.0 Client and Server
轻量级,高性能 JsonRPC 2.0 客户端和服务端的php扩展,基于 multi_curl + epoll的并行客户端。Jsonrpc_Client使用libcurl库的并行接口调取服务,使用IO多路复用的epoll去监听curl的IO事件,使用协程可以同步返回rpc的数据,但底层其实是异步的。Jsonrpc_Server支持php-fpm或swoole。遵守http://www.jsonrpc.org/协议规范。
English
特性
JSON-RPC 2.0协议规范
并发curl与epoll结合的并行客户端
php-fpm中持久化epoll
php-fpm中持久化curl_multi队列
默认使用YAJL解析JSON
服务端支持请求与通知
Linux系统(需要支持epoll)
PHP环境
PHP 5.3.*
PHP 5.4.*
PHP 5.5.*
PHP 5.6.*
安装
$/path/to/phpize
$./configure --with-php-config=/path/to/php-config
$make && make install
服务端
接口
Jsonrpc_Server::__construct(mixed $payload, array $callbacks, array $classes)
Jsonrpc_Server::register(string $name, mixed $closure)
Jsonrpc_Server::bind(string $procedure, mixed $classname, string $method)
Jsonrpc_Server::jsonformat()
Jsonrpc_Server::rpcformat(mixed $payload)
Jsonrpc_Server::executeprocedure(string $procedure, array $params)
Jsonrpc_Server::executecallback(object $closure, array $params)
Jsonrpc_Server::executemethod(string $class, string $method, array $params)
Jsonrpc_Server::execute(boolean $response_type)
注册函数
$server = new Jsonrpc_Server();
// style one function variable
$add1 = function($a, $b){
return $a + $b;
};
$server->register('addition1', $add1);
// style two function string
function add2($a, $b){
return $a + $b;
}
$server->register('addition2', 'add2');
// style three function closure
$server->register('addition3', function ($a, $b) {
return $a + $b;
});
//style four class method string
class Api
{
static public function add($a, $b)
{
return $a + $b;
}
}
$server->register('addition4', 'Api::add');
echo $server->execute();
//output >>>
//{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error"}}
?>
绑定方法
$server = new Jsonrpc_Server();
class Api
{
static public function add($a, $b)
{
return $a + $b;
}
public function newadd($a,$b){
return $a + $b;
}
}
$server->bind('addition5', 'Api', 'add');
$server->bind('addition6', $a=new Api, 'newadd');
echo $server->execute();
//output >>>
//{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error"}}
?>
swoole jsonrpc server
$http = new swoole_http_server("127.0.0.1", 9501);
function add($a, $b){
return $a + $b;
}
$http->on('Request', function($request, $response){
if ($request->server['request_uri'] == "/jsonrpc_server"){
$payload = $request->rawContent();
$jsr_server = new Jsonrpc_Server($payload);
$jsr_server->register('addition', 'add');
$res = $jsr_server->execute();
$response->end($res);
unset($payload);
unset($jsr_server);
unset($res);
}else {
$response->end("error");
}
});
$http->start();
?>
客户端
接口
Jsonrpc_Client::__construct(boolean $persist)
Jsonrpc_Client::call(string $url, string $procedure, array $params, mixed $id)
Jsonrpc_Client::connect(string $url)
Jsonrpc_Client::__call(string $procedure, array $params)
Jsonrpc_Client::execute(boolean $response_type)
Jsonrpc_Client::authentication(string $username, string $password)
Jsonrpc_Client::__destruct()
持久化
Jsonrpc_client(1) 参数为1的时候,将epoll和curl_multi队列两个资源进行持久化,默认使用非持久化。
直接调用
$client = new Jsonrpc_Client(1);
$client->connect('http://localhost/server.php');
$client->addition1(3,5);
$result = $client->execute();
?>
并行调用
$client = new Jsonrpc_Client(1);
$client->call('http://localhost/server.php', 'addition1', array(3,5));
$client->call('http://localhost/server.php', 'addition2', array(10,20));
/* ... */
$result = $client->execute();
var_dump($result);
//output >>>
/*
array(2) {
[0]=>
array(3) {
["jsonrpc"]=>
string(3) "2.0"
["id"]=>
int(110507766)
["result"]=>
int(8)
}
[1]=>
array(3) {
["jsonrpc"]=>
string(3) "2.0"
["id"]=>
int(1559316299)
["result"]=>
int(30)
}
...
}
*/
?>
自定义 id
$client = new Jsonrpc_client(1);
$client->call('http://localhost/server.php', 'addition', array(3,5),"custom_id_001");
$result = $client->execute();
var_dump($result);
//output >>>
/*
array(1) {
[0]=>
array(3) {
["jsonrpc"]=>
string(3) "2.0"
["id"]=>
string(13) "custom_id_001"
["result"]=>
int(8)
}
}
*/
?>
YAJL 生成/解析
Interface
Jsonrpc_Yajl::generate(array $array)
Jsonrpc_Yajl::parse(string $json)
生成
$arr = array(
1,
"string",
array("key"=>"value")
);
var_dump(Jsonrpc_Yajl::generate($arr));
/* ==>output
string(28) "[1,"string",{"key":"value"}]";
*/
?>
解析
$str = '[1,"string",{"key":"value"}]';
var_dump(Jsonrpc_Yajl::parse($str));
/* ==>output
array(3) {
[0]=>
int(1)
[1]=>
string(6) "string"
[2]=>
array(1) {
["key"]=>
string(5) "value"
}
}
*/
?>
常见错误信息
jsonrpc 2.0 错误信息
// 语法解析错误
{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error"}}
// 无效请求
{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"Invalid Request"}}
// 找不到方法
{"jsonrpc":"2.0","id":null,"error":{"code":-32601,"message":"Method not found"}}
// 无效的参数
{"jsonrpc":"2.0","id":null,"error":{"code":-32602,"message":"Invalid params"}}
//
HTTP协议错误信息
// 400
{"jsonrpc":"2.0","id":null,"error":{"code":-32400,"message":"Bad Request"}}
// 401
{"jsonrpc":"2.0","id":null,"error":{"code":-32401,"message":"Unauthorized"}}
// 403
{"jsonrpc":"2.0","id":null,"error":{"code":-32403,"message":"Forbidden"}}
// 404
{"jsonrpc":"2.0","id":null,"error":{"code":-32404,"message":"Not Found"}}
// 500
{"jsonrpc":"2.0","id":null,"error":{"code":-32500,"message":"Internal Server Error"}}
// 502
{"jsonrpc":"2.0","id":null,"error":{"code":-32502,"message":"Bad Gateway"}}
...
// unknow
{"jsonrpc":"2.0","id":null,"error":{"code":-32599,"message":"HTTP Unknow"}}
curl错误信息
// 1 CURLE_UNSUPPORTED_PROTOCOL
{"jsonrpc":"2.0","id":null,"error":{"code":-32001,"message":"Curl Unsupported Protocol"}}
// 2 CURLE_FAILED_INIT
{"jsonrpc":"2.0","id":null,"error":{"code":-32002,"message":"Curl Failed Init"}}
// 3 CURLE_URL_MALFORMAT
{"jsonrpc":"2.0","id":null,"error":{"code":-32003,"message":"Curl Url Malformat"}}
// 4
{"jsonrpc":"2.0","id":null,"error":{"code":-32004,"message":"Curl Not Built In"}}
// 5 CURLE_COULDNT_RESOLVE_PROXY
{"jsonrpc":"2.0","id":null,"error":{"code":-32005,"message":"Curl Couldnt Resolve Proxy"}}
// 6 CURLE_COULDNT_RESOLVE_HOST
{"jsonrpc":"2.0","id":null,"error":{"code":-32006,"message":"Curl Couldnt Resolve Host"}}
// 7 CURLE_COULDNT_CONNECT
{"jsonrpc":"2.0","id":null,"error":{"code":-32007,"message":"Curl Couldnt Connect"}}
...
// CURL ERROR UNKNOW
{"jsonrpc":"2.0","id":null,"error":{"code":-32099,"message":"Curl Error Unknow"}}
php curl jsonrpc,JsonRPC: Lightweight Json-RPC 2.0 client/server in PHP extension相关推荐
- jsonrpc php使用,利用php怎么编写一个json rpc框架
利用php怎么编写一个json rpc框架 发布时间:2020-12-25 15:24:41 来源:亿速云 阅读:71 作者:Leah 本篇文章为大家展示了利用php怎么编写一个json rpc框架, ...
- jsonrpc php使用,php实现的一个简单json rpc框架实例
json rpc 是一种以json为消息格式的远程调用服务,它是一套允许运行在不同操作系统.不同环境的程序实现基于Internet过程调用的规范和一系列的实现.这种远程过程调用可以使用http作为传输 ...
- C语言编写的简单HTTP json RPC
目录 0 概述 0.0 参考 0.1 HTTP 服务 0.2 JSON RPC HTTP错误码 0.3 目录结构 1 使用 1.1 接口访问示例 1.2 方法注册 1.3 修改监听端口 4 源码获取 ...
- shell处理curl返回数据_linux shell中curl 发送post请求json格式问题的处理方法
今天在linux中使用curl发送一个post请求时,带有json的数据,在发送时发现json中的变量没有解析出来 如下 curl -i -X POST -H 'Content-type':'appl ...
- CURL 钉钉机器人 JSON 传参
在日常工作中,经常需要定时执行一些任务.当前我们用的是通过钉钉机器人通知任务完成情况.使用钉钉机器人通知非常简单,通过 curl 命令行工具即可发送通知. curl 'https://oapi.din ...
- php模拟post提交json数据,如何在PHP中利用curl模拟post提交json数据
如何在PHP中利用curl模拟post提交json数据 发布时间:2021-02-05 16:30:19 来源:亿速云 阅读:63 作者:Leah 本篇文章为大家展示了如何在PHP中利用curl模拟p ...
- Hadoop基于Protocol Buffer的RPC实现代码分析-Server端--转载
原文地址:http://yanbohappy.sinaapp.com/?p=110 最新版本的Hadoop代码中已经默认了Protocol buffer(以下简称PB,http://code.goog ...
- 报错curl: (7) Failed to connect to 127.0.0.1 port xxxx: Connection refused
(pyenv install xxx) 报错curl: (7) Failed to connect to 127.0.0.1 port xxxx: Connection refused的解决方法 问题 ...
- 报错SyntaxError: Unexpected token T in JSON at position 0 at JSON.parse (<anonymous>)的解决方法
报错SyntaxError: Unexpected token T in JSON at position 0 at JSON.parse ()的解决方法 刚刚开始学习node.js会报很多各种各样的 ...
- 93.<报错 SyntaxError: Unexpected token R in JSON at position 0 at JSON.parse (<anonymous>)的解决方法>
@[TOC]( 报错 SyntaxError: Unexpected token R in JSON at position 0 at JSON.parse ()的解决方法) 报错代码: const ...
最新文章
- Oracle 11g Release 1 (11.1) Data Pump 导入模式
- iis7.5支持html5,IIS7.5 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面...
- Qt使用OpenCV读取图片练习
- JNI字段描述符[Ljava/lang/String
- 【struts2】第一个struts2实例
- Spring 使用context:annotation-config的设置
- Mac下安装LNMP(Nginx+PHP5.6)环境
- Android 6.0 API
- 世上最杰出程序员,B 语言、Unix 之父为玩游戏,写了个操作系统
- logback日志框架的简单使用
- c语言锁存器写入1,总线接口作两种用途,为何就要用到锁存器
- 学计算机要选什么科,新高考省份想学计算机专业怎么选科
- Dev-C++ 一直提示源文件未编译,原因及解决办法
- zabbix日志监控
- linux系统下使用gcc编译C++程序出现XXX未定义的引用的处理
- Borůvka算法学习小记
- MTK keypad调试,扩张键盘IC AW9523
- LNK1104:无法打开文件 “.exe”
- 大话Verilog——Verilog入门(一)
- 画属于自己的STM32C8T6PCB电路板
热门文章
- Djangobook
- intellij idea编辑器好看炫酷主题配色方案推荐
- 360浏览器兼容css,css样式怎样调360浏览器的兼容性
- 高等数学和计算机相结合的论文,高等数学教学与专业结合模式的初步探索论文...
- sqlite数据库可视化工具—— DB.Browser安装说明
- 【MPLS ***】基础知识:模型、PE路由器、VRF、RD及RT
- 出现Please make sure you have the correct access rights and the repository exists.问题解决
- 三星S7Edge刷了鉴机大师的Android8的增强版,超级流畅省电_我是亲民_新浪博客
- 番茄时间管理法——学会专注
- bcdedit无法打开启