Last active
January 1, 2016 00:09
-
-
Save amygdala/8064977 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Example code for connecting to the Google Cloud Datastore | |
require_once 'config.php'; | |
require_once 'DatastoreService.php'; | |
require_once 'Google/Client.php'; | |
require_once 'Google/Auth/AssertionCredentials.php'; | |
require_once 'Google/Service/Datastore.php'; | |
// assumes that an array like the following is defined in config.php | |
// $google_api_config = [ | |
// 'application-id' => 'A name for your app engine app', | |
// 'service-account-name' => 'xxx@developer.gserviceaccount.com', | |
// 'private-key' => file_get_contents('xxx-privatekey.p12'), | |
// 'dataset-id' => 'your-app-id' | |
// ]; | |
function create_entity() { | |
$entity = new Google_Service_Datastore_Entity(); | |
$entity->setKey(createKeyForTestItem()); | |
$string_prop = new Google_Service_Datastore_Property(); | |
$string_prop->setStringValue("test field string value"); | |
$property_map = []; | |
$property_map["testfield"] = $string_prop; | |
$entity->setProperties($property_map); | |
return $entity; | |
} | |
function createKeyForTestItem() { | |
$path = new Google_Service_Datastore_KeyPathElement(); | |
$path->setKind("testkind"); | |
$path->setName("testkeyname"); | |
$key = DatastoreService::getInstance()->createKey(); | |
$key->setPath([$path]); | |
return $key; | |
} | |
function create_test_request() { | |
$entity = create_entity(); | |
$mutation = new Google_Service_Datastore_Mutation(); | |
$mutation->setUpsert([$entity]); | |
$req = new Google_Service_Datastore_CommitRequest(); | |
$req->setMode('NON_TRANSACTIONAL'); | |
$req->setMutation($mutation); | |
return $req; | |
} | |
DatastoreService::setInstance(new DatastoreService($google_api_config)); | |
try { | |
// test the config and connectivity by creating a test entity, building | |
// a commit request for that entity, and creating/updating it in the datastore | |
$req = create_test_request(); | |
DatastoreService::getInstance()->commit($req); | |
} | |
catch (Google_Exception $ex) { | |
syslog(LOG_WARNING, 'Commit to Cloud Datastore exception: ' . $ex->getMessage()); | |
echo "There was an issue -- check the logs."; | |
return; | |
} | |
echo "Connected!"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment