This page shows a number of ways to create a PHP gRPC client and connect with a gRPC backend service.
For all the following examples, we use a simple gRPC server, written in Node.
$ git clone https://github.com/grpc/grpc-web $ cd grpc-web $ docker-compose build common node-server $ docker run -d -p 9090:9090 --name node-server grpcweb/node-server
All the following commands are assumed to be run from this current directory.
$ cd grpc/examples/php/echo
In order to build a PHP gRPC client, we need to install the grpc
extension first.
$ docker build -t grpc-php/base -f ./base.Dockerfile .
Let's first build a simple CLI gRPC client:
$ docker build -t grpc-php/echo-client -f ./cli.Dockerfile . $ docker run -it --rm --link node-server:node-server grpc-php/echo-client $ php client.php
Now let's see how the gRPC PHP client can run with Apache:
$ docker build -t grpc-php/apache -f ./apache.Dockerfile . $ docker run -it --rm --link node-server:node-server -p 80:80 grpc-php/apache
Open the browser to http://localhost
.
We can also try running PHP-FPM and put Nginx in front of it.
The PHP-FPM part:
$ docker build -t grpc-php/fpm -f ./fpm.Dockerfile . $ docker run -it --rm --link node-server:node-server -p 9000:9000 \ --name fpm grpc-php/fpm
The Nginx part:
$ docker run -it --rm -v $(pwd)/nginx.conf:/etc/nginx/conf.d/default.conf:ro \ --link fpm:fpm -p 80:80 nginx:1.17.4
Open the browser to http://localhost
.