下载谷歌组件
composer require google/cloud-debugger
如果中途下载很慢,那么请切换中文镜像
如果中途报错mismatch啥啥啥
那是composer 缓存太小
composer config -l
composer config --global cache-files-maxsize 1024MiB
支持语言:
https://cloud.google.com/translate/docs/languages?hl=zh-CN
接口:
https://cloud.google.com/translate/docs/?hl=zh-CN
案例:
https://cloud.google.com/translate/docs/translating-text?hl=zh-CN#translate_translate_text-php
Uncaught Google\Cloud\Core\Exception\ServiceException: { "error": { "code": 403, "message": "The request is missing a valid API key.", "errors": [ { "message": "The request is missing a valid API key.", "domain": "global", "reason": "forbidden" } ], "status": "PERMISSION_DENIED" } }
45美金/小时 GG
-
点击即可执行以下操作:
您可以随时在 GCP Console 中查看和管理这些资源。
- 创建或选择项目。
- 启用该项目Cloud Translation API。
- 创建服务帐号。
- 下载 JSON 格式的私钥。
-
将环境变量 GOOGLE_APPLICATION_CREDENTIALS 设置为包含服务帐号密钥的 JSON 文件的文件路径。此变量仅适用于当前的 shell 会话,因此,如果您打开新的会话,请重新设置该变量。
composer require google/cloud-translate
# Includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';
# Imports the Google Cloud client library
use Google\Cloud\Translate\TranslateClient;
# Your Google Cloud Platform project ID
$projectId = 'YOUR_PROJECT_ID';
# Instantiates a client
$translate = new TranslateClient([
'projectId' => $projectId
]);
# The text to translate
$text = 'Hello, world!';
# The target language
$target = 'ru';
# Translates some text into Russian
$translation = $translate->translate($text, [
'target' => $target
]);
echo 'Text: ' . $text . '
Translation: ' . $translation['text'];