-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_rpc.php
More file actions
executable file
·59 lines (49 loc) · 1.56 KB
/
test_rpc.php
File metadata and controls
executable file
·59 lines (49 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
class RpcClient
{
const RPC_EOL = "\r\n\r\n";
public function request($host, $class, $method, $param, $version='1.0', $ext=[])
{
$fp = stream_socket_client($host, $errno, $errstr);
if (!$fp) {
throw new Exception ("stream_socket_client fail error:{$errno}, errstr:{$errstr}");
}
$req = ['jsonrpc' =>'2.0',
'method' => sprintf("%s::%s::%s", $version, $class, $method),
'params' => $param,
'id' => '',
'ext' => $ext
];
$data = json_encode($req). self::RPC_EOL;
fwrite($fp, $data);
$result = '';
while (! feof($fp)) {
$tmp = stream_socket_recvfrom($fp, 1024);
if ($pos = strpos($tmp, self::RPC_EOL)) {
$result .= substr($tmp, 0, $pos);
break;
} else {
$result .= $tmp;
}
}
fclose($fp);
return json_decode($result, true);
}
}
$client = new RpcClient();
$method = '';
switch ($_GET['opr']) {
case 'getList':
$method = 'getList';
$param = [1,2];
break;
case 'getBigContent':
$method = 'getBigContent';
$param = [];
break;
default:
break;
}
//注意 第二个参数实际上是服务器上的UserInterface的全路径
$ret = $client->request('tcp://192.168.56.102:18308', \App\Rpc\Lib\UserInterface::class, $method, $param, '1.0');
var_dump($ret);