Google just announced support for a PHP runtime for App Engine. I have an app developed using the Java runtime which utilizes the native App Engine datastore. It currently functions as a back end for mobile clients. We are looking into developing a separate web front end which would need to interface this datastore. The developer working on this prefers to develop in PHP, so the timing of this announcement is interesting.
Looking over the documentation however, I only see reference to Google Cloud SQL as well as Google Cloud Storage as options under "Storing Data." Is it possible to interface the native App Engine datastore using the PHP runtime?
At I/O we also announce Cloud Datastore which for now is how you should think about accessing datastore from a PHP application.
you need to enable Google Cloud datastore for your project, see https://developers.google.com/datastore/docs/activate#google_cloud_datastore_for_an_existing_app_engine_application
note: you do not need compute engine enabled
make sure that on the admin console the application settings cloud integration section shows 'The project was created successfully. See the Basics section for more details.'
Follow the instructions on how to get and use the Google API client library on AppEngine https://gaeforphp-blog.appspot.com/2013/08/06/using-the-google-apis-client-library-for-php-with-app-engine/
See the attached working example for looking up Decoded entity key: Guestbook: name=default_guestbook > Greeting: id=5733953138851840
<?php
const SERVICE_ACCOUNT_NAME = 'your-service-account-id@developer.gserviceaccount.com';
require_once 'libraries/google-api-php-client/src/Google_Client.php';
require_once 'libraries/google-api-php-client/src/contrib/Google_DatastoreService.php';
$client = new Google_Client();
$client->setApplicationName("your_app_id");
$key = file_get_contents('storage/your-hashed-keyid-privatekey.p12');
$client->setAssertionCredentials(
new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/datastore'),
$key)
);
$datastore = new Google_DatastoreService($client);
$lookup = new Google_LookupRequest();
$path1 = new Google_KeyPathElement();
$path1->setKind('Guestbook');
$path1->setName('default_guestbook');
$path2 = new Google_KeyPathElement();
$path2->setKind('Greeting');
# this is just an example check a real entity id in your datastore
# if you do not have ancestor entity you only need one (path1) element
$path2->setId('5733953138851840');
$key = new Google_Key();
$key->setPath([$path1,$path2]);
$keyArray = array();
$keyArray[] = $key;
$lookup->setKeys($keyArray);
if(array_key_exists('catchError', $_GET)){
try{
$result = $datastore->datasets->lookup('your_project_name', $lookup);
var_dump($result);
}
catch(Google_ServiceException $e){
echo "<pre>";
var_dump($e);
echo "</pre>";
}
}
else{
$result = $datastore->datasets->lookup('your_project_name', $lookup);
var_dump($result);
}
This library was recently released (by me) - I hope it helps people finding this thread.
It makes using Datastore from PHP (on App Engine or not) much easier.
https://github.com/tomwalder/php-gds
Enjoy!
reference: https://developers.google.com/datastore/docs/concepts/gql#using_literals_sample_code
<?php
const APP_NAME='a-test-com';
const SERVICE_ACCOUNT_NAME='511908@developer.gserviceaccount.com';
$_PRIVATE_KEY=file_get_contents('data/34672c-privatekey.p12');
require_once 'google-api-php-client/Google_Client.php';
$client=new Google_Client();
$credentials=new Google_AssertionCredentials(SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/datastore'
),
$_PRIVATE_KEY
);
$client->setAssertionCredentials($credentials);
$postBody=json_encode(array('gqlQuery'=>array('allowLiteral'=>true, 'queryString'=>
"SELECT * FROM Guestbook WHERE __key__=key(Guestbook, 'default_guestbook')"
)));
$httpRequest=new Google_HttpRequest('datastore/v1beta2/datasets/'.APP_NAME.'/runQuery', 'POST', null, $postBody);
$head=array('content-type'=>'application/json; charset=UTF-8',
'content-length'=>Google_Utils::getStrLen($postBody)
);
$httpRequest->setRequestHeaders($head);
$httpRequest=Google_Client::$auth->sign($httpRequest);
$result=Google_REST::execute($httpRequest);
var_export($result);
?>
insertion code: How to insert a record using the Admin console Datastore Viewer using GQL
来源:https://stackoverflow.com/questions/16601074/can-the-recently-released-gae-php-runtime-access-the-native-gae-datastore