Skip to content

Instantly share code, notes, and snippets.

@YuzuruSano
Last active May 6, 2019 09:55
Show Gist options
  • Save YuzuruSano/ed72a6418a0fd4a9d7957d4598e3593c to your computer and use it in GitHub Desktop.
Save YuzuruSano/ed72a6418a0fd4a9d7957d4598e3593c to your computer and use it in GitHub Desktop.
Cloud Translate APIの利用
<?php
//キーの取得はこちらを参考に
//https://alaki.co.jp/blog/?p=2636
require_once './vendor/autoload.php';
use Google\Cloud\Translate\TranslateClient;
//「TranslateClient」クラスを呼び出し
$translate = new TranslateClient([
'projectId' => {プロジェクトID},
'key' => {APIキー},
]);
// //翻訳開始
$result = $translate->translate('オッケーグーグル、明日の予定は?', [
'target' => 'en',//翻訳したい言語を指定。今回は「日本語→英語」なので「en」
'restOptions' => [//このオプション指定なしだと動かない
'headers' => [
'referer' => {APIで許可したリファラに該当するもの}
]
]
]);
echo '<pre>';
var_dump($result);
echo '</pre>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment