nathan-muir/json-rpc-2

此包已废弃且不再维护。未建议替代包。

JSON-RPC 2.0 客户端与服务器实现

0.9.3 2013-04-11 00:56 UTC

This package is not auto-updated.

Last update: 2023-08-05 09:01:07 UTC


README

此库是针对 PHP 5.3+ 编写的 JSON-RPC 2.0 规范的高灵活实现,适用于 PSR 标准。

它被设计为允许在不编辑核心文件的情况下更改许多机制。可以在不修改库的情况下运行不同的传输、身份验证方案、方法授权系统、参数类型验证与数据清理方案、代理系统和方法列表缓存/解析系统。

诚然,目前缺少必要的单元测试,这将允许您检查某些组件的行为。

如果您编写了任何可以包含的模块或希望讨论此库,请随时告诉我!

如何使用客户端 - 基本用法

<?php
namespace MyCompany\Package;

use \Ndm\JsonRpc2\Client\HttpClient;
// use vendor autoload from composer
require('vendor/autoload.php');

// create a Client using the HttpTransport layer
$client = HttpClient::connect('http://api.somesite.com/');
// call a method, using named parameters
$client->call('somemethod', array('abc'=>123));

// alternatively, use the "native" interface
$nativeClient = $client->getNativeClient();
// however calls must use positional parameters
$nativeClient->somemethod(123);

如何使用服务器 - 基本用法

<?php


namespace MyCompany\Package;

use Ndm\JsonRpc2\HttpServer;
use \Ndm\JsonRpc2\Server\Exception\TransportReceiveException;
use \Ndm\JsonRpc2\Server\Exception\TransportReplyException;

require ('vendor/autoload.php'); // require autoloader created by composer

$api = new SomeClass();
$methods =  array (
    'static_func'=> 'AnotherStatic::Func',
    'global_func' => 'do_abc',
    'some_func' => function($p) { return $p + 1; }
);
// register the server with a set of methods, either from an instance, a class with static methods, or as a map of 'callables'
$server = HttpServer::register( $api, 'StaticClass',$methods);

// process the request!
try {
    $server->process();
} catch (TransportReceiveException $treceive){
    // exceptions on this layer - like not using HTTP-POST
    header('HTTP/1.0 400 Bad Request');
    exit;
} catch (TransportReplyException $treply){
    header('HTTP/1.0 500 Internal Server Error');
    exit;
}

如何使用服务器 - 高级用法

<?php


namespace MyCompany\Package;

use \Ndm\JsonRpc2\Server\Server;
use \Ndm\JsonRpc2\Server\Exception\TransportReceiveException;
use \Ndm\JsonRpc2\Server\Exception\TransportReplyException;
use \Ndm\JsonRpc2\Server\Transport\HttpTransport;
use \Ndm\JsonRpc2\Server\Dispatch as Dispatch;

require ('vendor/autoload.php'); // require autoloader created by composer

// init procedure -
// 1. perform any external checks / tests on your transport layer (ie Authentication via OAuth)
// 2. initialise a transport system to obtain the rpc calls from, and return results to
// 3. get some functions to provide
// 4. register them with a dispatch system
// 5. create a server with the aforementioned dispatch & transport systems

// the transport - a simple http wrapper
$transport = new HttpTransport(HttpTransport::OPT_REQUIRE_HTTPS | HttpTransport::OPT_SEND_OUTPUT_HEADERS);


$api = new SomeClass();

//create a set of methods from the instance of SomeClass
$methods = Dispatch\ReflectionMethod::createFrom($api);
// dispatch system is responsible for invoking methods called by clients
$dispatch = new Dispatch\MapDispatch();
// register all the methods with the dispatch system
$dispatch->registerAll($methods);

// start the server
$server = new Server($transport, $dispatch);
// process the request!
try {
    $server->process();
} catch (TransportReceiveException $treceive){
    header('HTTP/1.0 400 Bad Request');
    exit;
} catch (TransportReplyException $treply){
     header('HTTP/1.0 500 Internal Server Error');
     exit;
}

待办事项

文档

  • 客户端文档

测试

  • 单元测试

    • 核心:ResponseParser
    • 服务器:Server [模拟 'receive' 函数,或包装 TransportInterface 与 RequestParser 的组合]
    • 客户端:Client [模拟 'send' 函数,或包装 TransportInterface 与 ResponseParser 的组合]
    • 客户端:BatchClient
    • 服务器:Transport\HttpTransport
    • 未实现的类/传输

实现/功能

  • 使用 HttpTransport 的 OAuth & Basic Auth 客户端包装器
  • 更全面的分发系统实现(缓存、Docblock 解析、类型检查)

服务器结构/工作流程

传输生命周期

  1. 读取传输层/源 - 仅提供字符串/文本
  2. 接收文本回复以渲染

服务器生命周期

  1. 通过 'receive' 从传输获取请求数据。
  2. 使用 \JsonRpc\RequestParser->parse() 将其解析为对象
  3. 遍历每个请求,通过 Dispatch->invoke 获取结果。捕获异常并将其转换为 ResponseError。
  4. 将结果传递回传输以进行渲染。

分发生命周期

  1. 传递一个方法别名和参数,并必须返回一个结果或抛出异常。