PHP code example of qfrpc / yarrpc

1. Go to this page and download the library: Download qfrpc/yarrpc 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/ */

    

qfrpc / yarrpc example snippets


创建RPC服务
...
use QfRPC\YARRPC\QFRpcService;
use QfRPC\YARRPC\Core\RpcResponse;
...
$ak = '*** Provide your Access Key ***';
$sk = '*** Provide your Secret Key ***';

示例demo类

class Demo
{
    public function getTestPageList($params)
    {
        $list=[
            ['id'=>1,'name'=>'test1'],
            ['id'=>2,'name'=>'test2'],
            ['id'=>3,'name'=>'test3'],
        ];
        //构建rpc响应体
        $res=(new RpcResponse())->setCode(200)->setMessage('success')->setData($params);
        return $res;
    }
}
$client=(new QFRpcService(Demo::class))//创建RPC服务类
    ->allowItem('other1 Access Key','other1 Secret Key')//设置允许应用1的ak,sk
    ->allowItem('other2 Access Key','other2 Secret Key')//设置允许应用2的ak,sk
    ->run();//启动服务
...

调用RPC服务
...
use QfRPC\YARRPC\QFRpcClient;
...
$ak = '*** Provide your Access Key ***';
$sk = '*** Provide your Secret Key ***';
$endpoint = 'https://your-endpoint-uri';

示例demo类
$client = (new QFRpcClient($ak, $sk))
    ->withEndpoint($endpoint)
    ->newBuilder();

$params = $client::CreateRpcRequest()
    ->addItem('page_no', 1)
    ->addItem('page_size', 10);

$list = $client->send('getTestPageList', $params);

if ($list->getCode() == 200) {
    var_dump($list->getData());
} else {
    echo $list->getMessage();
}