inferno-code/jsonrpc2-php

简单的 JSON-RPC 2.0 实现。

1.0.7 2020-12-02 07:01 UTC

This package is auto-updated.

Last update: 2024-09-29 06:25:41 UTC


README

简单的 JSON-RPC 2.0 实现

功能

  • 正确:完全符合 JSON-RPC 2.0 规范
  • 没有依赖其他库和包。
  • 简单的 JSON-RPC 2.0 客户端和服务器代码。

待完成任务

  • 编写示例
  • 编写一些单元测试
  • 编写 API 文档

示例

典型客户端

$obj = new \JSONRPC2\RemoteProxyObject(

	new \JSONRPC2\Transports\HTTP(

		// URL of endpoint
		'https://example.com/some/endpoint',

		// options
		(object) [
			'headers' => [
				'Authorization' => 'Bearer ...'
			]
		]
	)
);

printf("result = %s\n", $obj->substract(50, 23));

典型服务器

$server = new \JSONRPC2\ServerObject();

$server->on(
	'substract',
	[ 'minuend', 'subtrahend' ],
	function ($minuend, $subtrahend) {
		return $minuend - $subtrahend;
	}
);

$headers = [
	'Content-Type: application/json; charset=utf-8',
	'Access-Control-Allow-Origin: *',
	'Access-Control-Allow-Methods: POST, GET, DELETE, PUT, PATCH, OPTIONS',
	'Access-Control-Allow-Headers: Authorization, Origin, Content-Type, Accept',
	'Access-Control-Max-Age: 5',
];

foreach ($headers as $header)
	header($header);

$encodedRequest = file_get_contents('php://input');
$encodedResponse = $server->reply($encodedRequest);

echo $encodedResponse;