The following instructions install PEAR and PECL on Mac OS X under /usr/local/. PECL is bundled with PEAR. So this is as simple as installing PEAR on Mac OS X.
PEAR is PHP's Package Repository and makes it easy to download and install PHP tools like PHPUnit and XDebug. I specifically recommend these two for every PHP developer.
Download PEAR
Configure and Install PEAR
Install PHP and PECL on Ubuntu/Debian: For PHP5: $ sudo apt-get install php5 php5-dev php-pear phpunit For PHP7. Install PHP and PECL on Mac. Install Pecl/Pear on Mac OSX August 5, 2020 August 5, 2020 by Johnnyparky Use curl as shown below to download the go-pear.phar file or just download the go-pear.phar file via your browser.
You should now be at a prompt to configure PEAR.
Type 1 and press return.
Enter:
Type 4 and press return.
Enter:
Press return
Verify PEAR
You should be able to type:
Eventually, if you use any extensions or applications from PEAR, you may need to update PHP's include path.
Find this interesting? Let's continue the conversation on Twitter.
This guide gets you started with gRPC in PHP with a simpleworking example.
Prerequisites
php
5.5 or above, 7.0 or abovepecl
composer
phpunit
(optional)
Install PHP and PECL on Ubuntu/Debian:
For PHP5:
For PHP7:
or
Install PHP and PECL on CentOS/RHEL 7:
Install PHP and PECL on Mac:
Install Composer (Linux or Mac):
Install PHPUnit (Linux or Mac):
Install the gRPC PHP extension
There are two ways to install gRPC PHP extension.
pecl
build from source
Using PECL
or specific version
Note: for users on CentOS/RHEL 6, unfortunately this step won’t work. Please follow the instructions below to compile the PECL extension from source.
Install on Windows
You can download the pre-compiled gRPC extension from the PECLwebsite
Build from Source with gRPC C core library
Clone this repository
Build and install the gRPC C core library
Build and install gRPC PHP extension
Compile the gRPC PHP extension
This will compile and install the gRPC PHP extension into the standard PHP extension directory. You should be able to run the unit tests, with the PHP extension installed.
Update php.ini
After installing the gRPC extension, make sure you add this line to your php.ini
file, (e.g. /etc/php5/cli/php.ini
, /etc/php5/apache2/php.ini
, or /usr/local/etc/php/5.6/php.ini
), depending on where your PHP installation is.
Add the gRPC PHP library as a Composer dependency
You need to add this to your project’s composer.json
file.
To run tests with generated stub code from .proto
files, you will also need the composer
and protoc
binaries. You can find out how to get these below.
Install other prerequisites for both Mac OS X and Linux
protoc: protobuf compiler
protobuf.so: protobuf runtime library
grpc_php_plugin: Generates PHP gRPC service interface out of Protobuf IDL
Install Protobuf compiler
If you don’t have it already, you need to install the protobuf compilerprotoc
, version 3.4.0+ (the newer the better) for the current gRPC version.If you installed already, make sure the protobuf version is compatible with the grpc version you installed. If you build grpc.so from source, you can checkthe version of grpc inside package.xml file.
The compatibility between the grpc and protobuf version is listed as table below:
grpc | protobuf |
---|---|
v1.0.0 | 3.0.0(GA) |
v1.0.1 | 3.0.2 |
v1.1.0 | 3.1.0 |
v1.2.0 | 3.2.0 |
v1.2.0 | 3.2.0 |
v1.3.4 | 3.3.0 |
v1.3.5 | 3.2.0 |
v1.4.0 | 3.3.0 |
v1.6.0 | 3.4.0 |
If protoc
hasn’t been installed, you can download the protoc
binaries fromthe protocol buffers GitHub repository.Then unzip this file and Update the environment variable PATH
to include the path to the protoc binary file./protobuf/releases).Then unzip this file and Update the environment variable PATH
to include the path to the protoc binary file.
If you really must compile protoc
from source, you can run the followingcommands, but this is risky because there is no easy way to uninstall /upgrade to a newer release.
Protobuf Runtime library
There are two protobuf runtime libraries to choose from. They are identicalin terms of APIs offered. The C implementation provides better performance, while the native implementation is easier to install. Make sure the installed protobuf version works with grpc version.
1. C implementation (for better performance)
or specific version
After protobuf extension is installed, Update php.ini by adding this line to your php.ini
file, (e.g. /etc/php5/cli/php.ini
, /etc/php5/apache2/php.ini
, or /usr/local/etc/php/5.6/php.ini
), depending on where your PHP installation is.
2. PHP implementation (for easier installation)
Add this to your composer.json
file:
PHP Protoc Plugin
You need the gRPC PHP protoc plugin to generate the client stub classes.It can generate server and client code from .proto service definitions.
It should already been compiled when you run make
from the root directoryof this repo. The plugin can be found in the bins/opt
directory. We areplanning to provide a better way to download and install the pluginin the future.
You can also just build the gRPC PHP protoc plugin by running:
Plugin may use the new feature of the new protobuf version, thus please alsomake sure that the protobuf version installed is compatible with the grpc version you build this plugin.
Download the example
You’ll need a local copy of the example code to work through this quickstart.Download the example code from our GitHub repository (the following commandclones the entire repository, but you just need the examples for this quickstartand other tutorials):
Note that currently you can only create clients in PHP for gRPC services -you can find out how to create gRPC servers in our other tutorials,e.g. Node.js.
Run a gRPC application
From the examples/node
directory:
Run the server
In another terminal, from the examples/php
directory:
Run the client
Congratulations! You’ve just run a client-server application with gRPC.
Update a gRPC service
Now let’s look at how to update the application with an extra method on theserver for the client to call. Our gRPC service is defined using protocolbuffers; you can find out lots more about how to define a service in a .proto
file in gRPC Basics: PHP. For now all you need to know is that both theserver and the client “stub” have a SayHello
RPC method that takes aHelloRequest
parameter from the client and returns a HelloResponse
fromthe server, and that this method is defined like this:
Let’s update this so that the Greeter
service has two methods. Editexamples/protos/helloworld.proto
and update it with a new SayHelloAgain
method, with the same request and response types:
(Don’t forget to save the file!)
Generate gRPC code
Next we need to update the gRPC code used by our application to use the newservice definition. From the grpc
root directory:
or running the helper script under the grpc/example/php
directory if you buildgrpc-php-plugin by source:
This regenerates the protobuf files, which contain our generated client classes,as well as classes for populating, serializing, and retrieving our request andresponse types.
Update and run the application
We now have new generated client code, but we still need to implement and callthe new method in the human-written parts of our example application.
Update the server
In the same directory, open greeter_server.js
. Implement the new method likethis:
Update the client
In the same directory, open greeter_client.php
. Call the new method like this:
Run!
Install Pecl On Mac
Just like we did before, from the examples/node/dynamic_codegen
directory:
Run the server
In another terminal, from the examples/php
directory:
Run the client
Install Pecl On Mac Virtualbox
What’s next
Install Pecl On Mac Shortcut
- Read a full explanation of how gRPC works in What is gRPC?and gRPC Concepts
- Work through a more detailed tutorial in gRPC Basics: PHP
- Explore the gRPC PHP core API in its referencedocumentation