v2.1.4 2019-04-16 14:40 UTC


gRPC plugin and sdk for php.


composer require lvht/grpc

Generate PHP SDK

run the following command to generate php sdk for helloworld.proto

protoc --php_out=out --grpc-php_out=composer_name=grpc/hello:out \
    --plugin=protoc-gen-grpc-php=./vendor/bin/protoc-gen-grpc-php \

you will get

├── composer.json                   -- make sdk as a composer package
├── GPBMetadata
│   └── Helloworld.php
└── Helloworld
    ├── GreeterService.php          -- php interface for service
    ├── GreeterServiceTrait.php     -- generated service helper code
    ├── GreeterServiceStub.php      -- generated service stub code
    ├── HelloReply.php              -- message class generated by protoc
    └── HelloRequest.php            -- message class generated by protoc

For XXX service define in proto, you will get a XXX interface. The XXX interface has one implementation, XXXServiceStub for client side. And you need develop your own server side implementation using the XXXServerTrait.

Please see the example/client.php and example/server.php for more detail.


protoc-gen-grpc-php offer some arguments.

  • composer_name set the generated code's package name
  • stub_trait add additional trait to the generated stub
  • stub_trait_only replace the default trait of the generated stub

Simple gRPC

Besides the standard gRPC, the generated code has two additional features.

gRPC over http/1.1

Set the use_http1 flag to true when you create a stub instance will let stub use http1.1 to transfer rpc data. Both grpc-status and grpc-message will be transfered as a normal http header instead of Trailers.

simple gRPC or sRPC

Set the Content-Type to application/json will let stub use a more simple mode to transfer rpc data. In the simple mode, only json is supported, and the message prefix must not be transfered. Both http/1 and http/2 are supported.

POST /helloworld.Greeter/SayHello HTTP/1.1
Accept: application/json, */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 24
Content-Type: application/json
Host: localhost:8080
User-Agent: HTTPie/0.9.9
foo: bar

    "name": "海涛"

HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 26
Date: Sat, 24 Feb 2018 08:09:20 GMT
Server: swoole-http-server
b-bin: 5L2g5aW9
content-type: application/json
grpc-status: 0

    "message": "Hello 海涛"