{"id":23343244,"url":"https://github.com/vaderangry/phpjsonrpc","last_synced_at":"2025-09-02T23:47:43.806Z","repository":{"id":57076506,"uuid":"72356762","full_name":"vaderangry/PhpJsonRpc","owner":"vaderangry","description":null,"archived":false,"fork":false,"pushed_at":"2018-03-02T13:12:32.000Z","size":89,"stargazers_count":10,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-28T17:04:28.777Z","etag":null,"topics":["api","batch-request","json-rpc2","php","php7"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vaderangry.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-10-30T15:12:42.000Z","updated_at":"2022-07-18T20:04:01.000Z","dependencies_parsed_at":"2022-08-24T14:40:40.712Z","dependency_job_id":null,"html_url":"https://github.com/vaderangry/PhpJsonRpc","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vaderangry%2FPhpJsonRpc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vaderangry%2FPhpJsonRpc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vaderangry%2FPhpJsonRpc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vaderangry%2FPhpJsonRpc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vaderangry","download_url":"https://codeload.github.com/vaderangry/PhpJsonRpc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248143114,"owners_count":21054709,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","batch-request","json-rpc2","php","php7"],"created_at":"2024-12-21T06:13:38.553Z","updated_at":"2025-04-10T02:21:25.958Z","avatar_url":"https://github.com/vaderangry.png","language":"PHP","readme":"# PhpJsonRpc\n\nFlexible [JSON-RPC2](http://www.jsonrpc.org/specification) server/client implementation for PHP7.\n\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/vaderangry/PhpJsonRpc/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/vaderangry/PhpJsonRpc/?branch=master)\n[![Build Status](https://travis-ci.org/vaderangry/PhpJsonRpc.svg?branch=master)](https://travis-ci.org/vaderangry/PhpJsonRpc)\n\n## Features\n\n - JSON-RPC 2.0 full conformance (batch requests, notification, positional and named arguments, etc)​.\n - Quick-start with default routing based on php namespaces.\n - Flexible custom routing for your requirements.\n - The mechanism of intercepting requests and responses through handlers.\n - Automatic casting types in requests and responses.\n - Fully unit tested.\n\n## Installation\n\nOpen a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:\n\n```console\n$ composer require vaderangry/php-json-rpc\n```\n\nThis command requires you to have Composer installed globally, as explained in the [installation chapter](https://getcomposer.org/doc/00-intro.md) of the Composer documentation.\n\n## Documentation\n\n - [Server usage](doc/01-server-usage.md)\n - [Client usage](doc/02-client-usage.md)\n\n## Basic usage\n\n### Server\n\nThe example of quick-start:\n```php\n\u003c?php\n\nuse PhpJsonRpc\\Server;\n\n// Class for which provide JSON-RPC2 API:\nclass Math\n{\n    public function pow(float $base, int $exp): float\n    {\n        return pow($base, $exp);\n    }\n}\n\n$server = new Server();\n\n$response = $server\n    -\u003eaddHandler(new Math())\n    -\u003eexecute();\n\necho $response;\n```\n\nMethod `Math::pow` by default will mapped to method `Math.pow` in JSON-RPC2 terms. Request example:\n```json\n{\n  \"jsonrpc\": \"2.0\", \n  \"method\": \"Math.pow\", \n  \"params\": {\"base\": 2, \"exp\": 3}, \n  \"id\": 1\n}\n```\n\nThe example of custom method mapping:\n\n```php\n\u003c?php\n\nuse PhpJsonRpc\\Server;\nuse PhpJsonRpc\\Server\\MapperInterface;\n\n// Define custom mapper\nclass Mapper implements MapperInterface\n{\n    public function getClassAndMethod(string $requestedMethod): array\n    {\n        // Keys of array presents requested method\n        $map = [\n            'pow' =\u003e [Math::class, 'pow'],\n        ];\n\n        if (array_key_exists($requestedMethod, $map)) {\n            return $map[$requestedMethod];\n        }\n\n        return ['', ''];\n    }\n}\n\n$server = new Server();\n\n// Register new mapper\n$server-\u003esetMapper(new Mapper());\n\n// Register handler and run server\n$response = $server-\u003eaddHandler(new Math())-\u003eexecute();\n\necho $response;\n```\n\nNow `Math::pow` will be mapped to `pow`. Request example:\n```json\n{\n  \"jsonrpc\": \"2.0\", \n  \"method\": \"pow\", \n  \"params\": {\"base\": 2, \"exp\": 3}, \n  \"id\": 1\n}\n```\n\n### Client\n\nSingle request:\n```php\n\u003c?php\n\nuse PhpJsonRpc\\Client;\n\n$client = new Client('http://localhost');\n$result = $client-\u003ecall('Math.pow', [2, 3]); // $result = 8\n$result = $client-\u003ecall('Math.methodNotFound', []); // $result = null\n```\n\nSingle request with exception server error mode:\n```php\n\u003c?php\n\nuse PhpJsonRpc\\Client;\n\n$client = new Client('http://localhost', Client::ERRMODE_EXCEPTION);\n$result = $client-\u003ecall('Math.methodNotFound', []); // throw MethodNotFoundException\n```\n\nBatch request:\n```php\n\u003c?php\n\nuse PhpJsonRpc\\Client;\n\n$client = new Client('http://localhost');\n\n$result = $client-\u003ebatch()\n    -\u003ecall('Util.Math.pow', [2, 1])\n    -\u003ecall('Util.Math.pow', [2, 2])\n    -\u003ecall('Util.Math.pow', [2, 3])\n    -\u003ecall('Util.methodNotFound', [])\n    -\u003ebatchExecute();\n// $result = [2, 4, 8, null]\n```\nAll unit of result stored at the same position of call. Server error present `null` object.\n\nBatch request with exception server error mode:\n```php\n\u003c?php\n\nuse PhpJsonRpc\\Client;\n\n$client = new Client('http://localhost', Client::ERRMODE_EXCEPTION);\n\n$result = $client-\u003ebatch()\n    -\u003ecall('Util.Math.pow', [2, 1])\n    -\u003ecall('Util.methodNotFound', [])\n    -\u003ebatchExecute();\n// $result = [2, MethodNotFoundException]\n```\nWith exception mode server error present `JsonRpcException` object.\nException will not throw for saving other units of result.\n\nThe example with personal custom headers in request:\n```php\n\u003c?php\n\nuse PhpJsonRpc\\Client;\nuse PhpJsonRpc\\Common\\Interceptor\\Container;\nuse PhpJsonRpc\\Common\\Interceptor\\Interceptor;\n\n$client = new Client('http://localhost');\n\n$client-\u003egetTransport()-\u003eonPreRequest()\n   -\u003eadd(Interceptor::createWith(function (Container $container) {\n        // Get transport from container\n        $transport = $container-\u003efirst();\n\n        // Add required headers\n        $transport-\u003eaddHeaders([\n            \"Origin: \" . $_SERVER['HTTP_HOST'],\n        ]);\n\n        // Now we MUST return container for next chain\n        return new Container($transport, $container-\u003elast());\n    }));\n    \n$result = $client-\u003ecall('Math.pow', [2, 3]); // $result = 8\n```\n\n## Tests\n\n```Bash\n$ ./vendor/bin/phpunit -c ./\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvaderangry%2Fphpjsonrpc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvaderangry%2Fphpjsonrpc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvaderangry%2Fphpjsonrpc/lists"}