原生PHP实现RPC(下)

使用到的函数如下:

  • stream_socket_client
  • fwrite
  • fread
  • fclose

创建客户端代:client.php

<?php

$client = stream_socket_client("tcp://127.0.0.1:8887", $errno,$errstr);
if(!$client){
    echo '异常代码:'.$errno.',异常信息:'.$errstr;exit;
}

$data['class'] = 'user';
$data['method'] = 'get_name';
$data['param'] = 10;
$_data = json_encode($data);
fwrite($client, $_data);

$server_data = fread($client, 2048);
$resutl = json_decode($server_data,true);
fclose($client);
print_r($resutl);

Fread fclose

Add a Comment

电子邮件地址不会被公开。 必填项已用*标注