Google Cloud Spanner PHP Samples
================================

[![Open in Cloud Shell][shell_img]][shell_link]

[shell_img]: http://gstatic.com/cloudssh/images/open-btn.svg
[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googlecloudplatform/php-docs-samples&page=editor&working_dir=spanner

This directory contains samples for calling [Google Cloud Spanner][spanner]
from PHP.

Google Cloud Spanner is a highly scalable, transactional, managed,
[NewSQL][newsql] database service. Cloud Spanner solves the need for a
horizontally-scaling database with consistent global transactions and SQL
semantics.

See [Getting Started in PHP][getting-started-php]
for a walkthrough of these samples.

[spanner]: https://cloud.google.com/spanner/docs/reference/libraries
[newsql]: https://en.wikipedia.org/wiki/NewSQL
[getting-started-php]: https://cloud.google.com/spanner/docs/getting-started/php/

## Setup

### Authentication

Authentication is typically done through [Application Default Credentials][adc]
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][google-cloud-sdk]

        gcloud auth application-default login

1. 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][additional_scopes].

1. You can create a [Service Account key file][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

[adc]: https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow
[additional_scopes]: https://cloud.google.com/compute/docs/authentication#using
[service_account_key_file]: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount

### Create an instance

These samples require you to first set up a [Spanner Instance][create-instance].
Once you've finished with the samples, you can [delete your instance][delete-instance]
to prevent incurring any additional charges.

[create-instance]: https://cloud.google.com/spanner/docs/create-manage-instances
[delete-instance]: https://cloud.google.com/spanner/docs/create-manage-instances#delete-instance

## Install Dependencies

1. Ensure the [gRPC PHP Extension][php_grpc] is installed and enabled on your machine.
1. [Enable the Cloud Spanner API](https://console.cloud.google.com/flows/enableapi?apiid=spanner.googleapis.com).

1. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md).
    Run `php composer.phar install` (if composer is installed locally) or `composer install`
    (if composer is installed globally).

1. Create a service account at the
[Service account section in the Cloud Console](https://console.cloud.google.com/iam-admin/serviceaccounts/)

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

1. Set `GOOGLE_APPLICATION_CREDENTIALS` environment variable pointing to that file.

## Samples

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

```
$ php src/create_instance.php

Usage: create_instance.php $instanceId

  @param string $instanceId The Spanner instance ID.
```

## 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 [Spanner Client Library for PHP][google-cloud-php-spanner].
You can read the documentation for more details on API usage and use GitHub
to [browse the source][google-cloud-php-source] and  [report issues][google-cloud-php-issues].

[php_grpc]: http://cloud.google.com/php/grpc
[google-cloud-php-spanner]: https://cloud.google.com/php/docs/reference/cloud-spanner/latest
[google-cloud-php-source]: https://github.com/GoogleCloudPlatform/google-cloud-php
[google-cloud-php-issues]: https://github.com/GoogleCloudPlatform/google-cloud-php/issues
[google-cloud-sdk]: https://cloud.google.com/sdk/
