4 This directory contains source code for PHP implementation of gRPC layered on
5 shared C library. The same installation guides with more examples and
6 tutorials can be seen at [grpc.io](https://grpc.io/docs/languages/php/quickstart).
7 gRPC PHP installation instructions for Google Cloud Platform is in
8 [cloud.google.com](https://cloud.google.com/php/grpc).
14 * `php`: version 7.0 or above (PHP 5.x support is deprecated from Sep 2020).
17 * `phpunit` (optional)
20 ## Install the _grpc_ extension
22 There are two ways to install the `grpc` extension.
29 $ [sudo] pecl install grpc
35 $ [sudo] pecl install grpc-1.30.0
38 Please make sure your `gcc` version satisfies the minimum requirement as
39 specified [here](https://grpc.io/docs/languages/#official-support).
42 ### Install on Windows
44 You can download the pre-compiled `grpc.dll` extension from the PECL
45 [website](https://pecl.php.net/package/grpc).
49 Clone this repository at the [latest stable release tag](https://github.com/grpc/grpc/releases).
52 $ git clone -b RELEASE_TAG_HERE https://github.com/grpc/grpc
56 #### Build the gRPC C core library
59 $ git submodule update --init
60 $ EXTRA_DEFINES=GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK make
63 #### Build and install the `grpc` extension
65 Compile the `grpc` extension from source
71 $ GRPC_LIB_SUBDIR=libs/opt ./configure --enable-grpc="${grpc_root}"
76 This will compile and install the `grpc` extension into the
77 standard PHP extension directory. You should be able to run
78 the [unit tests](#unit-tests), with the `grpc` extension installed.
83 After installing the `grpc` extension, make sure you add this line to your
84 `php.ini` file, depending on where your PHP installation is, to enable the
93 In addition to the `grpc` extension, you will need to install the `grpc/grpc`
94 composer package as well. Add this to your project's `composer.json` file.
98 "grpc/grpc": "~1.30.0"
102 To run tests with generated stub code from `.proto` files, you will also
103 need the `composer` and `protoc` binaries. You can find out how to get these
109 [protocol buffers](https://developers.google.com/protocol-buffers)
110 out-of-the-box. You will need the following things to get started:
112 * `protoc`: the protobuf compiler binary to generate PHP classes for your
113 messages and service definition.
114 * `grpc_php_plugin`: a plugin for `protoc` to generate the service stub
116 * `protobuf.so`: the `protobuf` extension runtime library.
118 ### `protoc` compiler
120 If you don't have it already, you need to install the protobuf compiler
121 `protoc`, version 3.5.0+ (the newer the better) for the current gRPC version.
122 If you installed already, make the protobuf version is compatible to the
123 grpc version you installed. If you build grpc.so from the souce, you can check
124 the version of grpc inside package.xml file.
126 The compatibility between the grpc and protobuf version is listed as table
129 grpc | protobuf | grpc | protobuf | grpc | protobuf
130 --- | --- | --- | --- | --- | ---
131 v1.0.0 | 3.0.0(GA) | v1.12.0 | 3.5.2 | v1.22.0 | 3.8.0
132 v1.0.1 | 3.0.2 | v1.13.1 | 3.5.2 | v1.23.1 | 3.8.0
133 v1.1.0 | 3.1.0 | v1.14.2 | 3.5.2 | v1.24.0 | 3.8.0
134 v1.2.0 | 3.2.0 | v1.15.1 | 3.6.1 | v1.25.0 | 3.8.0
135 v1.2.0 | 3.2.0 | v1.16.1 | 3.6.1 | v1.26.0 | 3.8.0
136 v1.3.4 | 3.3.0 | v1.17.2 | 3.6.1 | v1.27.3 | 3.11.2
137 v1.3.5 | 3.2.0 | v1.18.0 | 3.6.1 | v1.28.1 | 3.11.2
138 v1.4.0 | 3.3.0 | v1.19.1 | 3.6.1 | v1.29.0 | 3.11.2
139 v1.6.0 | 3.4.0 | v1.20.1 | 3.7.0 | v1.30.0 | 3.12.2
140 v1.8.0 | 3.5.0 | v1.21.3 | 3.7.0
142 If `protoc` hasn't been installed, you can download the `protoc` binary from
144 [Github repository](https://github.com/google/protobuf/releases).
145 Then unzip this file and update the environment variable `PATH` to include the
146 path to the protoc binary file.
148 If you really must compile `protoc` from source, you can run the following
149 commands, but this is risky because there is no easy way to uninstall /
150 upgrade to a newer release.
153 $ cd grpc/third_party/protobuf
154 $ ./autogen.sh && ./configure && make
155 $ [sudo] make install
158 ### `grpc_php_plugin` protoc plugin
160 You need the `grpc_php_plugin` to generate the PHP client stub classes. This
161 plugin works with the main `protoc` binary to generate classes that you can
162 import into your project.
164 It should already been compiled when you run `make` from the root directory
165 of this repo. The plugin can be found in the `bins/opt` directory. We are
166 planning to provide a better way to download and install the plugin
169 You can also just build the `grpc_php_plugin` by running:
172 $ git clone -b RELEASE_TAG_HERE https://github.com/grpc/grpc
174 $ git submodule update --init
175 $ make grpc_php_plugin
178 Alternatively, you can also build the `grpc_php_plugin` with `bazel` now:
181 $ bazel build @com_google_protobuf//:protoc
182 $ bazel build src/compiler:grpc_php_plugin
185 The `protoc` binary will be found in
186 `bazel-bin/external/com_google_protobuf/protoc`.
187 The `grpc_php_plugin` binary will be found in
188 `bazel-bin/src/compiler/grpc_php_plugin`.
190 Plugin may use the new feature of the new protobuf version, thus please also
191 make sure that the protobuf version installed is compatible with the grpc
192 version you build this plugin.
194 ### `protobuf` runtime library
196 There are two `protobuf` runtime libraries to choose from. They are identical
197 in terms of APIs offered. The C implementation provides better performance,
198 while the native implementation is easier to install.
200 #### C implementation (for better performance)
202 Install the `protobuf` extension from PECL:
205 $ [sudo] pecl install protobuf
210 $ [sudo] pecl install protobuf-3.12.2
213 And add this to your `php.ini` file:
216 extension=protobuf.so
219 #### PHP implementation (for easier installation)
221 Or require the `google/protobuf` composer package. Add this to your
222 `composer.json` file:
226 "google/protobuf": "~v3.12.2"
230 ### Generate PHP classes from your service definition
232 With all the above done, now you can define your message and service defintion
233 in a `.proto` file and generate the corresponding PHP classes, which you can
234 import into your project, with a command similar to the following:
237 $ protoc -I=. echo.proto --php_out=. --grpc_out=. \
238 --plugin=protoc-gen-grpc=<path to grpc_php_plugin>
243 You will need the source code to run tests
246 $ git clone -b RELEASE_TAG_HERE https://github.com/grpc/grpc
248 $ git submodule update --init
258 ## Generated Code Tests
260 This section specifies the prerequisites for running the generated code tests,
261 as well as how to run the tests themselves.
265 Install the runtime dependencies via `composer install`.
275 The generate client stub classes have already been generated from `.proto` files
276 by the `./bin/generate_proto_php.sh` script.
280 Run a local server serving the `Math`
281 [service](https://github.com/grpc/grpc/blob/master/src/proto/math/math.proto#L42).
284 $ cd grpc/src/php/tests/generated_code
286 $ node math_server.js
291 Run the generated code tests
295 $ ./bin/run_gen_code_test.sh
298 ## Apache, PHP-FPM and Nginx
300 For more information on how you can run the `grpc` library with Apache,
301 PHP-FPM and Nginx, you can check out
302 [this guide](https://github.com/grpc/grpc/tree/master/examples/php/echo).
303 There you will find a series of Docker images where you can quickly run an
306 ## Misc Config Options
310 Here's how you can specify SSL credentials when creating your PHP client:
313 $client = new Helloworld\GreeterClient('localhost:50051', [
314 'credentials' => Grpc\ChannelCredentials::createSsl(
315 file_get_contents('<path to certificate>'))
319 ### pcntl_fork() support
321 To make sure the `grpc` extension works with `pcntl_fork()` and related
322 functions, add the following lines to your `php.ini` file:
325 grpc.enable_fork_support = 1
326 grpc.poll_strategy = epoll1
329 ### Tracing and Logging
331 To turn on gRPC tracing, add the following lines to your `php.ini` file. For
332 all possible values of the `grpc.grpc.trace` option, please check
333 [this doc](https://github.com/grpc/grpc/blob/master/doc/environment_variables.md).
336 grpc.grpc_verbosity=debug
337 grpc.grpc_trace=all,-polling,-polling_api,-pollable_refcount,-timer,-timer_check
338 grpc.log_filename=/var/log/grpc.log
341 > Make sure the log file above is writable, by doing the following:
343 > $ sudo touch /var/log/grpc.log
344 > $ sudo chmod 666 /var/log/grpc.log
346 > Note: The log file does grow pretty quickly depending on how much logs are
347 > being printed out. Make sure you have other mechanisms (perhaps another
348 > cronjob) to zero out the log file from time to time,
349 > e.g. `cp /dev/null /var/log/grpc.log`, or turn these off when logs or tracing
350 > are not necessary for debugging purposes.
352 ### User agent string
354 You can customize the user agent string for your gRPC PHP client by specifying
355 this `grpc.primary_user_agent` option when constructing your PHP client:
358 $client = new Helloworld\GreeterClient('localhost:50051', [
359 'credentials' => Grpc\ChannelCredentials::createInsecure(),
360 'grpc.primary_user_agent' => 'my-user-agent-identifier',
364 ### Maximum message size
366 To change the default maximum message size, specify this
367 `grpc.max_receive_message_length` option when constructing your PHP client:
370 $client = new Helloworld\GreeterClient('localhost:50051', [
371 'credentials' => Grpc\ChannelCredentials::createInsecure(),
372 'grpc.max_receive_message_length' => 8*1024*1024,
378 You can customize the compression behavior on the client side, by specifying the following options when constructing your PHP client.
381 $client = new Helloworld\GreeterClient('localhost:50051', [
382 'credentials' => Grpc\ChannelCredentials::createInsecure(),
383 'grpc.default_compression_algorithm' => 2,
384 'grpc.default_compression_level' => 2,
388 Possible values for `grpc.default_compression_algorithm`:
392 1: Compress with DEFLATE algorithm
393 2: Compress with GZIP algorithm
394 3: Stream compression with GZIP algorithm
397 Possible values for `grpc.default_compression_level`: