This simple command-line application demonstrates how to invoke Google Pub\Sub from PHP.
Authentication is typically done through Application Default Credentials which means you do not have to change the code to authenticate as long as your environment has credentials. You have a few options for setting up authentication:
-
When running locally, use the Google Cloud SDK
gcloud auth application-default login -
When running on App Engine or Compute Engine, credentials are already set-up. However, you may need to configure your Compute Engine instance with additional scopes.
-
You can create a Service Account key file. This file can be used to authenticate to Google Cloud Platform services from any environment. To use the file, set the
GOOGLE_APPLICATION_CREDENTIALSenvironment variable to the path to the key file, for example:export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json
-
Ensure the gRPC PHP Extension is installed and enabled on your machine.
-
Install dependencies via Composer. Run
php composer.phar install(if composer is installed locally) orcomposer install(if composer is installed globally). -
Create a service account at the Service account section in the Cloud Console
-
Download the json key file of the service account.
-
Set
GOOGLE_APPLICATION_CREDENTIALSenvironment variable pointing to that file.
To run the Pub/Sub Samples, run any of the files in src/ on the CLI:
$ php src/create-topic.php
Usage: create_topic.php $projectId $topicName
@param string $projectId The Google project ID.
@param string $topicName The Pub/Sub topic name.
At this time, the GitHub actions in this repo fail to run the tests written in this folder. The developer is responsible for locally running and confirming their samples and corresponding tests.
Some tests in the pubsubTest.php requires PubSub emulator. These tests start with $this->requireEnv('PUBSUB_EMULATOR_HOST').
- Python
xcode-select --install
brew install pyenv
pyenv install <version>
python3 --version
- JDK
brew install openjdk
export JAVA_HOME=<path to openjdk folder>
export PATH="$JAVA_HOME/bin:$PATH"
Once python, JDK, and GCloud CLI are installed, follow these instructions to run the emulator.
Open a new tab in terminal, separate from the one running your emulator.
// php-docs-samples/testing folder
$ cd ../../../testing
$ export GOOGLE_PROJECT_ID=<project id>
$ export GOOGLE_PUBSUB_TOPIC=<topic name>
$ export GOOGLE_PUBSUB_STORAGE_BUCKET=<bucket name>
$ export GOOGLE_PUBSUB_SUBSCRIPTION=<subscription name>
// only set if your test requires the emulator
$ export PUBSUB_EMULATOR_HOST=localhost:<emulator port>
// unset the PUBSUB emulator host variable if you want to run a test that doesn't require an emulator
$ unset PUBSUB_EMULATOR
Run your test(s) like so in the same terminal tab that you set your env variables in the previous step.
// Run all tests in pubsubTest.php. --verbose tag is recommended to see any issues or stack trace
$ php-docs-samples/testing/vendor/bin/phpunit ../pubsub/api/test/pubsubTest.php --verbose
// Run a single test in pubsubTest.php
$ php-docs-samples/testing/vendor/bin/phpunit ../pubsub/api/test/pubsubTest.php --filter testSubscriptionPolicy --verbose
If you create a PR and the Lint / styles (pull_request) check fails, this is a quick fix.
$ php-docs-samples/testing/vendor/bin/php-cs-fixer fix <path to your file>
If you get the following error, set the environment variable GCLOUD_PROJECT to your project ID:
[Google\Cloud\Core\Exception\GoogleException]
No project ID was provided, and we were unable to detect a default project ID.
This sample uses the Cloud Pub/Sub Library for PHP. You can read the documentation for more details on API usage and use GitHub to browse the source and report issues.