3

Google 刚刚宣布支持 App Engine 的 PHP 运行时。我有一个使用本机 App Engine 数据存储的 Java 运行时开发的应用程序。它目前用作移动客户端的后端。我们正在研究开发一个单独的 Web 前端,它需要连接这个数据存储。从事此工作的开发人员更喜欢使用 PHP 进行开发,因此发布此公告的时机很有趣。

但是,查看文档时,我只看到对Google Cloud SQLGoogle Cloud Storage的引用作为“存储数据”下的选项。是否可以使用 PHP 运行时连接本机 App Engine 数据存储?

4

4 回答 4

4

在 I/O 上,我们还发布了Cloud Datastore,目前您应该考虑如何从 PHP 应用程序访问数据存储。

于 2013-05-17T04:00:46.530 回答
3
  1. 您需要为您的项目启用 Google Cloud 数据存储,请参阅https://developers.google.com/datastore/docs/activate#google_cloud_datastore_for_an_existing_app_engine_application

    注意:您不需要启用计算引擎

    确保在管理控制台上的应用程序设置云集成部分显示“项目已成功创建。有关更多详细信息,请参阅基础部分。

  2. 按照有关如何获取和使用 AppEngine 上的 Google API 客户端库的说明操作 https://gaeforphp-blog.appspot.com/2013/08/06/using-the-google-apis-client-library-for-php-使用应用程序引擎/

  3. 请参阅随附的工作示例以查找已解码的实体密钥: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);
}
于 2013-08-26T07:11:28.160 回答
2

这个库最近(由我发布) - 我希望它可以帮助人们找到这个线程。

它使使用 PHP 中的 Datastore(无论是否在 App Engine 上)变得更加容易。

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

享受!

于 2015-02-18T00:03:27.913 回答
0

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

插入代码:如何使用 GQL 使用管理控制台数据存储查看器插入记录

于 2013-10-10T14:30:21.830 回答