1. Go to this page and download the library: Download spiral/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/ */
spiral / grpc-client example snippets
use Spiral\Grpc\Client\Config\GrpcClientConfig;
use Spiral\Grpc\Client\Config\ServiceConfig;
use Spiral\Grpc\Client\Config\ConnectionConfig;
use Spiral\Grpc\Client\Interceptor\SetTimoutInterceptor;
use Spiral\Grpc\Client\Interceptor\RetryInterceptor;
use Spiral\Grpc\Client\Interceptor\ExecuteServiceInterceptors;
new GrpcClientConfig(
interceptors: [
SetTimoutInterceptor::createConfig(10_000), // 10 seconds
RetryInterceptor::createConfig(
maximumAttempts: 3,
initialInterval: 100, // 0.1 seconds
backoffCoefficient: 1.5,
),
ExecuteServiceInterceptors::class,
],
services: [
new ServiceConfig(
connections: ConnectionConfig::createInsecure('my-service:9001'),
interfaces: [
\GRPC\MyService\MailSenderInterface::class,
\GRPC\MyService\BlackListInterface::class,
\GRPC\MyService\SubscriberInterface::class,
],
),
],
)
final class Sender
{
public function __construct(
private \GRPC\MyService\MailSenderInterface $mailSender,
) {}
public function sendMail(string $email, $subject, string $message): bool
{
$request = (new \GRPC\MyService\SendMailRequest())
->setEmail($email)
->setSubject($subject)
->setMessage($message);
$response = $this->mailSender->sendMail(new \Spiral\RoadRunner\GRPC\Context([]), $request);
return $response->getSuccess();
}
}
final class AuthContextInterceptor implements InterceptorInterface
{
public function __construct(
private readonly AuthContextInterface $authContext,
) {}
public function intercept(CallContextInterface $context, HandlerInterface $handler): mixed
{
$token = $this->authContext->getToken();
if ($token === null) {
return $handler->handle($context);
}
$metadata = \Spiral\Grpc\Client\Interceptor\Helper::withMetadata($context);
$metadata['auth-token'] = [$token];
return $handler->handle(\Spiral\Grpc\Client\Interceptor\Helper::withMetadata($context, $metadata));
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.