Google Cloud PHP Pub/Sub Client

PHP idiomatic client for Google Cloud Pub/Sub services.
This client supports the following Google Cloud Platform services:

Prerequisites

Installation

Authentication

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.

Documentation

The documentation is available here.
The two main classes for the PubSub API are Publisher Client and Subscriber Client.

Examples

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

Execution

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

Pecl

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.

Installing 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.

Installing the gRPC Extension

There are two steps to install the gRPC extension:

  1. Build the extension using Pecl, and
  2. Add the extension to your php.ini file.
Build the 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.

Prerequisite for OSX: disable System Integrity Protection

Troubleshooting:

Add the extension to you PHP ini file

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:

  1. Locate your PHP ini file by running $ php --ini from the terminal. You should see a line like:
    Loaded Configuration File: /etc/php5/cli/php.ini
  2. Edit the file using a text editor. Note you will likely require root access to edit the file. So for example using vi, run:
    $ sudo vi /etc/php5/cli/php.ini
  3. Add the following line anywhere in the file: 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.

Troubleshooting

Problems running the sample:
Problems installing composer: