1. Go to this page and download the library: Download thesis/grpc-client 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/ */
thesis / grpc-client example snippets
use Auth\Api\V1\AuthenticateRequest;
use Auth\Api\V1\AuthenticationServiceClient;
use Thesis\Grpc\Client;
$client = new AuthenticationServiceClient(
new Client\Builder()
->withHost('dns:///127.0.0.1:50051')
->build(),
);
$response = $client->authenticate(new AuthenticateRequest('root', 'secret'));
use Thesis\Grpc\Metadata;
$response = $client->authenticate(
new AuthenticateRequest('root', 'secret'),
new Metadata()->with('x-request-id', 'req-123'),
);
use Amp\Socket\Certificate;
use Thesis\Grpc\Client;
$client = new Client\Builder()
->withTransportCredentials(
new Client\TransportCredentials()
->withCaCert('/certs/ca.crt')
->withPeerName('localhost')
->withCertificate(new Certificate('/certs/client.crt', '/certs/client.key')), // optional (mTLS)
)
->build();
use Thesis\Grpc\Client\LoadBalancer\RoundRobinFactory;
$client = new Client\Builder()
->withHost('ipv4:10.0.0.1:50051,10.0.0.2:50051')
->withLoadBalancer(new RoundRobinFactory())
->build();
use Random\Randomizer;
use Thesis\Grpc\Client\Endpoint;
use Thesis\Grpc\Client\LoadBalancer;
use Thesis\Grpc\Client\LoadBalancerFactory;
use Thesis\Grpc\Client\PickContext;
final class RandomBalancer implements LoadBalancer
{
/**
* @param non-empty-list<Endpoint> $endpoints
*/
public function __construct(
private array $endpoints,
private readonly Randomizer $randomizer = new Randomizer(),
) {}
#[\Override]
public function refresh(array $endpoints): void
{
$this->endpoints = $endpoints;
}
#[\Override]
public function pick(PickContext $context): Endpoint
{
return $this->endpoints[$this->randomizer->getInt(0, \count($this->endpoints) - 1)];
}
}
final readonly class RandomBalancerFactory implements LoadBalancerFactory
{
#[\Override]
public function name(): string
{
return 'random';
}
#[\Override]
public function create(array $endpoints): LoadBalancer
{
return new RandomBalancer($endpoints);
}
}
use Amp\Cache\LocalCache;
use Thesis\Grpc\Client\Builder;
use Thesis\Grpc\Client\EndpointResolver\DnsResolver;
$client = new Builder()
->withHost('dns:///my-grpc-server:50051')
->withEndpointResolver('dns', new DnsResolver(
cache: new LocalCache(),
minResolveInterval: 60,
maxResolveInterval: 600,
))
->build();
$client = new Builder()
->withEndpointResolver('etcd', new EtcdResolver($etcd, prefix: '/services/'))
->withLoadBalancer(new RoundRobinFactory())
->withHost('etcd:///echo')
->build();
use Thesis\Grpc\Compression\GzipCompressor;
$client = new Client\Builder()
->withCompression(new GzipCompressor())
->build();
use Amp\Cancellation;
use Thesis\Grpc\Client;
use Thesis\Grpc\Client\Invoke;
use Thesis\Grpc\Client\StreamInterceptor;
use Thesis\Grpc\Client\UnaryInterceptor;
use Thesis\Grpc\ClientStream;
use Thesis\Grpc\Metadata;
final readonly class ClientAuthInterceptor implements UnaryInterceptor, StreamInterceptor
{
#[\Override]
public function interceptUnary(object $request, Invoke $invoke, Metadata $md, Cancellation $cancellation, callable $invoker): object
{
return $invoker($request, $invoke, $md->with('Authorization', 'supertoken'), $cancellation);
}
#[\Override]
public function interceptStream(Invoke $invoke, Metadata $md, Cancellation $cancellation, callable $newStream): ClientStream
{
return $newStream($invoke, $md->with('Authorization', 'supertoken'), $cancellation);
}
}
$auth = new ClientAuthInterceptor();
$client = new Client\Builder()
->withInterceptors($auth)
->build();
use File\Api\V1\Chunk;
use File\Api\V1\FileServiceClient;
$files = new FileServiceClient(new Client\Builder()->build());
$upload = $files->upload();
for ($i = 0; $i < 10; ++$i) {
$upload->send(new Chunk(random_bytes(10)));
}
$info = $upload->close(); // FileInfo
dump($info->size); // 100
use Topic\Api\V1\SubscribeRequest;
use Topic\Api\V1\TopicServiceClient;
$topics = new TopicServiceClient(new Client\Builder()->build());
$stream = $topics->subscribe(new SubscribeRequest('payments'));
foreach ($stream as $event) {
dump($event->name, $event->payload);
}
use Chat\Api\V1\Message;
use Chat\Api\V1\MessengerServiceClient;
$chat = new MessengerServiceClient(new Client\Builder()->build())->chat();
$chat->send(new Message('Hi from gRPC client'));
dump($chat->receive()->text);
$chat->send(new Message('Bye'));
$chat->close();
dump($chat->receive()->text);