gRPC for PHP: Libraries and Examples
gRPC is a modern high-performance framework for remote procedure calls (RPC), developed by Google. It uses HTTP/2 for transport, Protocol Buffers (protobuf) for data serialization, and supports many languages, including PHP. In this article, we will cover how to integrate gRPC into a PHP project, what libraries are needed, and write a working example of client-server interaction.
Why gRPC in PHP?
Traditional REST APIs often work via JSON and HTTP/1.1, which leads to data redundancy and slow serialization. gRPC solves these problems:
- Binary protocol — protobuf takes up less space than JSON and processes faster.
- HTTP/2 — multiplexing, header compression, streaming.
- Strong typing — contracts are described in .proto files, simplifying development and debugging.
- Bidirectional streaming — server and client can exchange data asynchronously.
Main Libraries for gRPC in PHP
To work with gRPC in PHP, two components are required:
- grpc.so — PHP extension (C extension) implementing HTTP/2 and the gRPC protocol.
- protobuf.so — extension for protobuf serialization (alternative — pure PHP package
google/protobuf). - Composer packages:
grpc/grpcandgoogle/protobuf.
The recommended installation method is via PECL (extensions) and Composer (dependencies).
Installing gRPC and protobuf
For Linux servers (Ubuntu/Debian):
# Install extensions via PECLsudo pecl install grpcsudo pecl install protobuf
# Add to php.iniecho "extension=grpc.so" >> /etc/php/8.2/cli/php.iniecho "extension=protobuf.so" >> /etc/php/8.2/cli/php.iniFor Windows, download DLL files from PECL and specify them in php.ini.
Next, install the libraries via Composer:
composer require grpc/grpccomposer require google/protobufCreating a .proto File
Let's describe a 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 (UserList);}
message UserRequest { int32 id = 1;}
message UserResponse { int32 id = 1; string name = 2; string email = 3;}
message Empty {}
message UserList { repeated UserResponse users = 1;}Generating PHP Classes from .proto
Install protoc and the PHP plugin:
# Download protoc from https://github.com/protocolbuffers/protobuf/releases# Then generate the code:protoc --php_out=./generated --grpc_out=./generated --plugin=protoc-gen-grpc=/usr/local/bin/grpc_php_plugin user.protoIn the generated folder, classes will appear: UserRequest, UserResponse, UserServiceClient, etc.
Example gRPC Server in PHP
Let's create a server that will respond to requests. File server.php:
require_once __DIR__ . '/vendor/autoload.php';require_once __DIR__ . '/generated/UserServiceServer.php';
use Grpc\\Server;use User\\UserServiceServer;use User\\UserRequest;use User\\UserResponse;
class MyUserService extends UserServiceServer{ public function GetUser(\\Grpc\\ServerCallReader $request, \\Grpc\\ServerCallWriter $response, \\Grpc\\ServerContext $context): void { $req = $request->read(); $id = $req->getId();
$user = new UserResponse(); $user->setId($id); $