我想翻譯從客戶端發送到服務器的單詞。據谷歌的文檔,你可以如下使用:Google的翻譯API(REST)和PHP
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];
,但無濟於事。有什麼建議麼?
它以什麼方式不起作用?你有錯誤信息嗎? – Cfreak
http://code.google.com/p/google-api-php-client/source/browse/trunk/examples/translate/simple.php – underscore
$ translation變量爲空。 – jmarais