gruposinternet/json-rpc-php

PHP 的 JSON-RPC 2.0 客户端/服务器库

dev-master 2022-01-17 19:50 UTC

This package is not auto-updated.

Last update: 2024-09-25 22:08:32 UTC


README

JSON-RPC 2.0 客户端/服务器库,要求 PHP 5.2.6+ 以上版本

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

### 如何使用客户端?在实例化 JsonRpcClient 对象并指定服务器 URL 后,有两种发送请求的方式。这两种方法都使用 RpcRequest 类(位于 lib/client/ 目录下)来发送格式良好的请求。将很快提供详细的教程。## 单个请求 ### 发送请求 因此,在 $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 而不是 result。错误对象有三个成员,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();

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

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

###接受响应 单个请求与批量请求的唯一区别是 callBatch 将返回一个响应对象数组,所以在这种情况下 $responseArray 看起来是这样的:

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;
    }
}