Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Json Rpc Client Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require scaytrase/json-rpc-client
    

    Add to composer.json if not auto-loaded:

    "autoload": {
        "psr-4": {
            "App\\": "app/"
        }
    }
    

    Run composer dump-autoload.

  2. First Request

    use Scaytrase\JsonRpc\Client;
    
    $client = new Client('http://example.com/rpc');
    $response = $client->call('methodName', [1, 2, 3]);
    
  3. Basic Response Handling

    if ($response->isSuccess()) {
        $result = $response->getResult();
    } else {
        $error = $response->getError();
    }
    

First Use Case: Querying a Remote API

$client = new Client('https://api.example.com/rpc', [
    'headers' => ['Authorization' => 'Bearer token123']
]);

$balance = $client->call('getBalance', ['userId' => 123]);

Implementation Patterns

Common Workflows

  1. Batch Requests

    $batch = $client->batch();
    $batch->call('method1', []);
    $batch->call('method2', ['param']);
    $responses = $client->sendBatch($batch);
    
  2. 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
    }
    
  3. Middleware for Logging/Transforming

    $client->addMiddleware(function ($request) {
        // Log request details
        return $request;
    });
    

Integration Tips

  • 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
    

Gotchas and Tips

Pitfalls

  1. Deprecated Package

    • Last release in 2017; ensure compatibility with your PHP/Laravel version.
    • Check for forks or alternatives (e.g., rectorphp/json-rpc-client).
  2. No Built-in Async Support

    • Use GuzzleHttp or ReactPHP wrappers for async requests.
  3. Error Handling Quirks

    • JsonRpcException may not cover all HTTP errors (e.g., 500s). Use try-catch with GuzzleException if using HTTP transport.

Debugging Tips

  • Enable Debug Mode

    $client->setDebug(true); // Logs requests/responses
    
  • Inspect Raw Responses

    $rawResponse = $client->getLastRawResponse();
    

Extension Points

  1. Custom Transport Layer Override Scaytrase\JsonRpc\Transport\TransportInterface for custom HTTP clients (e.g., Guzzle, Symfony HTTP).

  2. Middleware for Authentication

    $client->addMiddleware(function ($request) {
        $request->setHeader('X-API-Key', config('services.jsonrpc.key'));
        return $request;
    });
    
  3. Response Transformers

    $client->addResponseTransformer(function ($response) {
        return json_decode($response, true);
    });
    

Configuration Quirks

  • 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
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity