scaytrase/rpc-common
Common PHP RPC interfaces and helpers with batch-style request support. Includes client decorators (lazy, logging, caching) plus test utilities like a mock client with queued responses and acceptance filters for predictable RPC testing.
Installation
composer require scaytrase/rpc-common
config/app.php under providers.First Use Case: Basic RPC Client
use Scaytrase\RpcCommon\Client\RpcClient;
use Scaytrase\RpcCommon\Message\RpcRequest;
use Scaytrase\RpcCommon\Message\RpcResponse;
$client = new RpcClient('http://example.com/rpc');
$request = new RpcRequest('methodName', ['param1', 'param2']);
$response = $client->send($request);
if ($response->isSuccess()) {
echo $response->getResult();
} else {
echo $response->getError();
}
Key Files to Explore
src/Client/RpcClient.php – Core client logic.src/Message/RpcRequest.php & src/Message/RpcResponse.php – Request/response structures.src/Exception/RpcException.php – Error handling.RpcRequest to define method names and parameters.
$request = new RpcRequest('user.get', ['id' => 1]);
isSuccess() before accessing data.
if ($response->isSuccess()) {
$data = $response->getResult();
}
AppServiceProvider.
$this->app->bind(
\Scaytrase\RpcCommon\Client\RpcClientInterface::class,
\Scaytrase\RpcCommon\Client\RpcClient::class
);
RpcClient to add auth headers or logging.
$client = new RpcClient('http://api.example.com', [
'headers' => ['Authorization' => 'Bearer ' . auth()->token()]
]);
$client->send(new RpcRequest('method1', []))
->then(function ($response) {
return $client->send(new RpcRequest('method2', []));
});
RpcException for malformed responses.
try {
$response = $client->send($request);
} catch (RpcException $e) {
Log::error($e->getMessage());
}
RpcClientInterface in unit tests.
$mock = Mockery::mock(RpcClientInterface::class);
$mock->shouldReceive('send')
->once()
->andReturn(new RpcResponse(true, ['data' => 'test']));
Deprecated Package
No Built-in Retries
$attempts = 0;
while ($attempts < 3) {
try {
$response = $client->send($request);
break;
} catch (Exception $e) {
$attempts++;
sleep(1);
}
}
No JSON Schema Validation
$client = new RpcClient('http://example.com', [
'debug' => true,
'handler' => HandlerStack::create(new \GuzzleHttp\Handler\CurlHandler())
]);
$rawResponse = $client->getLastRawResponse();
Custom Serializers
RpcRequest/RpcResponse to support custom data formats (e.g., XML).class CustomRpcRequest extends RpcRequest {
public function serialize(): string {
return json_encode(['custom' => $this->getParams()]);
}
}
Middleware Support
$client = new RpcClient('http://example.com');
$client->addMiddleware(function ($request) {
$request->addHeader('X-Custom', 'value');
});
Event Dispatching
event(new RpcRequestEvent($request));
$response = $client->send($request);
event(new RpcResponseEvent($response));
How can I help you explore Laravel packages today?