tourze/json-rpc-http-direct-call-bundle

JsonRPC另外一种调用方式

Installs: 8

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:symfony-bundle

pkg:composer/tourze/json-rpc-http-direct-call-bundle

0.1.4 2025-06-03 18:38 UTC

This package is auto-updated.

Last update: 2025-11-01 19:18:26 UTC


README

English | 中文

Latest Version Total Downloads PHP Version Require License codecov

A Symfony bundle that provides HTTP direct call functionality for JSON-RPC services. This bundle offers an alternative approach to JSON-RPC invocation through HTTP endpoints.

Features

  • HTTP direct calls to JSON-RPC services
  • Request/response encryption and decryption support
  • Multiple calling interface forms
  • Automatic route loading with attributes
  • Built-in error handling and logging
  • Support for prefixed and non-prefixed endpoints

Requirements

  • PHP 8.2 or higher
  • Symfony 7.3 or higher
  • ext-json

Installation

composer require tourze/json-rpc-http-direct-call-bundle

Configuration

Add the bundle to your config/bundles.php file:

return [
    // ...
    Tourze\JsonRPCHttpDirectCallBundle\JsonRPCHttpDirectCallBundle::class => ['all' => true],
];

Quick Start

Direct Call Endpoint

Send POST requests to JSON-RPC methods using these endpoints:

# With prefix
POST /json-rpc/{prefix}/{method}.aspx

# Without prefix (CP endpoint)
POST /cp/json-rpc/{method}.aspx

Example request:

curl -X POST http://localhost/json-rpc/user/getInfo.aspx \
  -H "Content-Type: application/json" \
  -H "request-id: unique-request-id" \
  -d '{"userId": 123}'

Direct POST Endpoint

Send POST requests directly to methods:

POST /json-rpc/call/{method}

Example request:

curl -X POST http://localhost/json-rpc/call/getUser \
  -d "userId=123&includeProfile=true"

Usage

Controllers

The bundle provides two main controllers:

  1. DirectCallController - Handles requests to /json-rpc/{prefix}/{method}.aspx and /cp/json-rpc/{method}.aspx
  2. DirectPostController - Handles requests to /json-rpc/call/{method}

Encryption Support

The bundle supports request/response encryption when the encryptor service detects encrypted endpoints:

// Automatic encryption detection based on request path
if ($this->encryptor->shouldEncrypt($request)) {
    $decryptedContent = $this->encryptor->decryptByRequest($request, $content);
    // Process decrypted content...
}

Request ID Handling

The bundle automatically handles request IDs:

  • Uses request-id header if provided
  • Generates UUID v4 if not provided
  • Prefixes with endpoint prefix for tracking

Architecture

src/
├── Controller/
│   ├── DirectCallController.php    # Main JSON-RPC endpoint controller
│   └── DirectPostController.php    # Direct POST endpoint controller
├── DependencyInjection/
│   └── JsonRPCHttpDirectCallExtension.php  # Service configuration
├── Exception/
│   └── UnexpectedControllerException.php   # Custom exceptions
├── Service/
│   └── AttributeControllerLoader.php       # Route auto-loader
└── JsonRPCHttpDirectCallBundle.php         # Bundle class

Testing

Run tests from the project root directory:

./vendor/bin/phpunit packages/json-rpc-http-direct-call-bundle/tests

Test coverage includes:

  • Unit tests for all components
  • Integration tests for complete workflows
  • Mock services for dependencies

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

The MIT License (MIT). Please see License File for more information.