我们可以在没有谷歌密钥的情况下使用谷歌翻译吗?

发布于 2024-11-07 13:30:55 字数 1475 浏览 1 评论 0原文

我在几个网站 (http://goo.gl/usUSP) 中找到了这段代码:

<?php
// Basic request parameters:
// s = source language
// d = destination language
// q = Text to be translated

$s = $_REQUEST['s'];
if(!$s)echo "translate.php?s=en&d=es&q=Hello%20World";
$d = $_REQUEST['d'];
$lang_pair = urlencode($s.'|'.$d);
$q = urlencode($_REQUEST['q']);

// Google's API translator URL
$url = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=".$q."&langpair=".$lang_pair;

// Make sure to set CURLOPT_REFERER because Google doesn't like if you leave the referrer out
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "http://www.yoursite.com/translate.php");
$body = curl_exec($ch);
curl_close($ch);

$json = json_decode($body, true);
echo $json['responseData']['translatedText'];
?>

好的,基本上据我所知,使用谷歌翻译 API 的每个调用都必须提供有效的密钥(用户的密钥)。

然而,从上面的示例代码来看,没有给出密钥并且它可以工作!证明: http://juzcode.com/translate.php ?s=en&d=es&q=Hello%20World

关于这个问题我有 3 个问题:

1)这怎么可能? (我不太懂 php)

2) Google 施加的限制是每个 Google-key 100k 个字符。该示例不使用密钥。所以基本上我可以无限使用?

3) 是否违反 http://code.google.com/apis/language/翻译/terms.html ? (我真的试着读过它!但我不是律师,我只是想确定一下)

I found this code in a couple of websites (http://goo.gl/usUSP):

<?php
// Basic request parameters:
// s = source language
// d = destination language
// q = Text to be translated

$s = $_REQUEST['s'];
if(!$s)echo "translate.php?s=en&d=es&q=Hello%20World";
$d = $_REQUEST['d'];
$lang_pair = urlencode($s.'|'.$d);
$q = urlencode($_REQUEST['q']);

// Google's API translator URL
$url = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=".$q."&langpair=".$lang_pair;

// Make sure to set CURLOPT_REFERER because Google doesn't like if you leave the referrer out
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "http://www.yoursite.com/translate.php");
$body = curl_exec($ch);
curl_close($ch);

$json = json_decode($body, true);
echo $json['responseData']['translatedText'];
?>

Ok basically from what I know, every call using the google translate API must provide a valid key (the user's key).

However from the example code above no key is given and it works! prove: http://juzcode.com/translate.php?s=en&d=es&q=Hello%20World.

I've got 3 questions regarding this issue:

1) How is this even possible? (i'm not very php literate)

2) The limit imposed by Google is 100k characters per Google-key. The example uses no key. So basically I'd have unlimited usage?

3) Does it violate http://code.google.com/apis/language/translate/terms.html ? (I've tried to read it, really! But I'm no lawyer, I'd just want to be sure)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

德意的啸 2024-11-14 13:30:55

1)这怎么可能? (我不是
非常懂 PHP)

这是用于基于 AJAX 的调用的 REST API,而不是基于自动化服务器的 API。

2) Google 施加的限制是 100k
每个 Google 键的字符数。例子
不使用密钥。所以基本上我会
无限制使用?

3)是否违反
http://code.google.com/apis/language/translate/terms.html
? (我真的尝试过读它!但是
我不是律师,我只想成为
当然)

你不想这样做,因为:

必须使用 Google Translate API
用于用户生成的翻译。
自动或批量查询任何
严禁此类行为。

使用 PHP 中的curl 被视为自动请求。请改用 Rest API。您将需要一把钥匙。

1) How is this even possible? (i'm not
very php literate)

The is the REST API meant for AJAX based calls, not the automated server based API.

2) The limit imposed by Google is 100k
characters per Google-key. The example
uses no key. So basically I'd have
unlimited usage?

3) Does it violate
http://code.google.com/apis/language/translate/terms.html
? (I've tried to read it, really! But
I'm no lawyer, I'd just want to be
sure)

You don't want to do that because:

The Google Translate API must be used
for user-generated translations.
Automated or batched queries of any
kind are strictly prohibited.

using curl from PHP is considered an automated request. Use the Rest API instead. You will need a key.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文