Install gRPC for PHP

gRPC is a modern, open-source, high-performance remote procedure call framework. If you want to use PHP client libraries for gRPC-enabled APIs, you must install gRPC for PHP. This tutorial explains how to install and enable gRPC.

Objectives

  • Install the gRPC extension for PHP.
  • Enable the gRPC extension for PHP.

Requirements

  • PHP 5.5 or later, or PHP 7.0 or later
  • PECL (unless you build from source)
  • Composer

Installing PECL

Ubuntu / Debian

For PHP 5

sudo apt-get install autoconf libz-dev php5-dev php-pear

For PHP 7

sudo apt-get install autoconf libz-dev php7.0-dev php-pear

CentOS / RHEL 7

 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 php56w-devel php-pear phpunit gcc zlib-devel

macOS

 curl -O https://pear.php.net/go-pear.phar
 sudo php -d detect_unicode=0 go-pear.phar

Windows

Windows does not require PECL.

Installing Composer

Linux

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

macOS

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

Windows

Follow the official Composer installation instructions for Windows.

Installing the gRPC extension

Using PECL

sudo pecl install grpc

This compiles and installs the gRPC PHP extension into the standard PHP extension directory.

Build from source

Follow these instructions to compile the gRPC core library and PHP extension from source.

  1. Clone the gRPC repository from GitHub.

    git clone -b $(curl -L https://grpc.io/release) https://github.com/grpc/grpc
    
  2. Build and install the gRPC C core library.

    cd grpc
    git submodule update --init
    make
    sudo make install
    
  3. Compile the gRPC PHP extension.

    cd src/php/ext/grpc
    phpize
    ./configure
    make
    sudo make install
    

Windows

Windows users can download the pre-compiled gRPC directly from the PECL website.

Enable the gRPC extension in php.ini

Linux / macOS

Add this line to your php.ini file, for example, /etc/php7/cli/php.ini.

extension=grpc.so

Windows

Add this line to your php.ini file, for example, C:\Program Files\PHP\7.3\php.ini.

extension=php_grpc.dll

Add gRPC as a Composer dependency

Require the grpc/grpc package by using Composer.

composer require "grpc/grpc:^v1.1.0"

Installing the protobuf runtime library

You can choose from two protobuf runtime libraries. The APIs they offer are identical. The C implementation performs better than the PHP (native) implementation, while the native implementation installs easier than the C implementation.

C implementation

For better performance with gRPC, enable the protobuf C-extension.

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

Windows

Download the pre-compiled protobuf extension directly from the PECL website.

Now add this line to your php.ini file, for example, C:\Program Files\PHP\7.3\php.ini.

extension=php_protobuf.dll

PHP implementation

For easier installation, require the google/protobuf package by using Composer.

composer require "google/protobuf:^v3.3.0"

What's next

Now that you've installed gRPC and the gRPC PHP extension, try out gRPC-enabled APIs such as Cloud Spanner.

Apakah halaman ini membantu? Beri tahu kami pendapat Anda:

Kirim masukan tentang...