acgrid/json-rpc-php

Pozo/json-rpc-php 库的 PHP 5/7 版本移植

v1.0.2 2020-08-27 15:05 UTC

This package is auto-updated.

Last update: 2024-08-27 23:42:22 UTC


README

支持 Composer PSR-4 的 PHP JSON-RPC 2.0 客户端/服务器库

授权协议

原作者 pozo 没有声明使用的授权协议。如果您不允许分支和移植,请通知我。

###需求 PHP 5.5.0+,支持 composer PSR-4。

使用 composer require acgrid/json-rpc-php 安装,或将 "acgrid/json-rpc-php": "@dev" 添加到您的 composer.json 文件中。

###使用的预定义类和接口

注意

以下 README 由原作者编写。请注意,已引入命名空间。

##如何使用客户端?在用服务器 URL 实例化 JsonRpcClient 之后,有两种发送请求的方式。两种方法都使用 lib/client/ 下的 RpcRequest 类,这有助于发送格式良好的请求。逐步教程即将推出。 ##单个请求 ###发送请求 在 $client = new JsonRpcClient('http://serverurl'); 后触发单个请求,您可以使用以下方法之一

####__call() 魔术方法(推荐)参数顺序必须与服务器实现相同,不允许通知

$response = $client->add($a,$b);

等同于

$response = $client->{'add'}($a,$b);

####RpcRequest

  1. 排序参数

正确

$response = $client->call(new RpcRequest('sanitize',array(array(1,2,3))));
$response = $client->call(new RpcRequest('deleteById',array(3)));

错误

$response = $client->call(new RpcRequest('sanitize',array(1,2,3)));
$response = $client->call(new RpcRequest('deleteById',3));
  1. 命名参数顺序不必要,如果存在,服务器将排序参数 区分大小写

    $response = $client->call(new RpcRequest('add',array('bValue'=>$b,'aValue'=>$a)));

  2. 通知

    $response = $client->call(new RpcRequest('deleteAndUpdtae',array(2),true)); $response = $client->call(new RpcRequest('update',null,true));

###处理响应 请注意,客户端的实现相对较少,因此您可以自由地管理响应,例如,将响应对象包装在 JsonRpcClient 中,当您收到意外答案时抛出异常等。

如果请求不是通知,并且处理成功,则 $response 将是一个包含 idjsonrpcresult 字段的对象,否则结果对象将包含 error 而不是 resulterror 对象有三个成员,messagecode 和可选的 data。因此,在 $response = $client->add($a,$b); 的情况下,如果 $a 为 10,$b 为 20,则 var_dump($response) 将如下所示:

object(stdClass)#19 (3) {
  ["jsonrpc"]=>
  string(3) "2.0"
  ["result"]=>
  int(30)
  ["id"]=>
  int(1)
}

如果我们错误地调用 addd 而不是 add

object(stdClass)#19 (3) {
  ["jsonrpc"]=>
  string(3) "2.0"
  ["error"]=>
  object(stdClass)#20 (2) {
    ["code"]=>
    int(-32601)
    ["message"]=>
    string(16) "Method not found"
  }
  ["id"]=>
  int(1)
}

##批量请求 ###发送请求 您可以同时发送多个请求。在这种情况下,您必须使用数组实用程序来收集 RpcRequest 对象,并将它们传递给 callBatch 方法

  1. $client = new JsonRpcClient("https:///jsonrpc/sample/server/");

    $listOfCalls = array();

    array_push($listOfCalls,new RpcRequest("add",array(33,77))); array_push($listOfCalls,new RpcRequest("divide",array(44,11),true)); array_push($listOfCalls,new RpcRequest("subtract",array(2,12.3))); array_push($listOfCalls,new RpcRequest("invalidateSession"));

    $responseArray = $client->callBatch($listOfCalls);

  2. 其中一个是一个通知

    $client = new JsonRpcClient("https:///jsonrpc/sample/server/"); $listOfCalls = array();

    array_push($listOfCalls,new RpcRequest("add",array(33,77))); array_push($listOfCalls,new RpcRequest("add",array(44,11),true)); array_push($listOfCalls,new RpcRequest("add",array(2,12.3)));

    $responseArray = $client->callBatch($listOfCalls);

  3. 所有请求都是通知

    $client = new JsonRpcClient("https:///jsonrpc/sample/server/"); $listOfCalls = array();

    将 RpcRequest("add",array(33,77),true) 添加到 $listOfCalls 数组中;将 RpcRequest("add",array(44,11),true) 添加到 $listOfCalls 数组中;将 RpcRequest("add",array(2,12.3),true) 添加到 $listOfCalls 数组中;

    $responseArray = $client->callBatch($listOfCalls);

### 接收响应 与单个请求相比,调用批处理将返回一个响应对象数组,所以 $responseArray 在情况 1) 下看起来如下:

array(3) {
  [0]=>
  object(stdClass)#8 (3) {
    ["jsonrpc"]=>
    string(3) "2.0"
    ["result"]=>
    int(110)
    ["id"]=>
    int(2)
  }
  [1]=>
  object(stdClass)#12 (3) {
    ["jsonrpc"]=>
    string(3) "2.0"
    ["result"]=>
    int(55)
    ["id"]=>
    int(3)
  }
  [2]=>
  object(stdClass)#13 (3) {
    ["jsonrpc"]=>
    string(3) "2.0"
    ["result"]=>
    float(14.3)
    ["id"]=>
    int(4)
  }
}

情况 2) 的情况

array(2) {
  [0]=>
  object(stdClass)#8 (3) {
    ["jsonrpc"]=>
    string(3) "2.0"
    ["result"]=>
    int(110)
    ["id"]=>
    int(2)
  }
  [1]=>
  object(stdClass)#12 (3) {
    ["jsonrpc"]=>
    string(3) "2.0"
    ["result"]=>
    float(14.3)
    ["id"]=>
    int(3)
  }
}

最后,情况 3) 下,$responseArray 将不包含任何内容,因此为 NULL

## 如何使用服务器?您可以在 sample 目录中找到完整的示例应用程序源代码。 逐步教程即将推出。 ## 实现服务 首先,您的服务实现必须继承自 Service 这对于所有服务都是必要的,因为 JsonRpcServer 将使用 Service 方法与 PHP 反射 来检测可调用方法及其参数。客户端只能访问具有以下内容的精确块注释的方法:

/* @JsonRpcMethod*/

/* 后的第一个空格非常重要,因为反射无法解析没有它的注释。但是,下面的示例仍然正确,因为 Service 使用 strstr 检测 @JsonRpcMethod 注解。

/**
* @JsonRpcMethod which is ...
*/

例如,如果您在 MathServiceImpl 中有一些公共方法,这些方法没有带有 @JsonRpcMethod 注解的块注释,则客户端无法访问它。

MathServiceImpl.php

class MathServiceImpl extends JsonRpcService {
/** @JsonRpcMethod*/
public function add($aValue,$bValue) {
	return $aValue+$bValue;
}
/** @JsonRpcMethod*/
public function divide($aValue,$bValue) {
	return $aValue/$bValue;
}
/** @JsonRpcMethod*/
public function subtract($aValue,$bValue) {
	return $aValue-$bValue;
}
public function notCallableByRpcAlthoughItsPublic($name) {
	return $name;
    }
}