【发布时间】:2020-02-06 14:14:27
【问题描述】:
我正在弄清楚是否有一种方法可以直接通过 API(使用 PHP 托管在应用引擎上)在 Google Compute Engine 上打开和关闭实例。
我找到了这个文档: https://cloud.google.com/compute/docs/reference/rest/v1/instances/start
但不明白这个文档是否是我真正需要的:/
编辑:
$client = new Google_Client();
$client->setApplicationName('Google-ComputeSample/0.1');
$client->useApplicationDefaultCredentials();
$client->addScope('https://www.googleapis.com/auth/cloud-platform');
$service = new Google_Service_Compute($client);
// Project ID for this request.
$project = 'my-project';
// The name of the zone for this request.
$zone = 'europe-west3-c';
// Name of the instance resource to start.
$instance = 'name-instance';
$response = $service->instances->start($project, $zone, $instance);
print_r($response)
得到这个错误: 未捕获的异常“DomainException”和消息“无法加载默认凭据。浏览https://developers.google.com/accounts/docs/application-default-credentials了解更多信息'
我正在尝试从 localhost 运行此代码,我认为这是问题所在。
如果我从项目中的应用程序引擎运行此代码,我想它会起作用。 但是 API 的 google-php-client 文件夹里面有 > 10000 个文件,所以我不能将它推送到应用引擎版本上并尝试它:/
【问题讨论】:
-
第一步是实际实现一些东西,然后在遇到问题时提出问题。您已标记
PHP。您是否查看过 PHP SDK?您想使用原始 HTTP 请求吗?您是否考虑过如何验证和授权您的请求?互联网上有一些关于如何以多种语言启动 GCE 实例的示例。您在问题中包含的文档链接有一个 PHP 示例。你研究过吗?如果是,从那个例子开始有什么问题?
标签: php api google-cloud-platform google-compute-engine