Authors: Jitendra Singh Dikhit & Ashish Kumar
In this blog tutorial, we are going to setup protocol buffer compiler and gRPC extension for PHP language. After this, you will be able to generate proto buffer files with gRPC support for Hyperledger-Fabric proto files in PHP language.
Protocol Buffers (a.k.a., protobuf) are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data, useful in developing languages to communicate with each other. Protocol buffers are designed to emphasize simplicity and performance.
Read more about Google protobuf
gRPC is a client application, which can directly call methods on a server application on a different machine as if it was a local object, making it easier for you to create distributed applications and services.
Read more about gRPC
It is a business blockchain framework hosted by the Linux Foundation intended as a foundation for developing blockchain applications or solutions with a modular architecture. It is a platform for distributed ledger solutions with high degree of confidentiality, resiliency, flexibility, and scalability.
Read more about Hyperledger
PHP is a great option for many reasons; here are some reasons why the language may be right for you or your project:
- It's free and simple.
- Powerful, flexible, and scalable.
- Fast loading.
- Large open source support.
- Extensions and add-ons.
- Exceptional performance.
This installation showcase is performed on a Mac machine, which has brew installed. We are going to install PHP version 7.1 because protocol buffers only support up to version 7.1 thusfar.
brew install php71
php -v
If not php71, unlink your current version, link php71, and check the version again. For example:
brew unlink php56
brew link php71
php -v
brew install grpc php71-grpc
which grpc_php_plugin
brew install protobuf php71-protobuf
protoc --version
Create a directory named protos
and download helloworld.proto
file inside it; download sample proto file from helloworld.proto:
mkdir protos
touch protos/helloworld.proto
mkdir php
Generate PHP classes from proto file:
protoc --proto_path=protos/ --php_out=php/ --grpc_out=php/ --plugin=protoc-gen-grpc=`which grpc_php_plugin` protos/helloworld.proto
Additional commands on the PHP/gRPC site.
Check your generated folder structure:
tree php/
Output:
php/
├── GPBMetadata
│ └── Helloworld.php
└── Helloworld
├── GreeterClient.php
├── HelloReply.php
└── HelloRequest.php
Now, you are all setup for generating PHP files for Hyperledger-fabric proto files.
Hyperledger-fabric provides proto files to generate gRPC files in the supported language. We will download standard proto files given by Hyperledger community and compile them.
- Get the proto folder.
- Add/adjust local copy
php_namespace
(and other options) as necessary, until they are updated upstream. - Run above with
protoc
or create a bash file with the code to generate.
find ./protobuf/protos -name "*.proto" -exec protoc --proto_path=protobuf/protos/ --php_out=protobuf/dist/ --grpc_out=protobuf/dist/ --plugin=protoc-gen-grpc=`which grpc_php_plugin` {} \;
Check generated files with tree protobuf/dist/
All done!!! These files are ready to include and communicate with Hyperledger-fabric server.