This repository contains the PHP classes for the public CBWS APIs: the messages and the clients. A process generates them from the definitions in cbws/protobufs. Do not change the generated files by hand. The transport classes in src/Transport are not generated.
To install the package with Composer, run this command:
composer require cbws/php-cbws-grpc
The grpc PHP extension is optional. Without it, the clients use gRPC-Web over HTTPS. The protobuf PHP extension is optional and gives better performance.
Create one transport for each API endpoint. Give the same transport to every client for that endpoint.
use Cbws\Grpc\Compute\V1alpha1\ComputeServiceClient;
use Cbws\Grpc\Compute\V1alpha1\ListMachinesRequest;
use Cbws\Grpc\Transport\Transports;
$transport = Transports::create('compute.cbws.cloud:443', [
'metadata_provider' => fn (array $metadata): array =>
$metadata + ['authorization' => ['Bearer ' . $token]],
]);
$compute = new ComputeServiceClient($transport);
[$response, $status] = $compute->ListMachines(new ListMachinesRequest(['parent' => 'projects/my-project']))->wait();
if ($status->code !== \Cbws\Grpc\Transport\Status::OK) {
throw new RuntimeException($status->details);
}Transports::create() selects the transport:
- If the
grpcextension is loaded, it uses native gRPC. - If the extension is not loaded, it uses gRPC-Web.
- To use gRPC-Web when the extension is loaded, set
'transport' => 'grpc-web'.
Send credentials with metadata_provider. It runs before every call.
Earlier versions of this package generated clients that extend \Grpc\BaseStub. The clients now have the same names and methods, but they call a Cbws\Grpc\Transport\Transport:
new ComputeServiceClient($hostname, ['credentials' => ...])still works and uses native gRPC.- A method returns a
Cbws\Grpc\Transport\UnaryCall. Itswait()gives[$response, $status], as before.$statushas the samecode,detailsandmetadataproperties. - Code that uses the types
\Grpc\BaseStubor\Grpc\UnaryCallmust change.
The tags of this repository have the same version numbers as the tags of cbws/protobufs. Each commit names the commit of cbws/protobufs it was generated from.
The package is available under the Apache License, Version 2.0. Refer to the LICENSE file.