如何在 php 中初始化 Cloud Firestore?

yan*_*a35 3 php firebase google-api-php-client grpc google-cloud-firestore

我按照这些教程在 PHP 中使用 Firestore:https : //github.com/googleapis/google-cloud-php-firestore https://firebase.google.com/docs/firestore/quickstart

但是当我加载我的页面时,会显示此消息:

Google\ApiCore\ValidationException:呈现“projects/{project= }/databases/{database= }”时出错:预期绑定“project”以匹配段“{project=*}”,而是得到空提供的绑定:数组([project ] => [database] => (default) ) 在 C:\wamp64\www\php\vendor\google\gax\src\ResourceTemplate\RelativeResourceTemplate.php 第 238 行'

这是我的代码:

作曲家.json:

 {
    "require": {
        "google/cloud-firestore": "^1.4",
        "grpc/grpc": "v1.19.0"
    }
}
Run Code Online (Sandbox Code Playgroud)

index.php(示例的副本):

require ('vendor/autoload.php');
use Google\Cloud\Firestore\FirestoreClient;

/**
 * Initialize Cloud Firestore with default project ID.
 * ```
 * initialize();
 * ```
 */
    // Create the Cloud Firestore client
    $db = new FirestoreClient();
    printf('Created Cloud Firestore client with default project ID.' . 
PHP_EOL);



$docRef = $db->collection('users')->document('lovelace');
$docRef->set([
    'first' => 'Ada',
    'last' => 'Lovelace',
    'born' => 1815
]);
Run Code Online (Sandbox Code Playgroud)

Jua*_*ara 11

看起来您没有设置默认的项目 ID。

尝试将GCLOUD_PROJECT环境变量设置为您的项目 ID。

set GCLOUD_PROJECT=PROJECT_ID
Run Code Online (Sandbox Code Playgroud)

您还可以尝试像这样设置项目 ID:

$db = new FirestoreClient([
        'projectId' => 'my-project-id',
    ]);
Run Code Online (Sandbox Code Playgroud)