Skip to content
Advertisement

Can’t access the Google Cloud Datastore with PHP code

  • I have installed the google cloud files with composer (terminal)
  • export GOOGLE_APPLICATION_CREDENTIALS=’…’
  • Received my .json key from gcloud which is in the very same directory as my code
  • Made 3 entities in my google Datastore

All I’m trying to do:

<?php
  session_start();
  require __DIR__ . '/vendor/autoload.php';
  use GoogleCloudDatastoreDatastoreClient;
  putenv('GOOGLE_APPLICATION_CREDENTIALS=...');

  // Various checks
  if(isset($_POST["userId"]) && isset($_POST["password"])){
    $userId = $_POST["userId"];
    $password = $_POST["password"];
    $datastore = new DatastoreClient(["..." => $projectId]);
    $key = $datastore->key($userId, $password);
    $entity = $datastore->lookup($key);
    if($entity != null){
      // do stuff...
    }
?>

The page loads fine it’s just when I submit the form it crashes giving exceptions/errors stating it can’t find the .json file or it can’t verify my credentials.

[Update]: By placing the putenv() it seems the datastore object can be made but whenever I try to retrieve an entity using the lookup() function I get the following error code:

Fatal error: Uncaught exception 'GoogleCloudCoreExceptionServiceException' with message
 "No system CA bundle could be found in any of the the common system 
 locations. PHP versions earlier than 5.6 are not properly configured
 to use the system's CA bundle by default. In order to verify peer 
 certificates, you will need to supply the path on disk to a
 certificate bundle to the 'verify' request option:
 http://docs.guzzlephp.org/en/latest/clients.html#verify. 
 If you do not need a specific certificate bundle, then Mozilla provides a commonly used CA bundle which can be downloaded here (provided by the maintainer of cURL): 
 https://raw.githubusercontent.com/bagder/ca-bundle/master/ca-bundle.crt. 
 Once you have a CA bundle available on disk, you can set the 'openssl.cafile' PHP ini setting to point to the path to the file, allowing you to omit the 'verify' request option. 
 See http://curl.haxx.se/docs/sslcerts.html for more information."

I am very unfamiliar with what a CA bundle is or how exactly I am suppose to use it to solve the issue here.

Advertisement

Answer

Have you tried to set the env variable before as in [1]

<?php
  putenv('GOOGLE_APPLICATION_CREDENTIALS=...');
  session_start();
  require __DIR__ . '/vendor/autoload.php';
  use GoogleCloudDatastoreDatastoreClient;
  # ...
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement