• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..17-Jul-2020-

bin/H17-Jul-2020-324149

docker/H17-Jul-2020-619428

ext/grpc/H17-Jul-2020-4,5163,174

lib/Grpc/H17-Jul-2020-1,558969

tests/H17-Jul-2020-18,0948,637

.gitignoreH A D17-Jul-2020231 2622

README.mdH A D17-Jul-20209.7 KiB395290

composer.jsonH A D17-Jul-2020412 2120

phpunit.xmlH A D17-Jul-2020754 2322

README.md

1
2# Overview
3
4This directory contains source code for PHP implementation of gRPC layered on
5shared C library. The same installation guides with more examples and
6tutorials can be seen at [grpc.io](https://grpc.io/docs/quickstart/php.html).
7gRPC PHP installation instructions for Google Cloud Platform is in
8[cloud.google.com](https://cloud.google.com/php/grpc).
9
10## Environment
11
12### Prerequisites
13
14* `php`: version 7.0 or above (PHP 5.x support will be deprecated some time
15in 2020).
16* `pecl`
17* `composer`
18* `phpunit` (optional)
19
20
21## Install the _grpc_ extension
22
23There are two ways to install the `grpc` extension.
24* `pecl`
25* Build from source
26
27### Install from PECL
28
29```sh
30$ [sudo] pecl install grpc
31```
32
33or specific version
34
35```sh
36$ [sudo] pecl install grpc-1.25.0
37```
38
39Note: for users on CentOS/RHEL 6, unfortunately this step won’t work.
40Please follow the instructions below to compile the PECL extension from source.
41
42
43### Install on Windows
44
45You can download the pre-compiled `grpc.dll` extension from the PECL
46[website](https://pecl.php.net/package/grpc).
47
48### Build from source
49
50Clone this repository at the [latest stable release tag](https://github.com/grpc/grpc/releases)
51
52```sh
53$ git clone -b RELEASE_TAG_HERE https://github.com/grpc/grpc
54```
55
56#### Build and install the gRPC C core library
57
58```sh
59$ cd grpc
60$ git submodule update --init
61$ EXTRA_DEFINES=GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK make
62$ [sudo] make install
63```
64
65#### Build and install the `grpc` extension
66
67Compile the `grpc` extension from source
68
69```sh
70$ cd grpc/src/php/ext/grpc
71$ phpize
72$ ./configure
73$ make
74$ [sudo] make install
75```
76
77This will compile and install the `grpc` extension into the
78standard PHP extension directory. You should be able to run
79the [unit tests](#unit-tests), with the `grpc` extension installed.
80
81
82### Update php.ini
83
84After installing the `grpc` extension, make sure you add this line to your
85`php.ini` file, depending on where your PHP installation is, to enable the
86`grpc` extension.
87
88```sh
89extension=grpc.so
90```
91
92## Composer package
93
94In addition to the `grpc` extension, you will need to install the `grpc/grpc`
95composer package as well. Add this to your project's `composer.json` file.
96
97```
98  "require": {
99    "grpc/grpc": "v1.25.0"
100  }
101```
102
103To run tests with generated stub code from `.proto` files, you will also
104need the `composer` and `protoc` binaries. You can find out how to get these
105below.
106
107## Protocol Buffers
108
109gRPC PHP supports
110[protocol buffers](https://developers.google.com/protocol-buffers)
111out-of-the-box. You will need the following things to get started:
112
113* `protoc`: the protobuf compiler binary to generate PHP classes for your
114messages and service definition.
115* `grpc_php_plugin`: a plugin for `protoc` to generate the service stub
116classes.
117* `protobuf.so`: the `protobuf` extension runtime library.
118
119### `protoc` compiler
120
121If you don't have it already, you need to install the protobuf compiler
122`protoc`, version 3.5.0+ (the newer the better) for the current gRPC version.
123If you installed already, make the protobuf version is compatible to the
124grpc version you installed. If you build grpc.so from the souce, you can check
125the version of grpc inside package.xml file.
126
127The compatibility between the grpc and protobuf version is listed as table
128below:
129
130grpc | protobuf
131--- | ---
132v1.0.0 | 3.0.0(GA)
133v1.0.1 | 3.0.2
134v1.1.0 | 3.1.0
135v1.2.0 | 3.2.0
136v1.2.0 | 3.2.0
137v1.3.4 | 3.3.0
138v1.3.5 | 3.2.0
139v1.4.0 | 3.3.0
140v1.6.0 | 3.4.0
141v1.8.0 | 3.5.0
142v1.12.0 | 3.5.2
143v1.13.1 | 3.5.2
144v1.14.2 | 3.5.2
145v1.15.1 | 3.6.1
146v1.16.1 | 3.6.1
147v1.17.2 | 3.6.1
148v1.18.0 | 3.6.1
149v1.19.1 | 3.6.1
150v1.20.1 | 3.7.0
151v1.21.3 | 3.7.0
152v1.22.0 | 3.8.0
153v1.23.1 | 3.8.0
154v1.24.0 | 3.8.0
155v1.25.0 | 3.8.0
156
157If `protoc` hasn't been installed, you can download the `protoc` binary from
158the protocol buffers
159[Github repository](https://github.com/google/protobuf/releases).
160Then unzip this file and update the environment variable `PATH` to include the
161path to the protoc binary file.
162
163If you really must compile `protoc` from source, you can run the following
164commands, but this is risky because there is no easy way to uninstall /
165upgrade to a newer release.
166
167```sh
168$ cd grpc/third_party/protobuf
169$ ./autogen.sh && ./configure && make
170$ [sudo] make install
171```
172
173### `grpc_php_plugin` protoc plugin
174
175You need the `grpc_php_plugin` to generate the PHP client stub classes. This
176plugin works with the main `protoc` binary to generate classes that you can
177import into your project.
178
179It should already been compiled when you run `make` from the root directory
180of this repo. The plugin can be found in the `bins/opt` directory. We are
181planning to provide a better way to download and install the plugin
182in the future.
183
184You can also just build the `grpc_php_plugin` by running:
185
186```sh
187$ git clone -b RELEASE_TAG_HERE https://github.com/grpc/grpc
188$ cd grpc
189$ git submodule update --init
190$ make grpc_php_plugin
191```
192
193Plugin may use the new feature of the new protobuf version, thus please also
194make sure that the protobuf version installed is compatible with the grpc
195version you build this plugin.
196
197### `protobuf` runtime library
198
199There are two `protobuf` runtime libraries to choose from. They are identical
200in terms of APIs offered. The C implementation provides better performance,
201while the native implementation is easier to install.
202
203#### C implementation (for better performance)
204
205Install the `protobuf` extension from PECL:
206
207``` sh
208$ [sudo] pecl install protobuf
209```
210or specific version
211
212``` sh
213$ [sudo] pecl install protobuf-3.8.0
214```
215
216And add this to your `php.ini` file:
217
218```sh
219extension=protobuf.so
220```
221
222#### PHP implementation (for easier installation)
223
224Or require the `google/protobuf` composer package. Add this to your
225`composer.json` file:
226
227```
228  "require": {
229    "google/protobuf": "^v3.8.0"
230  }
231```
232
233### Generate PHP classes from your service definition
234
235With all the above done, now you can define your message and service defintion
236in a `.proto` file and generate the corresponding PHP classes, which you can
237import into your project, with a command similar to the following:
238
239```
240$ protoc -I=. echo.proto --php_out=. --grpc_out=. \
241--plugin=protoc-gen-grpc=<path to grpc_php_plugin>
242```
243
244## Unit Tests
245
246You will need the source code to run tests
247
248```sh
249$ git clone -b RELEASE_TAG_HERE https://github.com/grpc/grpc
250$ cd grpc
251$ git submodule update --init
252```
253
254Run unit tests
255
256```sh
257$ cd grpc/src/php
258$ ./bin/run_tests.sh
259```
260
261## Generated Code Tests
262
263This section specifies the prerequisites for running the generated code tests,
264as well as how to run the tests themselves.
265
266### Composer
267
268Install the runtime dependencies via `composer install`.
269
270```sh
271$ cd grpc/src/php
272$ composer install
273```
274
275
276### Client Stub
277
278The generate client stub classes have already been generated from `.proto` files
279by the `./bin/generate_proto_php.sh` script.
280
281### Run test server
282
283Run a local server serving the math services. Please see [Node][] for how to
284run an example server.
285
286```sh
287$ cd grpc/src/php/tests/generated_code
288$ npm install
289$ node math_server.js
290```
291
292### Run test client
293
294Run the generated code tests
295
296```sh
297$ cd grpc/src/php
298$ ./bin/run_gen_code_test.sh
299```
300
301## Apache, PHP-FPM and Nginx
302
303For more information on how you can run the `grpc` library with Apache,
304PHP-FPM and Nginx, you can check out
305[this guide](https://github.com/grpc/grpc/tree/master/examples/php/echo).
306There you will find a series of Docker images where you can quickly run an
307end-to-end example.
308
309## Misc Config Options
310
311### SSL credentials
312
313Here's how you can specify SSL credentials when creating your PHP client:
314
315```
316$client = new Helloworld\GreeterClient('localhost:50051', [
317    'credentials' => Grpc\ChannelCredentials::createSsl(
318        file_get_contents('<path to certificate>'))
319]);
320```
321
322### pcntl_fork() support
323
324To make sure the `grpc` extension works with `pcntl_fork()` and related
325functions, add the following lines to your `php.ini` file:
326
327```
328grpc.enable_fork_support = 1
329grpc.poll_strategy = epoll1
330```
331
332### Tracing and Logging
333
334To turn on gRPC tracing, add the following lines to your `php.ini` file. For
335all possible values of the `grpc.grpc.trace` option, please check
336[this doc](https://github.com/grpc/grpc/blob/master/doc/environment_variables.md).
337
338```
339grpc.grpc_verbosity=debug
340grpc.grpc_trace=all,-timer_check
341grpc.log_filename=/var/log/grpc.log
342```
343
344### User agent string
345
346You can customize the user agent string for your gRPC PHP client by specifying
347this `grpc.primary_user_agent` option when constructing your PHP client:
348
349```
350$client = new Helloworld\GreeterClient('localhost:50051', [
351    'credentials' => Grpc\ChannelCredentials::createInsecure(),
352    'grpc.primary_user_agent' => 'my-user-agent-identifier',
353]);
354```
355
356### Maximum message size
357
358To change the default maximum message size, specify this
359`grpc.max_receive_message_length` option when constructing your PHP client:
360
361```
362$client = new Helloworld\GreeterClient('localhost:50051', [
363    'credentials' => Grpc\ChannelCredentials::createInsecure(),
364    'grpc.max_receive_message_length' => 8*1024*1024,
365]);
366```
367
368### Compression
369
370You can customize the compression behavior on the client side, by specifying the following options when constructing your PHP client.
371
372```
373Possible values for grpc.default_compression_algorithm:
3740 - No compression
3751 - Compress with DEFLATE algorithm
3762 - Compress with GZIP algorithm
3773 - Stream compression with GZIP algorithm
378```
379```
380Possible values for grpc.default_compression_level:
3810 - None
3821 - Low level
3832 - Medium level
3843 - High level
385```
386Here's an example on how you can put them all together:
387```
388$client = new Helloworld\GreeterClient('localhost:50051', [
389        'credentials' => Grpc\ChannelCredentials::createInsecure(),
390        'grpc.default_compression_algorithm' => 2,
391        'grpc.default_compression_level' => 2,
392]);
393
394[Node]:https://github.com/grpc/grpc/tree/master/src/node/examples
395