gRPC for PHP: Libraries and Implementation Examples
gRPC (gRPC Remote Procedure Calls) is a high-performance framework for remote procedure calls developed by Google. It uses Protocol Buffers (protobuf) as the interface description language and data serialization format. Unlike REST, gRPC works over HTTP/2, supports bidirectional streaming, and provides strict typing. In this article, we will cover how to integrate gRPC in PHP, which libraries to use, and write a complete client-server interaction example.
Overview of Libraries for gRPC in PHP
Two main approaches are available for working with gRPC in PHP:
- grpc.so extension — the official C extension from gRPC. Provides maximum performance but requires compilation and server configuration.
- Pure PHP client (e.g.,
grpc/grpcvia Composer) — a PHP implementation working through cURL and HTTP/2. Slower but easier to deploy.
For production, it is recommended to use the grpc extension, while for development or testing, the pure PHP client is suitable.
Installing Required Components
1. Installing protoc and the PHP Plugin
Download the protobuf compiler from the official repository for your OS. To generate PHP code, you will need the protoc-gen-php-grpc plugin:
# Installing protoc (Linux)wget https://github.com/protocolbuffers/protobuf/releases/download/v25.0/protoc-25.0-linux-x86_64.zipunzip protoc-25.0-linux-x86_64.zip -d /usr/local/protocexport PATH=$PATH:/usr/local/protoc/bin
# Installing the gRPC PHP pluginsudo apt-get install php-dev php-cli php-pearsudo pecl install grpcsudo pecl install protobuf2. Installing Composer Dependencies
Create a project and add the necessary packages:
composer initcomposer require grpc/grpccomposer require google/protobufCreating a .proto File
Let's define a simple service for working with users. Create a file user.proto:
syntax = "proto3";
package user;
service UserService { rpc GetUser (UserRequest) returns (UserResponse); rpc ListUsers (Empty) returns (stream UserResponse);}
message UserRequest { int32 id = 1;}
message UserResponse { int32 id = 1; string name = 2; string email = 3;}
message Empty {}Generating PHP Code from .proto
Run the command to compile .proto into PHP classes:
protoc --php_out=./generated --grpc_out=./generated \ --plugin=protoc-gen-grpc=/usr/local/bin/grpc_php_plugin \ user.protoThe following files will appear in the generated folder:
GPBMetadata/User.php— proto file metadataUser/UserRequest.php— request classUser/UserResponse.php— response classUser/UserServiceClient.php— client for calling the service
Implementing a gRPC Server in PHP
For the server side, use the official grpc extension. Create a file server.php:
<?phprequire_once __DIR__ . '/vendor/autoload.php';require_once __DIR__ . '/generated/User/UserServiceInterface.php';require_once __DIR__ . '/generated/User/UserServiceServer.php';
use Grpc\\Server;use User\\UserRequest;use User\\UserResponse;use User\\UserServiceServer;
class MyUserService extends UserServiceServer{ public function GetUser(\\User\\UserRequest $request): \\User\\UserResponse { $response = new UserResponse(); $response->setId($request->getId()); $response->setName('John Doe'); $response->setEmail('john@exa