輕量級 Json-RPC 2.0 client/server 的php擴展:JsonRPC

jopen 9年前發布 | 14K 次閱讀 WEB服務/RPC/SOA jsonrpc

JsonRPC 2.0 Client and Server
=============================

輕量級 Json-RPC 2.0 客戶端和服務端的php擴展,基于 multi_curl + epoll的并發客戶端,依據[jsonrpc](http://www.jsonrpc.org/)協議規范。

服務端:

$server = new Jsonrpc_Server();

// style one function variable
$add1 = function($a, $b){
    return $a + $b;
};
$server->register('addition1', $add1);

// style two function string
function add2($a, $b){
  return $a + $b;
}
$server->register('addition2', 'add2');

// style three function closure
$server->register('addition3', function ($a, $b) {
    return $a + $b;
});

//style four class method string
class A 
{
  static public function add($a, $b)
  {
    return $a + $b;
  }
}
$server->register('addition4', 'A::add');

echo $server->execute();

//output >>>
//{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error"}}

客戶端:

$client = new Jsonrpc_Client(1);
$client->call('http://localhost/server.php', 'addition1', array(3,5));
$client->call('http://localhost/server.php', 'addition2', array(10,20));
$client->call('http://localhost/server.php', 'addition3', array(2,8));
$client->call('http://localhost/server.php', 'addition4', array(6,15));
/* ... */
$result = $client->execute();

var_dump($result);

//output >>>
/*
array(2) {
  [0]=>
  array(3) {
    ["jsonrpc"]=>
    string(3) "2.0"
    ["id"]=>
    int(110507766)
    ["result"]=>
    int(8)
  }
  [1]=>
  array(3) {
    ["jsonrpc"]=>
    string(3) "2.0"
    ["id"]=>
    int(1559316299)
    ["result"]=>
    int(30)
  }
  ...
}
*/

項目主頁:http://www.baiduhome.net/lib/view/home/1447294284428

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!