graze/guzzle-jsonrpc
Abandoned JSON-RPC 2.0 client for Guzzle. Supports Guzzle 6/5/4/3 via branches, with helpers to build notifications, requests, and batch calls. Provides sync and async sending using Guzzle Promises. Consider forking for maintenance.
Installation:
composer require graze/guzzle-jsonrpc:~3.0
Ensure your project uses GuzzleHTTP 6 (or compatible versions as per the package's branches).
Basic Client Initialization:
use Graze\GuzzleHttp\JsonRpc\Client;
$client = Client::factory('http://your-jsonrpc-endpoint');
First Use Case: Send a simple request:
$response = $client->send(
$client->request(null, 'methodName', ['param1', 'param2'])
);
Synchronous Requests:
// Single request
$response = $client->send(
$client->request(1, 'getUser', ['userId' => 123])
);
// Batch requests
$responses = $client->sendAll([
$client->request(1, 'method1', []),
$client->request(2, 'method2', []),
]);
Asynchronous Requests (using Guzzle Promises):
$promise = $client->sendAsync(
$client->request(1, 'asyncMethod', [])
);
$promise->then(function ($response) {
// Handle response
});
Notifications (fire-and-forget):
$client->send(
$client->notification('logEvent', ['event' => 'user_login'])
);
Middleware/Subscribers: Register Guzzle middleware (e.g., for logging or error handling):
$client = new Client(
'http://endpoint',
['subscribers' => [new \GuzzleHttp\Subscriber\RetrySubscriber()]]
);
Error Handling: Enable RPC error exceptions:
$client = Client::factory('http://endpoint', ['rpc_error' => true]);
try {
$client->send($client->request(1, 'method', []));
} catch (\Graze\GuzzleHttp\JsonRpc\Exception\RequestException $e) {
// Handle RPC errors
}
Custom Headers/Config: Pass Guzzle options via the client constructor:
$client = Client::factory('http://endpoint', [
'headers' => ['Authorization' => 'Bearer token'],
'timeout' => 10,
]);
Deprecation Warning: The package is abandoned (last release: 2018). Use at your own risk or fork it.
PHP 7+ JSON Parsing:
Empty string responses may cause issues (fixed in 3.2.1). Ensure your server returns valid JSON.
Async Quirks:
.catch() for robust error handling:
$promise->catch(function ($exception) {
log::error($exception->getMessage());
});
Batch Requests:
sendAll() matches the input array. Validate responses by ID:
foreach ($responses as $response) {
if ($response->getId() === 1) { /* Handle */ }
}
Enable Guzzle Debugging:
$client = Client::factory('http://endpoint', [
'debug' => true,
]);
Check $client->getHandlerStack() for middleware.
Inspect Raw Responses:
$response = $client->send($request);
$rawBody = $response->getBody()->getContents();
Custom Response Handling:
Extend the Client class to modify response processing:
class CustomClient extends Client {
protected function processResponse($response) {
// Custom logic
return parent::processResponse($response);
}
}
Middleware for JSON-RPC: Add pre/post-processing middleware:
$stack = \GuzzleHttp\HandlerStack::create();
$stack->push(\GuzzleHttp\Middleware::tap(function ($request) {
// Modify request
}));
$client = new Client('http://endpoint', ['handler' => $stack]);
Batch Optimization: For high-volume batches, consider:
sendAllAsync() with then() for parallel processing.How can I help you explore Laravel packages today?