Because the work needs to use gRPC, I use windows 10, but the operation flow of linux is basically the same.
Go directly to the topic, divided into several parts
1. Download Protoc
Download address: https://github.com/google/protobuf/releases
Select the corresponding platform and download the binary file used here
https://github.com/protocolbuffers/protobuf/releases/download/v3.7.1/protoc-3.7.1-win64.zip
After unzipping, I get it in protoc.exe under bin folder. I put it under D:\Program Files\protoc-3.7.1-win64\bin and add it to the system environment variable Path. Then I can use the protoc command. I can use protoc --version to check whether it is successful. (this step is very important for generating the PHP code corresponding to the protobuf file.)
2. Write protocol buffers file
How to write Protobuf3 Language Guide Good writing. First, I created a testgrpc com project. My location is e: \ www \ testgrpc com. I simply wrote a xuexitest.proto under the project
syntax = "proto3"; // Specify proto version package xuexitest; // Specify package name //Define the Xuexitest service service Xuexitest { //Define SayTest method rpc SayTest(TestRequest) returns (TestReply) {} } //TestRequest request structure message TestRequest { int64 typeid = 1; } //TestReply response structure message TestReply { //Return data type message GetData { int64 id = 1; string name = 2; } repeated GetData getdataarr = 1; }
3. Download gRPC extension and protobuf extension of PHP
gRPC extension for PHP: http://pecl.php.net/package/gRPC
protobuf extension of PHP: http://pecl.php.net/package/protobuf
Note: 1. The protobuf extension of PHP does not have the windows version, but it does not affect the use. It is just a little slower. (use composer to load the protobuf class library, see the composer.json configuration file below.)
Note: 2. How to install PHP extensions is not covered here.
4. Compile the protocol buffers file
Compile xuexitest.proto and use protoc --php_out=. xuexitest.proto to generate:
5. Use PHP composer
First, install composer to manage and load the PHP class library. How to install it will not be discussed here. (this step is also important to solve the PHP class library dependency of Grpc and protobuf)
Write composer.json under the project and put it under the same level of the newly compiled protocol buffers file
{ "name": "grpc-go-php", "require": { "grpc/grpc": "^v1.3.0", "google/protobuf": "^v3.3.0" }, "autoload":{ "psr-4":{ "GPBMetadata\\":"GPBMetadata/", "Xuexitest\\":"Xuexitest/" } } }
6. Use Composer to download PHP code
1. Enter the directory of the project, for example: e: \ www \ testgrpc? Com, enter cmd in the address bar and enter
2. Enter composer install at the command prompt (black window) and wait for the download to complete.
7. Define the gRPC client of PHP
In the Xuexitest folder of the project, create a new XuexitestClient.php file
<?php namespace Xuexitest; /** * service Xuexitest{} * Write a client (gprc defines the Xuexitest service) */ class XuexitestClient extends \Grpc\BaseStub{ public function __construct($hostname, $opts, $channel = null) { parent::__construct($hostname, $opts, $channel); } /** * rpc SayTest(TestRequest) returns (TestReply) {} * Try to use the same method name as (gprc defines the Xuexitest service) * For request and response services */ public function SayTest(\Xuexitest\TestRequest $argument,$metadata=[],$options=[]){ // (/ xuexitest.Xuexitest/SayTest) is the service and method that requests the server, basically the same as the proto file definition // (\ Xuexitest\TestReply) is the response information (that kind), basically the same as the proto file definition return $this->_simpleRequest('/xuexitest.Xuexitest/SayTest', $argument, ['\Xuexitest\TestReply', 'decode'], $metadata, $options); } }
8. Write PHP executive file
Create a new xuexitest.php under the project
?php //Automatic loading with composer require __DIR__ . '/vendor/autoload.php'; //Used to connect to the server $client = new \Xuexitest\XuexitestClient('127.0.0.1:50052', [ 'credentials' => Grpc\ChannelCredentials::createInsecure() ]); //Instantiate TestRequest request class $request = new \Xuexitest\TestRequest(); $request->setTypeid(1); //Call remote service $get = $client->SayTest($request)->wait(); //Return array //$reply is a TestReply object //$status is an array list($reply, $status) = $get; //array $getdata = $reply->getGetdataarr(); foreach ($getdata as $k=>$v){ echo $v->getId(),'=>',$v->getName(),"\n\r"; }
9. Hurry up
This is the source code: https://github.com/laixhe/php_grpc