PHP idiomatic client for Google Cloud Pub/Sub services.
This client supports the following Google Cloud Platform services:
$ sudo pecl install grpc-1.0.0
extension=grpc.so
to your php.ini file if pecl was not able to do so$ php composer.phar install
$ composer install
To authenticate all your API calls, first install and setup the Google Cloud SDK.
After that is installed, run the following command in your terminal:
$ gcloud auth application-default login
NOTE: if you are using an older version of the Google Cloud SDK, you may need to use the following command instead:
$ gcloud beta auth application-default login
At this point, you are now authenticated to make calls to Pub/Sub and other Google Cloud services.
The documentation is available here.
The two main classes for the PubSub API are Publisher Client and Subscriber Client.
The documentation includes simple examples for every API method. Please read it through for more usage samples.
<?php
require __DIR__ . '/vendor/autoload.php';
use Google\Cloud\PubSub\V1\PublisherClient;
try {
$publisherClient = new PublisherClient();
$formattedName = PublisherClient::formatTopicName("[PROJECT_ID]", "[TOPIC_ID]");
$response = $publisherClient->createTopic($formattedName);
echo "Created topic with name " . $response->getName() . "\n";
} finally {
if (isset($publisherClient)) {
$publisherClient->close();
}
}
Place the code above in a file alongside your composer.json
file, for example PubSubSample.php
To execute your client app from the command line, run the following commands (which assume that you put your app in PubSubSample.php
):
$ php PubSubSample.php
If you have not used pecl to install PHP extensions before, you may encounter some issues the first time you install and use it. Please see below for help installing and troubleshooting pecl.
Pecl is provided along with Pear. The instructions for installing Pear are available here.
If you are using OS X El Capitan, an easy installation method is given by this Stack Overflow answer.
You can verify that Pecl is available by running $ pecl version
, or by following the instructions provided here.
There are two steps to install the gRPC extension:
You can build the extension by running this command in your terminal:
$ sudo pecl install grpc-1.0.0
If you have not used Pecl to build a PHP extension before, you may encounter
some problems. Please see the prerequisites and troubleshooting list below:
Prerequisite for OSX: accept the xcode license agreement.
$ m4 --version
Prerequisite for OSX: disable System Integrity Protection
Troubleshooting:
$ sudo pecl install grpc-1.0.0
$ brew install autoconf
or by following these instructions.$ sudo apt-get install autoconf
After the grpc extension has been build with pecl, you need to add it to your PHP ini file. If the Pear php_ini
setting is configured correctly, this will happen automatically, and the pecl installation will print Extension grpc enabled in php.ini. Otherwise, you will need manually modify your php ini file, by following these steps:
$ php --ini
from the terminal. You should see a line like:$ sudo vi /etc/php5/cli/php.ini
extension=grpc.so
If the grpc extension is not installed or has not been added to your PHP ini file, you will see an error message like:
Fatal error: Undefined constant ‘Grpc\STATUS_ABORTED’ in /Users/USERNAME/PROJECT/vendor/google/gax/src/GrpcConstants.php on line 53
This indicates that you need to follow the steps above to correctly install the gRPC extension and add it to your PHP ini file.
$ google auth application-default login
$ google beta auth application-default login
for older versions of the SDK)$ php --ini
date.timezone = America/Los_Angeles
$ pecl list
)brew
failed
$ brew update
and $ brew doctor
, and fix any issues found