mirror of
https://gitee.com/zoujingli/ThinkAdmin.git
synced 2025-08-26 14:49:44 +08:00
91 lines
2.8 KiB
PHP
91 lines
2.8 KiB
PHP
<?php
|
||
|
||
// +----------------------------------------------------------------------
|
||
// | Library for ThinkAdmin
|
||
// +----------------------------------------------------------------------
|
||
// | 版权所有 2014~2021 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
|
||
// +----------------------------------------------------------------------
|
||
// | 官方网站: https://gitee.com/zoujingli/ThinkLibrary
|
||
// +----------------------------------------------------------------------
|
||
// | 开源协议 ( https://mit-license.org )
|
||
// +----------------------------------------------------------------------
|
||
// | gitee 仓库地址 :https://gitee.com/zoujingli/ThinkLibrary
|
||
// | github 仓库地址 :https://github.com/zoujingli/ThinkLibrary
|
||
// +----------------------------------------------------------------------
|
||
|
||
declare (strict_types=1);
|
||
|
||
namespace think\admin\extend;
|
||
|
||
use think\admin\Exception;
|
||
|
||
/**
|
||
* JsonRpc 客户端
|
||
* Class JsonRpcClient
|
||
* @package think\admin\extend
|
||
*/
|
||
class JsonRpcClient
|
||
{
|
||
/**
|
||
* 请求ID
|
||
* @var integer
|
||
*/
|
||
private $id;
|
||
|
||
/**
|
||
* 服务端地址
|
||
* @var string
|
||
*/
|
||
private $proxy;
|
||
|
||
/**
|
||
* JsonRpcClient constructor.
|
||
* @param string $proxy
|
||
*/
|
||
public function __construct(string $proxy)
|
||
{
|
||
$this->proxy = $proxy;
|
||
$this->id = CodeExtend::uniqidNumber();
|
||
}
|
||
|
||
/**
|
||
* 执行 JsonRpc 请求
|
||
* @param string $method
|
||
* @param array $params
|
||
* @return mixed
|
||
* @throws Exception
|
||
*/
|
||
public function __call(string $method, array $params)
|
||
{
|
||
// Performs the HTTP POST
|
||
$options = [
|
||
'ssl' => [
|
||
'verify_peer' => false,
|
||
'verify_peer_name' => false,
|
||
],
|
||
'http' => [
|
||
'method' => 'POST',
|
||
'header' => 'Content-type: application/json',
|
||
'content' => json_encode([
|
||
'jsonrpc' => '2.0', 'method' => $method, 'params' => $params, 'id' => $this->id,
|
||
], JSON_UNESCAPED_UNICODE),
|
||
],
|
||
];
|
||
if ($fp = fopen($this->proxy, 'r', false, stream_context_create($options))) {
|
||
$response = '';
|
||
while ($row = fgets($fp)) $response .= trim($row) . "\n";
|
||
[, $response] = [fclose($fp), json_decode($response, true)];
|
||
} else {
|
||
throw new Exception("无法连接到 {$this->proxy}");
|
||
}
|
||
// Final checks and return
|
||
if ($response['id'] != $this->id) {
|
||
throw new Exception("错误标记 (请求标记: {$this->id}, 响应标记: {$response['id']})");
|
||
}
|
||
if (is_null($response['error'])) {
|
||
return $response['result'];
|
||
} else {
|
||
throw new Exception("请求错误:{$response['error']['message']}", $response['error']['code']);
|
||
}
|
||
}
|
||
} |