Search code examples
phpgoogle-app-enginegoogle-cloud-platformgoogle-cloud-firestoregoogle-app-engine-php

How to properly deploy PHP app with Firestore on Google Cloud Platform?


I'm setting up a PHP app with Cloud Firestore on Google Cloud Platform based on https://github.com/googleapis/google-cloud-php-firestore.

After deploying to App Engine, I see that "new FirestoreClient();" doesn't work, but the same code runs normally on localhost. How can I fix this?

The code:

index.php

<?php

require_once 'vendor/autoload.php';
use Google\Cloud\Firestore\FirestoreClient;

echo "Hello Firestore";

$firestore = new FirestoreClient();
$collectionReference = $firestore->collection('boards');
$documentReference = $collectionReference->document("b-1");
$snapshot = $documentReference->snapshot();
$data = $snapshot->data();
var_dump($data);

echo "Goodbye Firestore";
?>

app.yaml

runtime: php72

handlers:
- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

- url: /images
  static_dir: images

- url: /stylesheets
  static_dir: stylesheets

Google Cloude Platform logs

GET 200 385 B 1,3 s Chrome 74 / I GET 200 385 B 1,3 s Chrome 74
nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /tmp/google-config/nginx.conf:3 
A GET 200 107,58 KiB 2 ms Chrome 74 /images/logo.png A GET 200 107,58 KiB 2 ms Chrome 74

composer.json

{
    "name": "projects/plzwork",
    "description": "i don't know...",
    "type": "project",
    "license": "Apache-2.0",
    "require": {
        "google/cloud": "^0.101.0",
        "grpc/grpc": "^1.19"
    }
}

Output with localhost

Hello Firestore

array(4) { ["headline"]=> string(1) "b" ["amount_of_threads"]=> int(1) ["description"]=> string(22) "Flood and nothing else" ["content_type"]=> string(7) "default" }

Goodbye Firestore

Output with Google Cloud Platform

Hello Firestore

Database view


Solution

  • I think you might be missing a php.ini file to enable the grpc extension which Cloud Firestore requires:

    php.ini

    extension=grpc.so
    

    Include this file with your application. Check out these docs:

    Also, you mentioned not seeing any errors in your logs. The errors might've been reported in different files that aren't visible by default. Here's how to view them in the console:

    enter image description here