PHP Quick Start

This guide gets you started with gRPC in PHP with a simple working example.

Prerequisites

  • PHP 5.5 or higher, 7.0 or higher
  • PECL
  • Composer
  • PHPUnit (optional)

Install PHP and PECL on Ubuntu/Debian:

For PHP5:

  1. $ sudo apt-get install php5 php5-dev php-pear phpunit

For PHP7:

  1. $ sudo apt-get install php7.0 php7.0-dev php-pear phpunit

or

  1. $ sudo apt-get install php php-dev php-pear phpunit

Install PHP and PECL on CentOS/RHEL 7:

  1. $ sudo rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
  2. $ sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
  3. $ sudo yum install php56w php56w-devel php-pear phpunit gcc zlib-devel

Install PHP and PECL on Mac:

  1. $ brew install homebrew/php/php56-grpc
  2. $ curl -O http://pear.php.net/go-pear.phar
  3. $ sudo php -d detect_unicode=0 go-pear.phar

Install Composer (Linux or Mac):

  1. $ curl -sS https://getcomposer.org/installer | php
  2. $ sudo mv composer.phar /usr/local/bin/composer

Install PHPUnit (Linux or Mac):

  1. $ wget https://phar.phpunit.de/phpunit-old.phar
  2. $ chmod +x phpunit-old.phar
  3. $ sudo mv phpunit-old.phar /usr/bin/phpunit

Install the gRPC PHP extension

There are two ways to install gRPC PHP extension:

  • pecl
  • build from source

Using PECL

  1. sudo pecl install grpc

or specific version

  1. sudo pecl install grpc-1.7.0

Warning

This step unfortunately won’t work on CentOS/RHEL 6. Please follow the instructions below to compile the PECL extension from source.

Install on Windows

You can download the pre-compiled gRPC extension from the PECLwebsite

Build from Source with gRPC C core library

Clone this repository at given release tag

  1. $ git clone -b v1.28.1 https://github.com/grpc/grpc

Build and install the gRPC C core library

  1. $ cd grpc
  2. $ git submodule update --init
  3. $ make
  4. $ sudo make install

Build and install gRPC PHP extension

Compile the gRPC PHP extension

  1. $ cd grpc/src/php/ext/grpc
  2. $ phpize
  3. $ ./configure
  4. $ make
  5. $ sudo make install

This will compile and install the gRPC PHP extension into thestandard PHP extension directory. You should be able to runthe unit tests with the PHP extension installed.

Update php.ini

After installing the gRPC extension, make sure you add this lineto your php.ini file, (e.g. /etc/php5/cli/php.ini,/etc/php5/apache2/php.ini, or /usr/local/etc/php/5.6/php.ini),depending on where your PHP installation is.

  1. extension=grpc.so

Add the gRPC PHP library as a Composer dependency

You need to add this to your project’s composer.json file.

  1. "require": {
  2. "grpc/grpc": "v1.7.0"
  3. }

To run tests with generated stub code from .proto files, you will alsoneed the composer and protoc binaries. You can find out how to get these below.

Install other prerequisites for both Mac OS X and Linux

  • protoc: protobuf compiler
  • protobuf.so: protobuf runtime library
  • grpc_php_plugin: Generates PHP gRPC service interface out of Protobuf IDL

Install Protobuf compiler

If you don’t have it already, you need to install the protobuf compilerprotoc, version 3.4.0+ (the newer the better) for the current gRPC version.If you installed already, make sure the protobuf version is compatible with thegrpc version you installed. If you build grpc.so from source, you can checkthe version of grpc inside package.xml file.

The compatibility between the grpc and protobuf version is listed as table below:

grpcprotobuf
v1.0.03.0.0(GA)
v1.0.13.0.2
v1.1.03.1.0
v1.2.03.2.0
v1.2.03.2.0
v1.3.43.3.0
v1.3.53.2.0
v1.4.03.3.0
v1.6.03.4.0

If protoc hasn’t been installed, you can download the protoc binaries fromthe protocol buffers GitHub repository.Then unzip this file and Update the environment variable PATH to include the path tothe protoc binary file./protobuf/releases).Then unzip this file and Update the environment variable PATH to include the path tothe protoc binary file.

If you really must compile protoc from source, you can run the followingcommands, but this is risky because there is no easy way to uninstall /upgrade to a newer release.

  1. $ cd grpc/third_party/protobuf
  2. $ ./autogen.sh && ./configure && make
  3. $ sudo make install

Protobuf Runtime library

There are two protobuf runtime libraries to choose from. They are identicalin terms of APIs offered. The C implementation provides better performance,while the native implementation is easier to install. Make sure the installedprotobuf version works with grpc version.

C implementation (for better performance)

  1. $ sudo pecl install protobuf

or specific version

  1. $ sudo pecl install protobuf-3.4.0

After protobuf extension is installed, Update php.ini by adding this lineto your php.ini file, (e.g. /etc/php5/cli/php.ini,/etc/php5/apache2/php.ini, or /usr/local/etc/php/5.6/php.ini),depending on where your PHP installation is.

  1. extension=protobuf.so

PHP implementation (for easier installation)

