install the gRPC extension

To install the gRPC extension for PHP on Windows, Ubuntu, and macOS, follow these detailed steps.

Windows

  1. Install PHP:

    • Download the latest PHP from php.net.
    • Extract the PHP package to a directory, e.g., C:\php.
    • Add the PHP directory to your system PATH:
      • Right-click on This PC -> Properties -> Advanced system settings -> Environment Variables.
      • Find the Path variable in the System variables section, select it, and click Edit.
      • Click New and add the path to your PHP directory, e.g., C:\php.
  2. Install Composer:

    • Download Composer from getcomposer.org.
    • Follow the installation instructions to install Composer globally.
  3. Install the gRPC PHP extension:

    • Open a command prompt and navigate to your PHP directory:
      cd C:\php
      
    • Use pecl to install the gRPC extension:
      pecl install grpc
      
    • Add the extension to your php.ini file:
      • Open the php.ini file located in your PHP directory.
      • Add the following line:
        extension=grpc
        
    • Restart your web server if you're using one (e.g., Apache or Nginx).
  4. Install the gRPC PHP library:

    • In your project directory, run:
      composer require "grpc/grpc:^1.38"
      

Ubuntu

  1. Install PHP:

    sudo apt update
    sudo apt install php php-dev php-pear
    
  2. Install Composer:

    sudo apt install composer
    
  3. Install the gRPC PHP extension:

    • Install prerequisites:
      sudo apt install autoconf libtool pkg-config
      
    • Install the gRPC extension using pecl:
      sudo pecl install grpc
      
    • Add the extension to your php.ini file:
      • Open the php.ini file, typically located at /etc/php/<version>/cli/php.ini and /etc/php/<version>/apache2/php.ini.
      • Add the following line:
        extension=grpc.so
        
    • Restart your web server if you're using one:
      sudo systemctl restart apache2
      # or
      sudo systemctl restart nginx
      
  4. Install the gRPC PHP library:

    • In your project directory, run:
      composer require "grpc/grpc:^1.38"
      

macOS

  1. Install Homebrew:

    • Open Terminal and run:
      /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
      
  2. Install PHP:

    brew install php
    
  3. Install Composer:

    brew install composer
    
  4. Install the gRPC PHP extension:

    • Install prerequisites:
      brew install autoconf automake libtool pkg-config
      
    • Use pecl to install the gRPC extension:
      pecl install grpc
      
    • Add the extension to your php.ini file:
      • Open the php.ini file, typically located at /usr/local/etc/php/<version>/php.ini.
      • Add the following line:
        extension=grpc.so
        
    • Restart your web server if you're using one:
      brew services restart php
      
  5. Install the gRPC PHP library:

    • In your project directory, run:
      composer require "grpc/grpc:^1.38"
      

Verification

To verify your installation, you can create a simple gRPC service and client in PHP:

  1. Create a .proto file (e.g., helloworld.proto):

    syntax = "proto3";
    
    package helloworld;
    
    service Greeter {
      rpc SayHello (HelloRequest) returns (HelloReply) {}
    }
    
    message HelloRequest {
      string name = 1;
    }
    
    message HelloReply {
      string message = 1;
    }
    
  2. Generate PHP gRPC code:

    • Install protoc (Protocol Buffer compiler):
      # Windows: Download from https://github.com/protocolbuffers/protobuf/releases
      # Ubuntu:
      sudo apt install protobuf-compiler
      # macOS:
      brew install protobuf
      
    • Generate PHP files:
      protoc --php_out=. --grpc_out=. --plugin=protoc-gen-grpc=$(which grpc_php_plugin) helloworld.proto
      
  3. Create a server and client:

    • Server (GreeterServer.php):

      <?php
      require 'vendor/autoload.php';
      require 'helloworld/GPBMetadata/Helloworld.php';
      require 'helloworld/Helloworld/HelloRequest.php';
      require 'helloworld/Helloworld/HelloReply.php';
      require 'helloworld/Helloworld/Greeter.php';
      require 'helloworld/Helloworld/GreeterClient.php';
      
      class GreeterImpl extends \Helloworld\GreeterStub {
          public function SayHello(\Helloworld\HelloRequest $request, $context) {
              $reply = new \Helloworld\HelloReply();
              $reply->setMessage('Hello, ' . $request->getName());
              return $reply;
          }
      }
      
      $server = new \Grpc\RpcServer();
      $server->addHttp2Port('0.0.0.0:50051');
      $server->handle(new GreeterImpl());
      
      echo "Server running on port 50051\n";
      $server->run();
      
    • Client (GreeterClient.php):

      <?php
      require 'vendor/autoload.php';
      require 'helloworld/GPBMetadata/Helloworld.php';
      require 'helloworld/Helloworld/HelloRequest.php';
      require 'helloworld/Helloworld/HelloReply.php';
      require 'helloworld/Helloworld/Greeter.php';
      require 'helloworld/Helloworld/GreeterClient.php';
      
      $client = new \Helloworld\GreeterClient('localhost:50051', [
          'credentials' => \Grpc\ChannelCredentials::createInsecure(),
      ]);
      
      $request = new \Helloworld\HelloRequest();
      $request->setName('world');
      
      list($response, $status) = $client->SayHello($request)->wait();
      echo $response->getMessage() . "\n";
      
  4. Run the server:

    php GreeterServer.php
    
  5. Run the client:

    php GreeterClient.php
    

You should see the output from the client indicating a successful communication with the gRPC server. This confirms that the gRPC extension for PHP is properly installed and working on your system.