クライアント側からサーバーに送信された単語を翻訳したい。Google のドキュメントによると、次のように使用できます。
GET https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&source=en&target=de&q=Hello%20world
次の JSON オブジェクトで応答します。
{
"data": {
"translations": [
{
"translatedText": "Hallo Welt"
}
]
}
}
私はphpで以下を使用します:
$data = file_get_contents('https://www.googleapis.com/language/translate/v2?key=KEY8&source=en&target=ru&q='.$to_be_translated);
$data = json_decode($data);
$translated = $data->translations->translatedText;
これは機能しませんが、JS の例を使用すると、必要な結果が得られます。したがって、翻訳の設定は問題ありません。問題は PHP スクリプトにあるはずです。
私も試しました:
$data = $_GET['https://www.googleapis.com/language/translate/v2?key=KEY8&source=en&target=ru&q='.$to_be_translated];
しかし、役に立たない。助言がありますか?