yocto / yoclib-jsonrpc
此yocLibrary允许您的项目在PHP中编码和解码JSON-RPC消息。
v1.0.1
2024-05-21 11:19 UTC
Requires
- php: ^7.4||^8
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ^7||^8||^9
This package is auto-updated.
Last update: 2024-09-21 12:08:26 UTC
README
此yocLibrary允许您的项目在PHP中编码和解码JSON-RPC消息。
状态
安装
composer require yocto/yoclib-jsonrpc
使用
序列化
use YOCLIB\JSONRPC\JSONRPCException; use YOCLIB\JSONRPC\Message; // Create request $message = Message::createRequest(123,'getInfo',['payments']); // Create notification $message = Message::createNotification('notificationEvent',['payed']); // Create response $message = Message::createResponse(123,['payments'=>['$10.12','$23.45','$12.34']]); $object = $message->toObject(); try{ $json = Message::encodeJSON($object); }catch(JSONRPCException $e){ //Handle encoding exception }
反序列化
use YOCLIB\JSONRPC\JSONRPCException; use YOCLIB\JSONRPC\Message; $json = file_get_contents('php://input'); // Get request body try{ $object = Message::decodeJSON($json); }catch(JSONRPCException $e){ //Handle decoding exception } if(Message::isBatch($object)){ foreach($object AS $element){ try{ $message = Message::parse($element); }catch(JSONRPCException $e){ //Handle message exception } } }else{ try{ $message = Message::parse($object); }catch(JSONRPCException $e){ //Handle message exception } }