php-docs-samples/pubsub/api at master · ehurrell/php-docs-samples · GitHub
Skip to content

Latest commit

 

History

History
 
 

Folders and files

README.md

Google Pub\Sub CLI for PHP

Description

This simple command-line application demonstrates how to invoke Google Pub\Sub from PHP.

Setup

Authentication

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:

  1. When running locally, use the Google Cloud SDK

     gcloud auth application-default login
    
  2. 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.

  3. 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_CREDENTIALS environment variable to the path to the key file, for example:

     export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json
    

Install Dependencies

  1. Ensure the gRPC PHP Extension is installed and enabled on your machine.

  2. Enable the Cloud Pub/Sub API.

  3. Install dependencies via Composer. Run php composer.phar install (if composer is installed locally) or composer install (if composer is installed globally).

  4. Create a service account at the Service account section in the Cloud Console

  5. Download the json key file of the service account.

  6. Set GOOGLE_APPLICATION_CREDENTIALS environment variable pointing to that file.

Samples

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.

Troubleshooting

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.

The client library

This sample uses the Google Cloud Client Library for PHP. You can read the documentation for more details on API usage and use GitHub to browse the source and report issues.