How to start using gRPC with PHP - Part 3/4
Static Grpc Client Generation and extensions for PHP The first step to use a gRPC as a client is to generate the static files from the related protobuf file. Is expected a server application to provide all the protobuf files used to specify the services. Each language has unique requirements to process those files and generate the clients and types, here we will use the PHP way. For PHP, this process is a bit more complex because we need to compile the grpc.so extension. This step took me many hours of reading and testing, which, by the way, is why I decided to write this guide. Once this step is complete, we will have: The grpc.so extension enabled in our PHP server, allowing the use of gRPC features. The PHP classes, including the ServiceClient and stubs, to interact with the available gRPC services. The GPBM metadata files, which hold the gRPC service definitions and metadata. And finally, the Request and Response types, which serve as the structured messages exchanged between the client and server. protoc compiler, protoc-gen-grpc plugin and grpc.so As I did in the last section, I’m going to provide a Docker image that handles the heavy work for us. My image is an adaptation of the [official-grpc-php-samples][]. My version uses php:8.3-fpm-alpine and, at the time of writing, is up to date. Keeping this image ready to use is important because it essentially freezes the tools you will use to convert and generate the helper classes from the protofiles. The benefit of keeping the image as frozen is that you can use it as a multi-stage Docker image and copy the protoc compiler, protoc-gen-grpc plugin, and grpc directly to your development or production environment. The Docker image, named getjv/grpc-php-base, provides the protoc compiler, the protoc-gen-grpc plugin, and the grpc.so extension. It is currently built with php8.3. In the future, you can update the image to use a newer PHP version (e.g., php:8.4-fpm-alpine) and rebuild it if necessary. Keeping the pre-built image ensures you avoid the build time, which is approximately 559.5 seconds. You can check the script to generete the static client files here This is the complete explanation of how to generate these dependencies. You can explore further using all the provided links. Now, it's finally time to code the project and make use of the gRPC client. See you in the next article!

Static Grpc Client Generation and extensions for PHP
The first step to use a gRPC as a client is to generate the static files from the related protobuf file.
Is expected a server application to provide all the protobuf files used to specify the services. Each language has unique requirements to process those files and generate the clients and types, here we will use the PHP way.
For PHP, this process is a bit more complex because we need to compile the grpc.so
extension. This step took me many hours of reading and testing, which, by the way, is why I decided to write this guide.
Once this step is complete, we will have:
- The
grpc.so
extension enabled in our PHP server, allowing the use of gRPC features. - The PHP classes, including the ServiceClient and stubs, to interact with the available gRPC services.
- The GPBM metadata files, which hold the gRPC service definitions and metadata.
- And finally, the Request and Response types, which serve as the structured messages exchanged between the client and server.
protoc compiler, protoc-gen-grpc plugin and grpc.so
As I did in the last section, I’m going to provide a Docker image that handles the heavy work for us.
My image is an adaptation of the [official-grpc-php-samples][]. My version uses php:8.3-fpm-alpine and, at the time of writing, is up to date.
Keeping this image ready to use is important because it essentially freezes the tools you will use to convert and generate the helper classes from the protofiles.
The benefit of keeping the image as frozen is that you can use it as a multi-stage Docker image and copy the protoc
compiler, protoc-gen-grpc
plugin, and grpc
directly to your development or production environment.
The Docker image, named getjv/grpc-php-base, provides the protoc
compiler, the protoc-gen-grpc
plugin, and the grpc.so
extension.
It is currently built with php8.3
. In the future, you can update the image to use a newer PHP version (e.g., php:8.4-fpm-alpine
) and rebuild it if necessary.
Keeping the pre-built image ensures you avoid the build time, which is approximately 559.5 seconds.
You can check the script to generete the static client files here
This is the complete explanation of how to generate these dependencies. You can explore further using all the provided links.
Now, it's finally time to code the project and make use of the gRPC client.
See you in the next article!