【问题标题】:PHP API for Google Cloud Datastore谷歌云数据存储的 PHP API
【发布时间】:2013-07-03 01:32:21
【问题描述】:
【问题讨论】:
标签:
php
google-app-engine
google-cloud-datastore
google-cloud-platform
【解决方案2】:
来自 Google 的 official PHP client library 现在是 GA。
您可以使用 Composer 安装它:
composer require google/cloud
使用它就像包含它一样简单,为您的项目初始化客户端,然后执行您的操作:
# Includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';
# Imports the Google Cloud client library
use Google\Cloud\Datastore\DatastoreClient;
# Your Google Cloud Platform project ID
$projectId = 'YOUR_PROJECT_ID';
# Instantiates a client
$datastore = new DatastoreClient([
'projectId' => $projectId
]);
# The kind for the new entity
$kind = 'Task';
# The name/ID for the new entity
$name = 'sampletask1';
# The Cloud Datastore key for the new entity
$taskKey = $datastore->key($kind, $name);
# Prepares the new entity
$task = $datastore->entity($taskKey, ['description' => 'Buy milk']);
# Saves the entity
$datastore->upsert($task);
echo 'Saved ' . $task->key() . ': ' . $task['description'] . PHP_EOL;
官方客户端库之前是https://github.com/tomwalder/php-gds,是 Cloud Datastore 中使用最广泛的 PHP 库,目前仍在使用和工作。
从版本 3 开始,PHP GDS 支持 v1 REST API,这意味着您可以在 App Engine 之外的任何计算服务上使用它。