gRPC is a modern, open-source, and high-performance remote procedure call framework. If you want to use PHP client libraries for APIs that support gRPC, you must install gRPC for PHP. This tutorial explains how to install and enable gRPC.
Destination:
Install the gRPC extension for PHP.
Enable the gRPC extension for PHP.
Requirements
PHP 7.0 or later
PECL (unless you are building from source)
Installing PECL
Ubuntu/Debian
sudo apt-get install autoconf zlib1g-dev php-dev php-pear
If using PHP 7.4+, PHP must have been installed with the --with-pear flag.
Centos/RHEL7
sudo rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
sudo yum install php-devel php-pear gcc zlib-devel
MacOS
curl -O https://pear.php.net/go-pear.phar
sudo php -d detect_unicode=0 go-pear.phar
Install extensive gRPC
Using PECL
It compiles and installs the gRPC PHP extension into the standard PHP extension directory.
Note: For users on CentOS/RHEL 6, unfortunately this step will not work. Follow the instructions under the Build from source tab to compile the extension from source.
Build from Source
Follow these instructions to compile the gRPC core library and PHP extensions from source.
Clone the gRPC repository from GitHub.
git clone --depth=1 -b $(curl -L https://grpc.io/release) \
https://github.com/grpc/grpc
Build and install the gRPC C core library.
cd grpc
git submodule update --init
make
sudo make install
It can take a few minutes to download and execute the library.
If you have git version 1.8.4 or greater, you can speed up
the `git submodule update --init` command by adding the `--depth=1`
flag.
Compile the gRPC PHP extension.
cd src/php/ext/grpc
phpize
./configure
make
sudo make install
Enable gRPC extension in php.ini
Linux/MacOS
Add this line anywhere in your php.ini file, for example, /etc/php7/cli/php.ini. You can find this file by running php --ini.
extension=grpc.so
Installing protobuf runtime library
You can choose from two protobuf runtime libraries. The APIs they offer are identical. The C implementation performs better than the (native) PHP implementation, while the native implementation installs easier than the C implementation.
C Implementation
For better performance with gRPC, enable the C-extension protobuf.
Linux/MacOS
Install the protobuf.so extension by using PECL.
sudo pecl install protobuf
Now add this line to your php.ini file, for example, /etc/php5/cli/php.ini.
extension=protobuf.so
PHP Implementation
For easier installation, it requires the google/protobuf package using Composer.
composer require "google/protobuf:^3.17"
Now that you have installed gRPC and the gRPC PHP extension
Good luck and thank you.