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

Before you begin

Prerequisites

  • php: version 5.5 or higher, 7.0 or higher
  • pecl: version 1.9 or higher
  • composer

Debian/Ubuntu

$ [sudo] apt-get install php5 php5-dev php-pear zlib1g-dev

OR

$ [sudo] apt-get install php7.0 php7.0-dev zlib1g-dev

Mac OS

$ brew install autoconf

$ curl -O http://pear.php.net/go-pear.phar
$ [sudo] php -d detect_unicode=0 go-pear.phar

Composer

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

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.

Install gRPC

Install gRPC:

$ [sudo] pecl install grpc

Install Protobuf-PHP

You will need to install the protocol buffer compiler protoc and the special plugin for generating server and client code from .proto service definitions. For the first part of our quickstart example, we’ve already generated the server and client stubs from helloworld.proto, but you’ll need the tools for the rest of our quickstart, as well as later tutorials and your own projects.

To install protoc:

The simplest way to do this is to download pre-compiled binaries for your platform(protoc-<version>-<platform>.zip) from here: https://github.com/google/protobuf/releases

  • Unzip this file.
  • Update the environment variable PATH to include the path to the protoc binary file.

To install Protobuf-PHP, run:

$ git clone https://github.com/stanley-cheung/Protobuf-PHP
$ cd Protobuf-PHP
$ rake pear:package version=1.0
$ [sudo] pear install Protobuf-1.0.tgz

If you did not have ‘rake’ or ‘ronn’ installed, please install them using this command: sh $ gem install rake ronn

Download the example

You’ll need a local copy of the example code to work through this quickstart. Download the example code from our Github repository (the following command clones the entire repository, but you just need the examples for this quickstart and other tutorials):

$ # Clone the repository to get the example code:
$ git clone https://github.com/grpc/grpc
$ # Navigate to the "hello, world" PHP example:
$ cd grpc/examples/php
$ composer install

Run a gRPC application

From the examples/node/dynamic_codegen directory:

  1. Run the server

    $ npm install
    $ node greeter_server.js
    

In another terminal, from the examples/php directory:

  1. Run the client

    $ ./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 the server for the client to call. Our gRPC service is defined using protocol buffers; you can find out lots more about how to define a service in a .proto file in gRPC Basics: PHP. For now all you need to know is that both the server and the client “stub” have a SayHello RPC method that takes a HelloRequest parameter from the client and returns a HelloResponse from the server, and that this method is defined like this:

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

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

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
  // Sends another greeting
  rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

(Don’t forget to save the file!)

Generate gRPC code

Next we need to update the gRPC code used by our application to use the new service definition. From the examples/php directory:

$ protoc-gen-php -i . -o . helloworld.proto

This regenerates helloworld.php, which contains our generated client classes, as well as classes for populating, serializing, and retrieving our request and response types.

Update and run the application

We now have new generated client code, but we still need to implement and call the 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 like this:

function sayHello(call, callback) {
  callback(null, {message: 'Hello ' + call.request.name});
}

function sayHelloAgain(call, callback) {
  callback(null, {message: 'Hello again, ' + call.request.name});
}
...

Update the client

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

    $request = new helloworld\HelloRequest();
    $request->setName($name);
    list($reply, $status) = $client->SayHello($request)->wait();
    $message = $reply->getMessage();
    list($reply, $status) = $client->SayHelloAgain($request)->wait();
    $message = $reply->getMessage();

Run!

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

  1. Run the server

    $ node greeter_server.js
    

In another terminal, from the examples/php directory:

  1. Run the client

    $ ./run_greeter_client.sh
    

What’s next