简体   繁体   中英

PHP API for Google Cloud Datastore

I've been unable to locate any documentation for a PHP API for Google Could Datastore. I'm attempting to migrate a NoSQL (MongoDB database) website to Google App Engine - and it appears Cloud Datastore is the best option at present. The only documentation I could locate is for Node.js, Python and Java.

https://developers.google.com/datastore/docs/getstarted/

我在这里有一个用于此的库: https : //github.com/pwhelan/datachore (也可以从packagist以datachore / datachore的形式获得)。

The official PHP client library from Google is now GA.

You can install it using Composer:

composer require google/cloud

Using it is then as simple as including it, initializing the client for your project, then performing your operations:

# 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;

Previous to the official client library, the most widely used PHP library for Cloud Datastore, that's still used and works, is https://github.com/tomwalder/php-gds

As of version 3, PHP GDS supports the v1 REST API meaning you can use it outside of App Engine on any compute service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM