Skip to content

Latest commit

 

History

History

firestore

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Google Cloud Firestore API Samples

These samples show how to use the Google Cloud Firestore API to store and query data.

Setup

Prerequisites

  1. Open the Firebase Console and create a new project. (You can't use both Cloud Firestore and Cloud Datastore in the same project, which might affect apps using App Engine. Try using Cloud Firestore with a different project if this is the case).

  2. In the Database section, click Try Firestore Beta.

  3. Click Enable.

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. Enable the Cloud Firestore API.

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

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

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

  5. Set GOOGLE_APPLICATION_CREDENTIALS environment variable pointing to that file.

Samples

To run the Firestore Samples, run any of the files in src/ on the CLI:

$ php src/setup_dataset.php

Usage: setup_dataset.php $projectId

    @param string $projectId The Google Cloud Project ID

The client library

This sample uses the Firestore 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.

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.

If you have not set a timezone you may get an error from php. This can be resolved by:

  1. Finding where the php.ini is stored by running php -i | grep 'Configuration File'
  2. Finding out your timezone from the list on this page: http://php.net/manual/en/timezones.php
  3. Editing the php.ini file (or creating one if it doesn't exist)
  4. Adding the timezone to the php.ini file e.g., adding the following line: date.timezone = "America/Los_Angeles"