DuckPhp\Ext\JsonRpcExt

简介

组件类 json-rpc 扩展

选项

‘jsonrpc_namespace’ => ‘JsonRpc’, ‘jsonrpc_backend’ => ‘https://127.0.0.1‘, ‘jsonrpc_is_debug’ => false, ‘jsonrpc_enable_autoload’ => true, ‘jsonrpc_check_token_handler’ => null, ‘jsonrpc_wrap_auto_adjust’ => true, ‘jsonrpc_service_interface’ => ‘’, ‘jsonrpc_service_namespace’ => ‘’,

示例

  1. // Base\App onInit;
  2. $this->options['ext']['Ext\JsonRpcExt']=[
  3. 'jsonrpc_backend'=>['http://test.duckphp.dev/json_rpc','127.0.0.1:80'],
  4. ];

/////////////

  1. <?php declare(strict_types=1);
  2. /**
  3. * DuckPHP
  4. * From this time, you never be alone~
  5. */
  6. require(__DIR__.'/../../../autoload.php'); // @DUCKPHP_HEADFILE
  7. use DuckPhp\DuckPhp;
  8. use DuckPhp\SingletonEx\SingletonEx;
  9. use DuckPhp\Ext\JsonRpcExt;
  10. use JsonRpc\CalcService as RemoteCalcService;
  11. class CalcService
  12. {
  13. use SingletonEx;
  14. public function add($a,$b)
  15. {
  16. return $a+$b;
  17. }
  18. }
  19. class Main
  20. {
  21. public function index()
  22. {
  23. $t=CalcService::G()->add(1,2);
  24. var_dump($t);
  25. CalcService::G(JsonRpcExt::Wrap(CalcService::class));
  26. $t=CalcService::G()->add(3,4);
  27. var_dump($t);
  28. $t=RemoteCalcService::G()->add(5,6);
  29. var_dump($t);
  30. var_dump(DATE(DATE_ATOM));
  31. }
  32. public function json_rpc()
  33. {
  34. $ret=JsonRpcExt::G()->onRpcCall($_POST);
  35. echo json_encode($ret);
  36. }
  37. }
  38. $options=[
  39. 'is_debug'=>true,
  40. 'skip_setting_file'=>true,
  41. 'namespace_controller'=>'\\',
  42. 'ext'=> [
  43. JsonRpcExt::class =>[
  44. 'jsonrpc_namespace'=>'JsonRpc',
  45. 'jsonrpc_is_debug'=>true,
  46. //'jsonrpc_backend'=>'';
  47. ],
  48. ],
  49. ];
  50. DuckPhp::RunQuickly($options,function(){
  51. $url=DuckPhp::Domain().$_SERVER['SCRIPT_NAME'].'/json_rpc';
  52. $ip=$_SERVER['SERVER_ADDR'].':'.$_SERVER['SERVER_PORT'];
  53. JsonRpcExt::G()->options['jsonrpc_backend']=[$url,$ip];
  54. });

这个例子,将会两次远程调用 json_rpc 的 CalcService 。

这里的 json_rpc 是服务端的实现

如果你要 做自己的权限处理,则重写 protected function prepare_token($ch)。

  1. public function __construct()
  2. public function init(array $options, object $context = null)
  3. public function clear()
  4. public function getRealClass($object)
  5. public static function Wrap($class)
  6. public static function _Wrap($class)
  7. public function _autoload($class)
  8. public function callRpc($classname, $method, $arguments)
  9. public function onRpcCall(array $input)
  10. protected function curl_file_get_contents($url, $post)
  11. protected function prepare_token($ch)