Matt Allan has a new post to his site sharing some of his experience in using PHP to write Protobuf services. Protobuf (short for "protocol buffers") are a language-agnostic data structure that allows for easy serialization.
Lately I’ve been investigating Protobuf as a replacement for JSON RPC services. If you aren’t familiar with Protobuf, it’s a language neutral serialization format from Google. It’s most commonly associated with Google’s RPC framework gRPC but it can be used standalone too. In this guide we are going to build a simple calculator RPC service using nothing but the Protobuf compiler and PHP.
The post starts by creating a ".proto" file, defining the structure of the data to be consumed. He includes the definition for the service itself, showing how to define a basic "Calculator" service with "add" and "subtract" methods. From there he defines the request and response formats for the data. The definitions are then compiled and classes are generated from the definitions. Switching to the PHP side, he sets up Composer autoloading and pulls in the
google/protobuf package. Next comes the PHP code to work with the service and serving it up via the built-in server. Finally, he shares the code to create a client for the service and uses it to make some requests.