Home > Backend Development > PHP Tutorial > rpc - Regarding the problem that thrift --gen php:rest parameters have nested structures?

rpc - Regarding the problem that thrift --gen php:rest parameters have nested structures?

WBOY
Release: 2016-09-23 11:31:02
Original
1322 people have browsed it

使用thrift, php作为客户端。

thrift协议如下:

<code>service
struct A{
    required i32 a1
}

struct B{
    required i32 b1,
    required A a
}
service TestService{
    resa testa(1:required a reqa);
    resb testb(1:required B reqb);
}</code>
Copy after login
Copy after login

调用代码如下:

<code>//socket
$socket = new \Thrift\Transport\TSocket($host, $port, $name);
$socket->setSendTimeout($timeout);
$socket->setRecvTimeout($timeout);

//transport
$transport = new \Thrift\Transport\TFramedTransport($socket);
$transport->open();

//protocol
$protocol           = new \Thrift\Protocol\TBinaryProtocol($transport);
$clientName    = "\\ThriftClient\\{$name}Client";
$restName      = "\\ThriftClient\\{$name}Rest";
$connection       = new $restName(new $clientName($protocol));

//调用成功
$connection->testa(json_encode(['a1'=>2]));
//参数是嵌套的结构体,则调用失败
$connection->testb(json_encode(['b1'=>1,a=>['a1'=>2]]));</code>
Copy after login
Copy after login

是不是thrift --gen php:rest不支持嵌套结构体参数?

回复内容:

使用thrift, php作为客户端。

thrift协议如下:

<code>service
struct A{
    required i32 a1
}

struct B{
    required i32 b1,
    required A a
}
service TestService{
    resa testa(1:required a reqa);
    resb testb(1:required B reqb);
}</code>
Copy after login
Copy after login

调用代码如下:

<code>//socket
$socket = new \Thrift\Transport\TSocket($host, $port, $name);
$socket->setSendTimeout($timeout);
$socket->setRecvTimeout($timeout);

//transport
$transport = new \Thrift\Transport\TFramedTransport($socket);
$transport->open();

//protocol
$protocol           = new \Thrift\Protocol\TBinaryProtocol($transport);
$clientName    = "\\ThriftClient\\{$name}Client";
$restName      = "\\ThriftClient\\{$name}Rest";
$connection       = new $restName(new $clientName($protocol));

//调用成功
$connection->testa(json_encode(['a1'=>2]));
//参数是嵌套的结构体,则调用失败
$connection->testb(json_encode(['b1'=>1,a=>['a1'=>2]]));</code>
Copy after login
Copy after login

是不是thrift --gen php:rest不支持嵌套结构体参数?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template