| 1 |
|
| 2 |
# gRPC PHP End-to-End Examples |
| 3 |
|
| 4 |
This page shows a number of ways to create a PHP gRPC client and connect with |
| 5 |
a gRPC backend service. |
| 6 |
|
| 7 |
|
| 8 |
## Run the Server |
| 9 |
|
| 10 |
For all the following examples, we use a simple gRPC server, written in Node. |
| 11 |
|
| 12 |
```sh |
| 13 |
$ git clone https://github.com/grpc/grpc-web |
| 14 |
$ cd grpc-web |
| 15 |
$ docker-compose build prereqs node-server |
| 16 |
$ docker run -d -p 9090:9090 --name node-server grpcweb/node-server |
| 17 |
``` |
| 18 |
|
| 19 |
|
| 20 |
## Install the gRPC PECL extension |
| 21 |
|
| 22 |
All the following commands are assumed to be run from this current directory. |
| 23 |
|
| 24 |
```sh |
| 25 |
$ cd grpc/examples/php/echo |
| 26 |
``` |
| 27 |
|
| 28 |
|
| 29 |
In order to build a PHP gRPC client, we need to install the `grpc` extension |
| 30 |
first. |
| 31 |
|
| 32 |
```sh |
| 33 |
$ docker build -t grpc-php/base -f ./base.Dockerfile . |
| 34 |
``` |
| 35 |
|
| 36 |
|
| 37 |
## CLI |
| 38 |
|
| 39 |
|
| 40 |
Let's first build a simple CLI gRPC client: |
| 41 |
|
| 42 |
```sh |
| 43 |
$ docker build -t grpc-php/echo-client -f ./cli.Dockerfile . |
| 44 |
$ docker run -it --rm --link node-server:node-server grpc-php/echo-client |
| 45 |
$ php client.php |
| 46 |
``` |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
## Apache |
| 51 |
|
| 52 |
|
| 53 |
Now let's see how the gRPC PHP client can run with Apache: |
| 54 |
|
| 55 |
```sh |
| 56 |
$ docker build -t grpc-php/apache -f ./apache.Dockerfile . |
| 57 |
$ docker run -it --rm --link node-server:node-server -p 80:80 grpc-php/apache |
| 58 |
``` |
| 59 |
|
| 60 |
Open the browser to `http://localhost`. |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
## Nginx + FPM |
| 65 |
|
| 66 |
|
| 67 |
We can also try running PHP-FPM and put Nginx in front of it. |
| 68 |
|
| 69 |
|
| 70 |
The PHP-FPM part: |
| 71 |
|
| 72 |
```sh |
| 73 |
$ docker build -t grpc-php/fpm -f ./fpm.Dockerfile . |
| 74 |
$ docker run -it --rm --link node-server:node-server -p 9000:9000 \ |
| 75 |
--name fpm grpc-php/fpm |
| 76 |
``` |
| 77 |
|
| 78 |
The Nginx part: |
| 79 |
|
| 80 |
```sh |
| 81 |
$ docker run -it --rm -v $(pwd)/nginx.conf:/etc/nginx/conf.d/default.conf:ro \ |
| 82 |
--link fpm:fpm -p 80:80 nginx:1.17.4 |
| 83 |
``` |
| 84 |
|
| 85 |
|
| 86 |
Open the browser to `http://localhost`. |