jdolieslager / jsonrpc
JSON RPC 通信的服务器和/或客户端库
0.3.3
2013-11-21 15:12 UTC
Requires
- php: >= 5.3.3
This package is not auto-updated.
Last update: 2024-09-24 00:21:57 UTC
README
JSON RPC 服务器/客户端库
示例用法(服务器地址: http://josnrpc.mine)
class Test { public function hello($name) { return "Hello {$name}!"; } } $server = new Jdolieslager\JsonRpc\Server(true); $server->registerHandler('Test'); // Second argument you can define a namespace // Methods are than seperated with <namespace>.<orignal_method_name> $server->printResponseForRawRequest(file_get_contents('php://input'));
客户端单次请求(基于参数)
$client = new Jdolieslager\JsonRpc\Client('http://jsonrpc.mine'); $response = $client->sendSimpleRequest('hello', array('name' => 'World')); if ($response->hasError()) { $error = $response->getError(); throw new \Exception($error->getMessage(), $error->getCode()); } else { echo $response->getResult(); }
客户端单次请求(基于对象)
$request = new Jdolieslager\JsonRpc\Entity\Request(); $request->setId(1); $request->setJsonrpc(Jdolieslager\JsonRpc\Client::VERSION_1); $request->setMethod('hello'); $request->addParam('World', 'name'); $client = new Jdolieslager\JsonRpc\Client('http://jsonrpc.mine'); $response = $client->sendSingleRequest($request); if ($response->hasError()) { $error = $response->getError(); throw new \Exception($error->getMessage(), $error->getCode()); } else { echo $response->getResult(); }
客户端单次请求通知(基于参数)
$client = new Jdolieslager\JsonRpc\Client('http://jsonrpc.mine'); $client->sendNotification('hello', array('name' => 'World'));
客户端多次请求
use Jdolieslager\JsonRpc\Entity\Response; // Create objects $client = new Jdolieslager\JsonRpc\Client('http://jsonrpc.mine'); $collection = new Jdolieslager\JsonRpc\Collection\Request(); $request = new Jdolieslager\JsonRpc\Entity\Request(); // Create request one $request->setId(1); $request->setMethod('hello'); $request->addParam('World', 'name'); $collection->addRequest($request, function(Response $response) { if ($response->hasError()) { $error = $response->getError(); throw new \Exception($error->getMessage(), $error->getCode()); } else { echo $response->getResult(); } }); // Create request two $request = new Jdolieslager\JsonRpc\Entity\Request(); $request->setId(2); $request->setMethod('hello'); $request->addParam('Jesper', 'name'); $collection->addRequest($request, function(Response $response) { if ($response->hasError()) { $error = $response->getError(); throw new \Exception($error->getMessage(), $error->getCode()); } else { echo $response->getResult(); } }); // Responses holds all the responses // Index is the ID number $responses = $client->sendRequest($collection);
加密层(服务器端)
class Test { public function hello($name) { return 'Hello ' . ucfirst($name) . '!'; } } $layer = new Jdolieslager\JsonRpc\ProtocolLayer\ServerEncryption(array( 'key' => 'my secret pass here' )); $server = new Jdolieslager\JsonRpc\Server(); $server->registerHandler('Test'); $server->addProtocolLayer($layer, Jdolieslager\JsonRpc\Server::LAYER_PLACEMENT_BOTTOM); $server->printResponseForRawRequest(file_get_contents('php://input'));
加密层(客户端)
$client = new Jdolieslager\JsonRpc\Client( 'http://jsonrpc.mine' ); $layer = new Jdolieslager\JsonRpc\ProtocolLayer\ClientEncryption(array( 'key' => 'my secret pass here' )); $client->addProtocolLayer($layer, Jdolieslager\JsonRpc\Client::LAYER_PLACEMENT_TOP); var_dump($client->sendSimpleRequest('hello', array('world')));