PHP code example of tinywan / rpc

1. Go to this page and download the library: Download tinywan/rpc library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tinywan / rpc example snippets


namespace service;
class User
{
    public function get($args)
    {
        return response_rpc_json(0,'获取成功', $args);
    }
}

// 建立socket连接到内部推送端口
$resource = stream_socket_client('tcp://127.0.0.1:9512', $errorCode, $errorMessage);
if (false === $resource) {
    throw new \Exception('rpc failed to connect: '.$errorMessage);
}
$request = [
    'class'   => 'user',
    'method'  => 'get',
    'args'    => [
        [
            'uid' => 2023,
            'username' => 'Tinywan',
        ]
    ]
];
// 发送数据,注意5678端口是Text协议的端口,Text协议需要在数据末尾加上换行符
fwrite($resource, json_encode($request)."\n"); 
// 读取推送结果
$result = fgets($resource, 10240000);
fclose($resource);
// 解析JSON字符串
$result = json_decode($result, true);
var_export($result);
regexp
1. 接口方法  
   包括接口名、方法名  
2. 方法参数  
   包括参数类型、参数值  
3. 调用属性  
   包括调用属性信息,例如调用附件隐式参数、调用超时时间等  
-- 返回编码 --  
1. 返回结果  
   接口方法中定义的返回值  
2. 返回码  
   异常返回码  
3. 返回异常信息  
   调用异常信息