2017 © Pedro Peláez
 

library jsonrpc

image

wwtg99/jsonrpc

  • Thursday, December 7, 2017
  • by wwtg99
  • Repository
  • 1 Watchers
  • 1 Stars
  • 92 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 14 % Grown

The README.md

JSON-RPC Server and Client

JSON-RPC 2.0 server and client, implementation of JSON-RPC 2.0 for Laravel., (*1)

Features

  • JSON-RPC 2.0 only
  • Support batch requests and notifications
  • Simple to use for Laravel
  • Require PHP >= 5.6 and GuzzleHttp >= 6.0
  • License: MIT

JSON-RPC specification

Installation

composer require wwtg99/jsonrpc

For Lumen or earlier Laravel than v5.5, you need to register the service provider and alias manually,, (*2)

Wwtg99\JsonRpc\Provider\JsonRpcServiceProvider::class

```php 'JsonRpc' => Wwtg99\JsonRpc\Facades\JsonRpc::class, (*3)


# Usage ## Server Side ### Bind Methods Bind callback: ```php JsonRpc::bind('method1', function($request) { $method = $request->getMethod(); $params = $request->getParams(); $p = $request->parseParam('name'); //get param name $id = $request->getId(); //some process... //return result array, request id will be added automatically return [1, 2, 3]; //Or use JsonRpcResponse return new JsonRpcResponse($id, [1, 2, 3]); //return error return new JsonRpcResponse($id, null, ['code'=>1, 'message'=>'error']); }); // Or use handler instance $ph = resolve('ProcessHandler'); $ph->bind('method1', function($request) { return [1, 2, 3]; });

Bind class method, (*4)

namespace Test;
class BindingTest {
    public function test1($request) 
    {
        return [1, 2, 3];
    }
}
JsonRpc::bind('method2', 'Test\BindingTest@test1');
// Or $ph->bind('method2', 'Test\BindingTest@test1');

Handle requests

Add route, (*5)

//you should disable VerifyCsrfToken middleware if use post method
Route::match(['GET', 'POST'], '/json_rpc', function() {
    $res = JsonRpc::parse(request());
    //other process
    return response()->json($res);
});

Or simply use, (*6)

Route::match(['GET', 'POST'], '/json_rpc', function (\Illuminate\Http\Request $request) {
    return Wwtg99\JsonRpc\Provider\JsonRpcRouter::parse($request);
});

Client Side

Send request in client

The first parameter is json rpc server url, and second parameter is config options., (*7)

Options

  • http_method: http method to send request, get or post, default post
  • return_type: return type for response, json or string, default json

Other options will be sent to Guzzle client., (*8)

//get client
$cli = new JsonRpcClient($url);  //default method is post, return type json
//use get method
//$cli = new JsonRpcClient($url, ['http_method'=>'get']);
//use raw string return instead of json
//$cli = new JsonRpcClient($url, ['return_type'=>'string']);

//build requests
$req1 = new JsonRpcRequest('method1', 1, [1, 2, 3]);
$req1 = new JsonRpcRequest('method1', 2, [1, 2, 3]);

//send one request
$res = $cli->send($req1);
//send batch requests
$res = $cli->appendRequest($req1)->appendRequest($req2)->send();
//send notify
$cli->notify('method2', ['a'=>'b'])

The Versions

07/12 2017

dev-master

9999999-dev

  Sources   Download

The Requires

 

The Development Requires

by wuwentao

laravel jsonrpc

07/12 2017

0.2.2

0.2.2.0

  Sources   Download

The Requires

 

The Development Requires

by wuwentao

laravel jsonrpc

07/12 2017

0.2.1

0.2.1.0

  Sources   Download

The Requires

 

The Development Requires

by wuwentao

laravel jsonrpc

07/12 2017

0.2.0

0.2.0.0

  Sources   Download

The Requires

 

The Development Requires

by wuwentao

laravel jsonrpc

14/04 2017

0.1.1

0.1.1.0

  Sources   Download

The Requires

 

The Development Requires

by wuwentao

laravel jsonrpc

14/04 2017

0.1.0

0.1.0.0

  Sources   Download

The Requires

 

The Development Requires

by wuwentao

laravel jsonrpc