1. Go to this page and download the library: Download mosamirzz/octane-grpc 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/ */
mosamirzz / octane-grpc example snippets
return [
// ...
/*
|--------------------------------------------------------------------------
| gRPC Server
|--------------------------------------------------------------------------
|
| The following setting used by the swoole gRPC server.
|
*/
'grpc' => [
'host' => '0.0.0.0',
'port' => 50051,
'mode' => OpenSwoole\Http\Server::POOL_MODE,
'services_path' => base_path('routes/grpc.php'),
],
];
use Proto\Greeter\GreeterService;
return [
// GreeterService::class => new GreeterService(),
];
declare(strict_types=1);
namespace Proto\Greeter;
use OpenSwoole\GRPC;
class GreeterService implements GreeterInterface
{
/**
* @param GRPC\ContextInterface $ctx
* @param HelloRequest $request
* @return HelloReply
*
* @throws GRPC\Exception\InvokeException
*/
public function SayHello(GRPC\ContextInterface $ctx, HelloRequest $request): HelloReply
{
// 1. get the request data
$name = $request->getName();
// 2. write any business logic here
$message = "Hello " . $name;
// 3. build the response object
$response = new HelloReply();
$response->setMessage($message);
// 4. return the response
return $response;
}
}
use Proto\Greeter\GreeterService;
return [
GreeterService::class => new GreeterService(),
];
use Proto\Greeter\GreeterClient;
use Proto\Greeter\HelloRequest;
50051", [
'credentials' => Grpc\ChannelCredentials::createInsecure(),
]);
// build the request data
$request = new HelloRequest();
$request->setName("Mohamed Samir");
// send the request and wait for the response
/** @var Proto\Greeter\HelloReply $response */
[$response, $status] = $client->SayHello($request)->wait();
// check the status code of the response and get the error details
if ($status->code !== Grpc\STATUS_OK) {
echo "ERROR: " . $status->code . ", " . $status->details . PHP_EOL;
exit(1);
}
// do anything with the response
var_dump($response->getMessage());