【问题标题】:Can't access the Google Cloud Datastore with PHP code无法使用 PHP 代码访问 Google Cloud Datastore
【发布时间】:2020-03-16 23:51:46
【问题描述】:
  • 我已经用 composer(终端)安装了谷歌云文件
  • export GOOGLE_APPLICATION_CREDENTIALS='...'
  • 从 gcloud 收到我的 .json 密钥,该密钥与我的代码位于同一目录中
  • 在我的 google 数据存储中创建了 3 个实体

我正在尝试做的所有事情:

<?php
  session_start();
  require __DIR__ . '/vendor/autoload.php';
  use Google\Cloud\Datastore\DatastoreClient;
  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...
    }
?>

页面加载正常,只是当我提交表单时它崩溃了,给出了异常/错误,说明它找不到 .json 文件或无法验证我的凭据。

[更新]: 通过放置 putenv() 似乎可以创建数据存储对象,但每当我尝试使用 lookup() 函数检索实体时,我都会收到以下错误代码:

Fatal error: Uncaught exception 'Google\Cloud\Core\Exception\ServiceException' 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."

我非常不熟悉 CA 捆绑包是什么,或者我想如何使用它来解决这里的问题。

【问题讨论】:

  • 您好,您能否包括您收到的错误信息?

标签: php gcloud


【解决方案1】:

您之前是否尝试过像[1]那样设置环境变量

<?php
  putenv('GOOGLE_APPLICATION_CREDENTIALS=...');
  session_start();
  require __DIR__ . '/vendor/autoload.php';
  use Google\Cloud\Datastore\DatastoreClient;
  # ...

【讨论】:

  • 谢谢,这允许创建数据存储客户端对象,但我似乎仍然无法检索任何实体(我已更新我的问题,显示我不断收到的错误代码)
  • 关于错误信息,您使用的PHP版本是否低于5.6?如果是这样,您可以迁移到更新的 PHP 版本吗?
  • 我不太确定。在终端中,我的 php 版本是 7.3.11,但在我的目录中的 app.yaml 文件中,该文件在将我的应用程序部署到 gcloud 时使用,它显示“运行时:php55”
  • 您看到的错误与此 GitHub 线程中的错误相似,请查看github.com/googleapis/google-cloud-php/issues/… 中的解决方案
  • 还有 StackOverflow 线程中的另一个响应 stackoverflow.com/a/35569174/6003934
猜你喜欢
  • 2023-03-05
  • 2016-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-08
  • 1970-01-01
相关资源
最近更新 更多