在protobuf生成的类中未定义php方法

时间:2019-02-28 22:33:56

标签: php grpc daml

我正在编写一个客户端应用程序以连接到DA沙箱。以下代码:

$grpc_channel = Grpc\ChannelCredentials::createInsecure();
    $client = new Com\Digitalasset\Ledger\Api\V1\LedgerIdentityServiceClient('localhost:7600', [
        'credentials' => $grpc_channel,
    ]);
    $request = new Com\Digitalasset\Ledger\Api\V1\GetLedgerIdentityRequest();
    $ledger_id_response = $client->GetLedgerIdentity($request);
    $ledger_id = $ledger_id_response->getLedgerId();

导致以下错误:

PHP Fatal error:  Uncaught Error: Call to undefined method Grpc\UnaryCall::getLedgerId() in /.../damlprojects/loaner_car/php/ledger_client.php:31

但是,应该定义它,因为$ ledger_id_response的类型为GetLedgerIdentityResponse,它确实具有方法:

public function getLedgerId()
{
    return $this->ledger_id;
}   

是什么原因导致错误?

2 个答案:

答案 0 :(得分:0)

这是一元电话吗?您尚未收到回复。到目前为止,$ ledger_id_response为空。

$call = $client->GetLedgerIdentity($request);
list($ledger_id_response, $status) = $call->wait();
if ($status->code == \Grpc\STATUS_OK) {
  $ledger_id = $ledger_id_response->getLedgerId();
}

答案 1 :(得分:0)

grpc's website

上的hello world示例中仔细检查了“客户端代码”之后
$request = new Helloworld\HelloRequest();
$request->setName($name);
list($reply, $status) = $client->SayHello($request)->wait();

我意识到自己的错误。
1.提出服务请求时。必须通过在返回对象上调用wait()来完成。因此

$client->GetLedgerIdentity($request);

必须更改为

$client->GetLedgerIdentity($request)->wait();

2。返回值以数组形式出现。因此

$ledger_id_response = must be changed to

list($ledger_id_response, $status) =

像这样

list($ledger_id_response, $status) = $client->GetLedgerIdentity($request)->wait();

现在可以调用getLedgerId

$ledger_id = $ledger_id_response->getLedgerId();

没有错误!