To install the gRPC extension for PHP on Windows, Ubuntu, and macOS, follow these detailed steps.
Install PHP:
C:\php.This PC -> Properties ->
Advanced system settings -> Environment Variables.
Path variable in the System variables
section, select it, and click Edit.New and add the path to your PHP directory, e.g.,
C:\php.Install Composer:
Install the gRPC PHP extension:
cd C:\php
pecl to install the gRPC extension:
pecl install grpc
php.ini file:php.ini file located in your PHP directory.extension=grpc
Install the gRPC PHP library:
composer require "grpc/grpc:^1.38"
Install PHP:
sudo apt update
sudo apt install php php-dev php-pear
Install Composer:
sudo apt install composer
Install the gRPC PHP extension:
sudo apt install autoconf libtool pkg-config
pecl:
sudo pecl install grpc
php.ini file:php.ini file, typically located at
/etc/php/<version>/cli/php.ini and
/etc/php/<version>/apache2/php.ini.extension=grpc.so
sudo systemctl restart apache2
# or
sudo systemctl restart nginx
Install the gRPC PHP library:
composer require "grpc/grpc:^1.38"
Install Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Install PHP:
brew install php
Install Composer:
brew install composer
Install the gRPC PHP extension:
brew install autoconf automake libtool pkg-config
pecl to install the gRPC extension:
pecl install grpc
php.ini file:php.ini file, typically located at
/usr/local/etc/php/<version>/php.ini.extension=grpc.so
brew services restart php
Install the gRPC PHP library:
composer require "grpc/grpc:^1.38"
To verify your installation, you can create a simple gRPC service and client in PHP:
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;
}
Generate PHP gRPC code:
protoc (Protocol Buffer compiler):
# Windows: Download from https://github.com/protocolbuffers/protobuf/releases
# Ubuntu:
sudo apt install protobuf-compiler
# macOS:
brew install protobuf
protoc --php_out=. --grpc_out=. --plugin=protoc-gen-grpc=$(which grpc_php_plugin) helloworld.proto
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";
Run the server:
php GreeterServer.php
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.