PHP 㧠GCP ã® Cloud Datastore ã®ãã¼ã¿ãèªã¿æ¸ããã¦ã¿ã¾ããã ãã¾ãæ å ±ããªãã®ã§ãã¨ã£ãããã¨ãã¦ã¡ã¢ãã¦ããã¾ãã
Google App Engine ããè¡ãã¾ããããä»ã®ç°å¢ããã§ãåãã¯ãã§ãã
google/google-api-php-clientãããããã使ããã¦ãããã¾ãã
ãã®ã©ã¤ãã©ãªã¯ãGoogle Cloudã§ã¯ãªããGoogle APIã®ã©ã¤ãã©ãªãªã®ã§ãGoogle Cloudのライブラリ一覧のページã«ã¯è¼ã£ã¦ãã¾ãã*1ã ããGoogle Cloud Client library for PHP ã¯ãªããããªã®ã§ããã¡ãã使ãã¾ãã
ã©ã¤ãã©ãªã®å°å ¥
githubのreadmeéãã«å°å ¥ãã¾ãã
$ composer require google/apiclient:^2.0
ã½ã¼ã¹ã³ã¼ãã«autoload.phpãrequireãã¾ãã
ããã¸ã§ã¯ãã®ä½æã¨è¨¼ææ¸ã®åå¾
Google Developer Consoleãããããã¸ã§ã¯ããä½æãï¼ãªãå ´åï¼ããµã¼ãã¹ã¢ã«ã¦ã³ããä½æãã¾ãã
証ææ¸ãåå¾ãã¾ãã
- ã³ã³ã½ã¼ã«ãéã
- API Manager
- èªè¨¼æ å ±
- ããµã¼ãã¹ã¢ã«ã¦ã³ããã¼ããä½æ
- ãApp Engine default service accountããé¸æï¼æ°è¦ä½æãã¦ãå¯ï¼
- jsonãã¡ã¤ã«ããã¦ã³ãã¼ãã§ãã
ã½ã¼ã¹ã³ã¼ãã®è¨ç½®
hogehoge.json ããã»ã©ã®è¨¼ææ¸
app.yaml
application: practice161003 version: 1 module: default runtime: php55 api_version: 1 threadsafe: true handlers: - url: / script: index.php
index.php
<?php require('vendor/autoload.php'); # 証ææ¸ã®ãã¹ putenv('GOOGLE_APPLICATION_CREDENTIALS=' . __DIR__ . '/hogehoge.json'); $client = new Google_Client(); $client->useApplicationDefaultCredentials(); $client->addScope(Google_Service_Datastore::DATASTORE); $datastore = new Google_Service_Datastore($client); $kind = new Google_Service_Datastore_KindExpression(['name' => 'User']); $property = new Google_Service_Datastore_PropertyReference(['name' => 'name']); $value = new Google_Service_Datastore_Value(['stringValue' => 'konishi']); $property_filter = new Google_Service_Datastore_PropertyFilter([ 'property' => $property, 'op' => 'EQUAL', 'value' => $value]); $filter = new Google_Service_Datastore_Filter(['propertyFilter' => $property_filter]); $query = new Google_Service_Datastore_Query(['kind' => [$kind], 'filter' => $filter]); $request = new Google_Service_Datastore_RunQueryRequest(['query' => $query]); $result = $datastore->projects->runQuery('practice161003', $request); var_dump($response->toSimpleObject());
ä¸è¨ã®ã³ã¼ãã§ãkind=Userã®ã¨ã³ãã£ãã£ãname=konishiã§ãã£ã«ã¿ãããã¼ã¿ãnameåã®éé ã§åå¾ãã¨ãªãã¾ãã
ã¡ãªã¿ã«ããã«åºã¦ããã¯ã©ã¹ã¯ãã¹ã¦Google_model
ã¯ã©ã¹ãç¶æ¿ãã¦ãã¾ãã
Google_model
ãç¶æ¿ããã¯ã©ã¹ã¯ãsetterã使ããªãã¦ããã³ã³ã¹ãã©ã¯ã¿ã«é£æ³é
åã§æ¸¡ããã®ã§ã$query
ã¯ä»¥ä¸ã®ããã«ãããã¾ãã
$query = new Google_Service_Datastore_Query([ 'kind' => [ [ 'name' => 'User', ], ], 'filter' => [ 'propertyFilter' => [ 'property' => [ 'name' => 'name', ], 'op' => 'EQUAL', 'value' => [ 'stringValue' => 'konishi' ] ] ], 'order' => [ 'property' => [ 'name' => 'name', ], 'direction' => 'descending', ], 'limit' => 10, ]); $request = new Google_Service_Datastore_RunQueryRequest(['query' => $query]); $response = $datastore->projects->runQuery('ããã¸ã§ã¯ãå', $request);
ãã®ãããã¯å¥½ã¿ã§ãããã å人çã«ã¯ã©ããã©ããã¼æ¸ãã¦ãã¾ããã¨ã«ãªãã¨æãã®ã§ãjsonã§ã ã¼ã£ã¨æ¸ãã¦ãã¾ãæ¹ã«ãããã¨æãã¾ãã
insert
$mutation = new Google_Service_Datastore_Mutation([ 'insert' => [ 'key' => [ 'path' => [ 'kind' => 'User', 'name' => 'keyã¨ãªãããã¹ã', ] ], 'properties' => [ 'age' => [ 'excludeFromIndexes' => false, 'integerValue' => 27, ] ] ] ]); $request = new Google_Service_Datastore_CommitRequest([ 'mode' => 'NON_TRANSACTIONAL', 'mutations' => [$mutation], ]); $response = $datastore->projects->commit('ããã¸ã§ã¯ãå', $request);
jsonã¯ã以ä¸ã®ãªãã¡ã¬ã³ã¹ãè¦ã¦çµã¿ç«ã¦ã¾ãã
Method: projects.runQuery | Cloud Datastore | Google Cloud Platform
便å©ãããªã©ããã¼ä½ã£ã¦ãã人ããã¾ããã2013å¹´ã§æ´æ°ããã¦ããããä»ã¯åãã¾ããã
ææ³
ãã¼ã«ã«ç°å¢ããå®è¡ããã¨ãããä¸è¨ã®ã³ã¼ãã®runQueryã®é¨åã ãã§2ç§ã»ã©ããã£ã¦ãã¾ããã
GAEä¸ã ã¨10msã»ã©ã«ãªã£ã¦ãã¾ããã Cloud Datastoreã®ãã¼ã¿ã¯ãã«ããªã¼ã¸ã§ã³ã¨ãããã¨ã§ãããGAEã®ãªã¼ã¸ã§ã³us-centralã«ãªã£ã¦ããã®ã§ããã¹ãã§ã¢ãããããã¼ã¿ã¯us-centralã«ããããã§ãã
æ±äº¬ãªã¼ã¸ã§ã³æ©ãæ¥ã¦æ¬²ãããªã¼ã
åè
å°ãå¤ãã£ãã®ã§ãç¾å¨åããããã«ã¯ä½ç¹ãä¿®æ£ãå¿ è¦ã§ããã
Google Cloud Datastore APIをPHPから使ってみる(データ取得) - Qiita
*1:æ¤ç´¢ãããããã¾ãããhttps://github.com/GoogleCloudPlatform/google-cloud-php 対å¿ãã¦ããªããµã¼ãã¹ãå¤ãã®ã§ããã以å¤ã®ãµã¼ãã¹ã®å ´åã¯ãAPI Client Library使ã£ã¦ãã¨æ¸ãã¦ããã