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

问题描述 投票:0回答:2

我正在编写一个客户端应用程序来连接到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;
}   

是什么导致错误?

php grpc daml
2个回答
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();
}

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();

没有错误!

© www.soinside.com 2019 - 2024. All rights reserved.