Reputation: 1
I'm really new to the whole GCP platform and I'm trying to create my first web app, specifically in PHP. I've went through every thread, tutorials, etc. but they are too advance from me.
Could someone please let me know what are the most basic steps for querying a data in Cloud datastore using PHP from APP Engine?
This is my setup: In my Datastore, there's 1 entry which basically has 2 properties called name and address, a PHP file on APP Engine that would just query the datastore.
I've enabled Datastore API access for my app. I've also deployed it. But I can't make it to work.
This is my view.php file:
<?php
use Google\Cloud\Datastore\DatastoreClient;
# Your Google Cloud Platform project ID
$projectId = '<my project id>';
# Instantiates a client
$datastore = new DatastoreClient(['projectId' => $projectId]);
$query = $datastore->query()
->kind('person');
$result = $datastore->runQuery($query);
foreach ($result as $entity) {
echo 'Entity found: ' . $entity['name'] . PHP_EOL;
}
?>
note: I deployed my app with these 3 files:
app.yaml content:
runtime: php55
api_version: 1
handlers:
- url: /
script: index.php
- url: /index\.html
script: index.php
- url: /view
script: view.php
- url: /view\.html
script: view.php
- url: /.*
script: not_found.php
Upvotes: 0
Views: 933
Reputation: 301
You also have not included any files in your view.php script. PHP/GAE need to know where to find
Google\Cloud\Datastore\DatastoreClient;
One way is to install composer and allow composer to manage all your includes/requires. The other option is to manually include everything you will need to use. With Google though, there are many many components, so using composer would be much easier. Install composer, require the Google PHP SDK package, and then all you need to do is include vendor/autoload.php at the start of your PHP file.
Upvotes: 0
Reputation: 301
If you are looking for a really easy to use library to access Datastore from PHP, I would recommend PHP-GDS by Tom Walder https://github.com/tomwalder/php-gds He has done a great job of making Datastore easy for PHP, without it can be a nightmare at times. He is continually updating the library as he uses it in his projects, and I have successfully used it in a few of my projects as well.
The library also has a heap of examples that show you how to do pretty much everything you will need. It is as simple as including the library manually or via composer, and defining your data-structures etc.
I have also created a tutorial/discussion on how to use php-gds on my site https://buziit.com.au/google-datastore-a-saas-experience/, but note this is a little out of date, as it was written before PHP was officially supported by Datastore. The main difference is in defining the Gateway, but Tom's documentation is up to date (you need to change 1 line).
You would most likely be better off using a library developed by someone who has felt your pain before, as Datastore can be a pain to get right for PHP (at least it was when it was not officially supported for PHP). I can't get away form the library now :)
Upvotes: 0