【问题标题】:How to properly deploy PHP app with Firestore on Google Cloud Platform?如何在 Google Cloud Platform 上使用 Firestore 正确部署 PHP 应用程序?
【发布时间】:2019-09-30 15:21:45
【问题描述】:

我正在基于https://github.com/googleapis/google-cloud-php-firestoreGoogle Cloud Platform 上使用Cloud Firestore 设置PHP 应用

部署到 App Engine 后,我看到“new FirestoreClient();”不起作用,但相同的代码在 localhost。我该如何解决这个问题?

代码:

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 平台日志

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"
    }
}

使用本地主机输出

你好 Firestore

array(4) { ["headline"]=> string(1) "b" ["amount_of_threads"]=> int(1) ["description"]=> string(22) "洪水,仅此而已" ["content_type"]=> 字符串(7) "默认" }

再见 Firestore

使用谷歌云平台输出

你好 Firestore

Database view

【问题讨论】:

  • 由于在“Hello Firestore”输出后停止执行,因此出现错误。在 Cloud Console 中查看您的应用日志以了解更多详情。发现错误后,如果您无法自行修复,请在此问题中分享。

标签: php google-app-engine google-cloud-platform google-cloud-firestore google-app-engine-php


【解决方案1】:

我认为您可能缺少 php.ini 文件来启用 Cloud Firestore 所需的 grpc 扩展:

php.ini

extension=grpc.so

将此文件包含在您的应用程序中。查看这些文档:

另外,您提到在您的日志中没有看到任何错误。这些错误可能已在默认情况下不可见的不同文件中报告。以下是在控制台中查看它们的方法:

【讨论】:

  • 现在我在 localhost 上看到身份验证警告,而 gcloud 上没有任何新内容(
  • 您收到任何错误吗?查看日志cloud.google.com/appengine/docs/standard/php7/…
  • 出于某种原因,我在上面和一段时间后只显示了信息日志 - 头部标题中 logo.png 的响应 204
  • 要验证的另一件事是项目 ID。您可能还想删除putenv("GCLOUD_PROJECT=moondepth"); 行。 moondepth 是正确的项目 ID,项目是否有 boards/b-1 文档?
  • 项目 ID 是“moondepth”并且文档存在,但是当我将 id 更改为“sdgsadwgf”时 - 在本地主机上我得到“Hello Firestore\n NULL\n Goodbye Firestore\n”和 GCloud我得到“Hello Firestore\n”没有错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-24
  • 2016-08-04
  • 1970-01-01
  • 2018-08-27
  • 1970-01-01
  • 2020-12-28
  • 1970-01-01
相关资源
最近更新 更多