scaytrase/json-rpc-client
Lightweight PHP JSON-RPC client for calling remote procedures over HTTP. Provides request/response handling, batching, and error parsing, making it easier to integrate JSON-RPC services into your Laravel or vanilla PHP apps with minimal setup.
Installation
composer require scaytrase/json-rpc-client
Add to composer.json if not auto-loaded:
"autoload": {
"psr-4": {
"App\\": "app/"
}
}
Run composer dump-autoload.
First Request
use Scaytrase\JsonRpc\Client;
$client = new Client('http://example.com/rpc');
$response = $client->call('methodName', [1, 2, 3]);
Basic Response Handling
if ($response->isSuccess()) {
$result = $response->getResult();
} else {
$error = $response->getError();
}
$client = new Client('https://api.example.com/rpc', [
'headers' => ['Authorization' => 'Bearer token123']
]);
$balance = $client->call('getBalance', ['userId' => 123]);
Batch Requests
$batch = $client->batch();
$batch->call('method1', []);
$batch->call('method2', ['param']);
$responses = $client->sendBatch($batch);
Error Handling with Retries
$client = new Client('https://api.example.com/rpc');
$client->setRetryPolicy(3); // Retry 3 times on failure
try {
$response = $client->call('unreliableMethod', []);
} catch (JsonRpcException $e) {
// Log or handle specific errors
}
Middleware for Logging/Transforming
$client->addMiddleware(function ($request) {
// Log request details
return $request;
});
Laravel Service Provider
$this->app->singleton('jsonrpc.client', function ($app) {
return new Client(config('services.jsonrpc.url'));
});
Dependency Injection
public function __construct(Client $client) {
$this->client = $client;
}
Configuration via .env
JSONRPC_URL=https://api.example.com/rpc
JSONRPC_TIMEOUT=30
Deprecated Package
rectorphp/json-rpc-client).No Built-in Async Support
GuzzleHttp or ReactPHP wrappers for async requests.Error Handling Quirks
JsonRpcException may not cover all HTTP errors (e.g., 500s). Use try-catch with GuzzleException if using HTTP transport.Enable Debug Mode
$client->setDebug(true); // Logs requests/responses
Inspect Raw Responses
$rawResponse = $client->getLastRawResponse();
Custom Transport Layer
Override Scaytrase\JsonRpc\Transport\TransportInterface for custom HTTP clients (e.g., Guzzle, Symfony HTTP).
Middleware for Authentication
$client->addMiddleware(function ($request) {
$request->setHeader('X-API-Key', config('services.jsonrpc.key'));
return $request;
});
Response Transformers
$client->addResponseTransformer(function ($response) {
return json_decode($response, true);
});
Timeout Defaults Set explicitly to avoid silent hangs:
$client = new Client('https://api.example.com/rpc', [
'timeout' => 10 // seconds
]);
SSL Verification Disable only for testing:
$client->setVerifyPeer(false); // Not recommended for production
How can I help you explore Laravel packages today?