PHP gRPC Server Example
This is a simple demo, used to demonstrate how to use PHP as a gRPC service.
0. Prepare
When these installations encounter problems, please refer to: https://grpc.io/docs/quickstart/php.html
Install protoc
git clone -b $(curl -L https://grpc.io/release) https://github.com/grpc/grpc
cd grpc
git submodule update --init
make
sudo make installInstall PHP Protoc plugin
git clone -b $(curl -L https://grpc.io/release) https://github.com/grpc/grpc
cd grpc
git submodule update --init
make grpc_php_pluginInstall ext-protobuf
pecl install protobufInstall ext-grpc
pecl install ext-grpc1. Init project
Composer
composer installProtoc
protoc --php_out=./pb-src --grpc_out=./pb-src --plugin=protoc-gen-grpc=$(which grpc_php_plugin) ./protos/HelloWorld.proto2. Startup server
require_once __DIR__.'/vendor/autoload.php';
$server = new \Dev\Fyn\Server('0.0.0.0:50051');
$server->serve();3. Using client call gRPC Service
require_once __DIR__.'/vendor/autoload.php';
$client = new HelloWorldClient('127.0.0.1:50051',[
'credentials'=>\Grpc\ChannelCredentials::createInsecure()
]);
$request = new SayHelloRequest();
$request->setName('fyn');
$call = $client->SayHello($request);
[$response, $status] = $call->wait();
echo "Code: ", $status->code,"\n";
echo "Details: ", $status->details,"\n";
echo "Reply: ", ($response?$response->getReply():null),"\n";On this page
Contributors
Created March 18, 2019
Updated December 27, 2022