3

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?

hakre
  • 193,403
  • 52
  • 435
  • 836
Jeff Lockhart
  • 5,379
  • 4
  • 36
  • 51
  • 1
    It looks like they Cloud Datastore is it. Interesting that google didn't want to introduce the nosql model to the php audience as the primary datastore ;-) for php on appengine. – Tim Hoffman May 17 '13 at 07:57
  • some things do take time, surprisingly enough ;) – Stuart Langley May 17 '13 at 14:30
  • There is a possibility using [Quercus on GAE](http://caucho.com/articles/Quercus_on_GAE.pdf), this project modified the popular WordPress PHP blogging software to use Google App Engine's Datastore as a backend. – eQ19 Mar 18 '16 at 20:11

4 Answers4

4

At I/O we also announce Cloud Datastore which for now is how you should think about accessing datastore from a PHP application.

Stuart Langley
  • 7,044
  • 1
  • 20
  • 20
  • I see how to [activate Cloud Datastore for an existing App Engine application](https://developers.google.com/datastore/docs/activate#google_cloud_datastore_for_an_existing_app_engine_application). But I don't see documentation on using Cloud Datastore in a PHP app instance, only [getting started with Node.js, Python and Java](https://developers.google.com/datastore/docs/getstarted/). Am I missing something? – Jeff Lockhart May 17 '13 at 08:05
  • You should expect to see more info on this over the next few weeks as we put all of the pieces in place. – Stuart Langley May 17 '13 at 14:30
  • 1
    There's support been added to the latest google php api client - Look at the class Google_DatastoreService. I'll try and do a quick demo tutorial soon. – Stuart Langley Aug 15 '13 at 22:42
  • Stuart mentioned that the new api has classes to use the Datastore. Has there been any more progress on this? I see it in the api, but would love an example :) Currently I am using Quarcus to run PHP on Java. – Sean Brennan Aug 20 '13 at 13:13
3
  1. 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.'

  2. 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/

  3. 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);
}
Feczo
  • 608
  • 5
  • 8
2

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!

Tom
  • 1,563
  • 12
  • 12
0

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

Community
  • 1
  • 1
diyism
  • 12,477
  • 5
  • 46
  • 46