fozlcz / jsonrpc2
适用于通用目的的jsonrpc2实现。包含Nette框架的测试套件。
v1.2.1
2020-04-21 18:18 UTC
Requires
- php: >=5.3.2
This package is not auto-updated.
Last update: 2024-09-14 12:44:34 UTC
README
此库包含一个PHP实现的JSON-RPC版本2,包括客户端和服务器。
安装
将lib
文件夹的内容下载到您的项目中。然后简单地将库包含进去。
include "lib/Server.php";
服务器方法
$server = new Lightbulb\Json\Rpc2\Server;
// Class based where all the methods in myClass are exposed as user.method
$server->user = new MyClass;
// Anything that is "callable", either built in PHP functions or your own
$server->upper = 'strtoupper';
$server->userClean = 'userClean';
// Anonymous functions work too
$server->firstTwo = function($str) { return substr($str,0,2); };
// Force a namespace to map to an object method
$server->{'mytesthandler.myfunc'} = array($myObject, 'myMethod');
// Static method calls work
$server->myStaticHandler = 'MyStaticClass::theStaticFunction';
提供给服务器的方法可以通过编号或命名参数进行调用(有关json-rpc规范的详细信息,请参阅此处: http://groups.google.com/group/json-rpc/web/json-rpc-2-0?pli=1 )
服务器类尊重事件方法的绑定
// Bind events
$server->onBeforeCall[] = function($server) {};
$server->onBeforeCall[] = function($server) {};
$server->onSuccess[] = function($server) {};
$server->onError[] = function($server) {};
有关详细用法,请参阅服务器和客户端类的注释。有关详细测试,请参阅测试文件夹。
客户端调用
$client = new Lightbulb\Json\Rpc2\Client('http://api.domain.com/endpoint');
$client->upper("kitten");
$client->firstTwo("Hello");
客户端支持类链以调用嵌套方法
$ok = $client->user->login($user, $pass);
实际上将导致以下JSON调用
{... method: "user.login" ...}
许可证
根据新BSD许可证许可。版权所有2011年Pavel Ptacek。保留所有权利。