Can the recently released GAE PHP runtime access the GAE embedded datastore?

Google has just announced support for the PHP runtime for App Engine. I have an application developed using the Java runtime that uses its own App Engine datastore. It currently works as a back end for mobile clients. We are exploring the development of a separate web interface that should interact with this data warehouse. The developer working on this prefers to develop in PHP, so the time of this announcement is interesting.

However, looking at the documentation, I see the Google Cloud SQL link, as well as Google Cloud Storage as parameters in the "Data Storage" section. Is it possible to link the native App Engine datastore using the PHP runtime?

+3
source share
4 answers

In I / O, we also announce Cloud Datastore , which at the moment is how you should think about accessing the data store from a PHP application.

+4
source

<?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); } 
+3
source

This library was recently released (by me) - I hope this helps people find this thread.

This makes it easier to use Datastore from PHP (in the App Engine or not).

https://github.com/tomwalder/php-gds

Enjoy it!

+2
source

link: 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); ?> 

insert code: How to insert a record using the Datastore Viewer console using GQL

0
source

Source: https://habr.com/ru/post/955392/


All Articles