Add this to your composer.json file:

  1. "require": {
  2. "google/protobuf": "^v3.3.0"
  3. }

PHP Protoc Plugin

You need the gRPC PHP protoc plugin to generate the client stub classes.It can generate server and client code from .proto service definitions.

It should already been compiled when you run make from the root directoryof this repo. The plugin can be found in the bins/opt directory. We areplanning to provide a better way to download and install the pluginin the future.

You can also just build the gRPC PHP protoc plugin by running:

  1. $ git clone -b v1.28.1 https://github.com/grpc/grpc
  2. $ cd grpc
  3. $ git submodule update --init
  4. $ make grpc_php_plugin

Plugin may use the new feature of the new protobuf version, thus please alsomake sure that the protobuf version installed is compatible with the grpc versionyou build this plugin.

Download the example

You’ll need a local copy of the example code to work through this quick start.Download the example code from our GitHub repository (the following commandclones the entire repository, but you just need the examples for this quick startand other tutorials):

Note that currently you can only create clients in PHP for gRPC services -you can find out how to create gRPC servers in our other tutorials,e.g.Node.js.

  1. # Clone the repository to get the example code:
  2. $ git clone -b v1.28.1 https://github.com/grpc/grpc
  3. # Build grpc_php_plugin to generate proto files if not build before
  4. $ cd grpc && git submodule update --init && make grpc_php_plugin
  5. # Navigate to the "hello, world" PHP example:
  6. $ cd examples/php
  7. $ ./greeter_proto_gen.sh
  8. $ composer install

Run a gRPC application

From the examples/node directory:

  • Run the server:
  1. $ npm install
  2. $ cd dynamic_codegen
  3. $ node greeter_server.js
  • From another terminal, from the examples/php directory,run the client:
  1. $ ./run_greeter_client.sh

Congratulations! You’ve just run a client-server application with gRPC.

Update a gRPC service

Now let’s look at how to update the application with an extra method on theserver for the client to call. Our gRPC service is defined using protocolbuffers; you can find out lots more about how to define a service in a .protofile ingRPC Basics: PHP. For now all you need to know is that both theserver and the client “stub” have a SayHello RPC method that takes aHelloRequest parameter from the client and returns a HelloResponse fromthe server, and that this method is defined like this:

  1. // The greeting service definition.
  2. service Greeter {
  3. // Sends a greeting
  4. rpc SayHello (HelloRequest) returns (HelloReply) {}
  5. }
  6. // The request message containing the user's name.
  7. message HelloRequest {
  8. string name = 1;
  9. }
  10. // The response message containing the greetings
  11. message HelloReply {
  12. string message = 1;
  13. }

Let’s update this so that the Greeter service has two methods. Editexamples/protos/helloworld.proto and update it with a new SayHelloAgainmethod, with the same request and response types:

  1. // The greeting service definition.
  2. service Greeter {
  3. // Sends a greeting
  4. rpc SayHello (HelloRequest) returns (HelloReply) {}
  5. // Sends another greeting
  6. rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
  7. }
  8. // The request message containing the user's name.
  9. message HelloRequest {
  10. string name = 1;
  11. }
  12. // The response message containing the greetings
  13. message HelloReply {
  14. string message = 1;
  15. }

Remember to save the file!

Generate gRPC code

Next we need to update the gRPC code used by our application to use the newservice definition. From the grpc root directory:

  1. $ protoc --proto_path=examples/protos \
  2. --php_out=examples/php \
  3. --grpc_out=examples/php \
  4. --plugin=protoc-gen-grpc=bins/opt/grpc_php_plugin \
  5. ./examples/protos/helloworld.proto

or running the helper script under the grpc/example/php directory if you buildgrpc-php-plugin by source:

  1. $ ./greeter_proto_gen.sh

This regenerates the protobuf files, which contain our generated client classes,as well as classes for populating, serializing, and retrieving our request andresponse types.

Update and run the application

We now have new generated client code, but we still need to implement and callthe new method in the human-written parts of our example application.

Update the server

In the same directory, open greeter_server.js. Implement the new method likethis:

  1. function sayHello(call, callback) {
  2. callback(null, {message: 'Hello ' + call.request.name});
  3. }
  4. function sayHelloAgain(call, callback) {
  5. callback(null, {message: 'Hello again, ' + call.request.name});
  6. }
  7. function main() {
  8. var server = new grpc.Server();
  9. server.addProtoService(hello_proto.Greeter.service,
  10. {sayHello: sayHello, sayHelloAgain: sayHelloAgain});
  11. server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure());
  12. server.start();
  13. }
  14. ...

Update the client

In the same directory, open greeter_client.php. Call the new method like this:

  1. $request = new Helloworld\HelloRequest();
  2. $request->setName($name);
  3. list($reply, $status) = $client->SayHello($request)->wait();
  4. $message = $reply->getMessage();
  5. list($reply, $status) = $client->SayHelloAgain($request)->wait();
  6. $message = $reply->getMessage();

Run!

Just like we did before, from the examples/node/dynamic_codegen directory:

  • Run the server:
  1. $ node greeter_server.js
  • From another terminal, from the examples/php directory,run the client:
  1. $ ./run_greeter_client.sh

What’s next