MyGit

graze/guzzle-jsonrpc

Fork: 61 Star: 93 (更新于 2024-11-04 20:12:07)

license: MIT

Language: PHP .

JSON-RPC 2.0 client for Guzzle

最后发布版本: 2.1.1 ( 2018-01-02 19:13:48)

官方网址 GitHub网址

[ABANDONED] Guzzle JSON-RPC

No Maintenance Intended

This is no longer supported, please consider forking this repository to make any desired changes.

Master branch build status Coverage Status Quality Score Published version PHP ~5.5 MIT Licensed

This library implements JSON-RPC 2.0 for the Guzzle HTTP client. We try to support all commonly used versions of Guzzle including:

It can be installed in whichever way you prefer, but we recommend Composer.

{
    "require": {
        "graze/guzzle-jsonrpc": "~3.0"
    }
}

Documentation

<?php
use Graze\GuzzleHttp\JsonRpc\Client;

// Create the client
$client = Client::factory('http://localhost:8000');

// Send a notification
$client->send($client->notification('method', ['key'=>'value']));

// Send a request that expects a response
$client->send($client->request(123, 'method', ['key'=>'value']));

// Send a batch of requests
$client->sendAll([
    $client->request(123, 'method', ['key'=>'value']),
    $client->request(456, 'method', ['key'=>'value']),
    $client->notification('method', ['key'=>'value'])
]);

Async requests

Asynchronous requests are supported by making use of the Guzzle Promises library; an implementation of Promises/A+.

<?php
use Graze\GuzzleHttp\JsonRpc\Client;

// Create the client
$client = Client::factory('http://localhost:8000');

// Send an async notification
$promise = $client->sendAsync($client->notification('method', ['key'=>'value']));
$promise->then(function () {
    // Do something
});

// Send an async request that expects a response
$promise = $client->sendAsync($client->request(123, 'method', ['key'=>'value']));
$promise->then(function ($response) {
    // Do something with the response
});

// Send a batch of requests
$client->sendAllAsync([
    $client->request(123, 'method', ['key'=>'value']),
    $client->request(456, 'method', ['key'=>'value']),
    $client->notification('method', ['key'=>'value'])
])->then(function ($responses) {
    // Do something with the list of responses
});

Throw exception on RPC error

You can throw an exception if you receive an RPC error response by adding the option [rpc_error => true] in the client constructor.

<?php
use Graze\GuzzleHttp\JsonRpc\Client;
use Graze\GuzzleHttp\JsonRpc\Exception\RequestException;

// Create the client with the `rpc_error`
$client = Client::factory('http://localhost:8000', ['rpc_error'=>true]);

// Create a request
$request = $client->request(123, 'method', ['key'=>'value']);

// Send the request
try {
    $client->send($request);
} catch (RequestException $e) {
    die($e->getResponse()->getRpcErrorMessage());
}

Contributing

We accept contributions to the source via Pull Request, but passing unit tests must be included before it will be considered for merge.

~ $ make deps
~ $ make lint test

License

The content of this library is released under the MIT License by Nature Delivered Ltd.

You can find a copy of this license at mit or in LICENSE

最近版本更新:(数据更新于 2024-09-18 21:04:22)

2018-01-02 19:13:48 2.1.1

2016-02-16 23:19:57 3.2.1

主题(topics):

guzzle, php

graze/guzzle-jsonrpc同语言 PHP最近更新仓库

2024-11-13 23:19:14 symfony/symfony

2024-11-13 22:53:21 symfony/var-dumper

2024-11-11 23:40:24 coollabsio/coolify

2024-11-06 20:45:55 schmittjoh/JMSSerializerBundle

2024-10-31 21:37:53 filamentphp/filament

2024-10-11 03:27:29 odan/slim4-skeleton